Skip to content

Commit

Permalink
feat: optional override for Postmark email From: field (#354)
Browse files Browse the repository at this point in the history
This change allows an optional `POSTMARK_SENDER` env variable to be set
alongside the existing `POSTMARK_TOKEN` in case the configure token's
account is not approved for sending from the default (main/real) domain.
Without this the "From: …" sender email address is hardcoded at quite a
low-level which makes it difficult for code re-use.

With this change, I'm enabled to set up a local environment and test
from my *own* Postmark account without needing production keys. (Since I
don't own or control the domain, sending an email from
`[email protected]` gets rejected by the Postmark server with an error
if requested via my own API token.)

I have avoided touching most of the many places where environment
variables are handled because most of those deal in terms of
**required** env. This new config variable is optional: available for
those who need it, but if not set, the email logic simply stays
defaulted to what was the hardcoded sender.

---------

Co-authored-by: Alan Shaw <[email protected]>
Co-authored-by: Travis Vachon <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2023
1 parent 99cbd2f commit f6b2350
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/access-api/src/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface Env {
PRIVATE_KEY: string
SENTRY_DSN: string
POSTMARK_TOKEN: string
POSTMARK_SENDER?: string
LOGTAIL_TOKEN: string
// bindings
SPACES: KVNamespace
Expand Down
1 change: 1 addition & 0 deletions packages/access-api/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function loadConfig(env) {
ENV: parseRuntimeEnv(vars.ENV),

POSTMARK_TOKEN: vars.POSTMARK_TOKEN,
POSTMARK_SENDER: env.POSTMARK_SENDER,
SENTRY_DSN: vars.SENTRY_DSN,
LOGTAIL_TOKEN: vars.LOGTAIL_TOKEN,

Expand Down
5 changes: 4 additions & 1 deletion packages/access-api/src/utils/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export function getContext(request, env, ctx) {
validations: new Validations(config.VALIDATIONS),
accounts: new Accounts(config.DB),
},
email: new Email({ token: config.POSTMARK_TOKEN }),
email: new Email({
token: config.POSTMARK_TOKEN,
sender: config.POSTMARK_SENDER,
}),
uploadApi: createUploadApiConnection({
audience: DID.parse(config.DID).did(),
url: new URL(config.UPLOAD_API_URL),
Expand Down
6 changes: 4 additions & 2 deletions packages/access-api/src/utils/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ export class Email {
*
* @param {object} opts
* @param {string} opts.token
* @param {string} [opts.sender]
*/
constructor(opts) {
this.sender = opts.sender || 'web3.storage <[email protected]>'
this.headers = {
Accept: 'text/json',
'Content-Type': 'text/json',
Expand All @@ -22,7 +24,7 @@ export class Email {
method: 'POST',
headers: this.headers,
body: JSON.stringify({
From: 'web3.storage <[email protected]>',
From: this.sender,
To: opts.to,
TemplateAlias: 'welcome',
TemplateModel: {
Expand Down Expand Up @@ -57,7 +59,7 @@ export class Email {
method: 'POST',
headers: this.headers,
body: JSON.stringify({
From: 'web3.storage <[email protected]>',
From: this.sender,
To: to,
TextBody: textBody,
Subject: subject,
Expand Down

0 comments on commit f6b2350

Please sign in to comment.