Skip to content

Instantly share code, notes, and snippets.

@good-idea
Last active April 12, 2018 21:36
Show Gist options
  • Save good-idea/3f20c9d19c5412aa50f017ab9f266d0a to your computer and use it in GitHub Desktop.
Save good-idea/3f20c9d19c5412aa50f017ab9f266d0a to your computer and use it in GitHub Desktop.
Simple postmark emails. (untested POC!)
const postmark = require('postmark')
const config = require('../config')
const client = new postmark.Client(config.postmark.key)
const sendEmail = async ({ From = config.fromEmail, To, Subject, HTMLBody, TextBody, ...opts }) => {
await client.sendEmail({
From,
To,
Subject,
HTMLBody,
TextBody,
...opts
})
}
const juice = require('juice')
const { html } = require('common-tags')
const styles = `
<style type="text/css">
body {
background-color: grey;
}
main {
background-color: white;
margin: 10px auto;
border: 1px solid darkGrey;
}
p > a {
color: tomato;
font-weight: 600;
text-decoration: underline
}
</style>
`
const head = `
<head>
<title>${title}</title>
${css}
</head>
`
const footer = `
<footer>
<p>This email was sent to you.</p>
<h5><a href="...">Unsubscribe</a></h5>
</footer>
`
const textFooter = `
This email was sent to you.
Unsubscribe here: https://www.somewhere.com/unsubscribe/tokenABC123
`
export const composeEmailHTML = (body) => juice(html`${head}<body><main>${body}</main>${footer}</body>`)
export const composeEmailText = (body) => deindent`${body}${textFooter}`
import { composeEmailHTML } from './utils'
import { sendEmail } from './postmark'
export const composeWelcomeEmail = (user, data) => {
const html = composeEmailHTML(`
<h3>${user.displayName},</h3>
<p>Thanks for signing up for Hello World! You can see your profile <a href="${user.profileURL}">here</a></p>
`)
const text = composeEmailText(`
${user.displayName},
Thanks for signing up for Hello World! You can see your profile here:
${user.profileURL}
`)
return { html, text }
}
export const sendWelcomeEmail = async (user, data) => {
const { html, text } = composeWelcomeEmail(user, data)
return sendEmail({
To: user.email,
Subject: 'Welcome to Hello World',
TextBody: text,
HTMLBody: html,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment