qpa-client/server/src/post_office.ts

32 lines
728 B
TypeScript
Raw Normal View History

2019-03-11 13:39:16 +01:00
import { mailgun as mailgunConfig } from './config'
2018-06-03 11:11:45 +02:00
const Mailgun = require('mailgun-js')
2018-04-02 20:23:44 +02:00
interface Email {
from: string
to: string
subject: string
text: string
}
2019-03-11 13:39:16 +01:00
export type PostOffice = (email: Email) => Promise<boolean>
export const sendEmail: PostOffice = async (email: Email) => {
2018-04-21 16:58:09 +02:00
console.log('Will try to send following email', JSON.stringify(email))
2018-04-02 20:23:44 +02:00
return new Promise((resolve, reject) => {
2018-05-10 08:05:56 +02:00
try {
2019-03-11 13:39:16 +01:00
const client = Mailgun(mailgunConfig)
2018-05-10 08:05:56 +02:00
client.messages().send(email, function (error, body) {
if (error) {
reject(error)
} else {
resolve(body)
}
2019-03-11 13:39:16 +01:00
})
2018-05-10 08:05:56 +02:00
} catch (e) {
console.error('Failed to send mail', e)
reject(e)
}
2018-04-02 20:23:44 +02:00
})
2019-03-11 13:39:16 +01:00
}