|
| 1 | +import FormData from 'form-data'; |
| 2 | +import Router from 'koa-router'; |
| 3 | +import Mailgun from 'mailgun.js'; |
| 4 | + |
| 5 | +import { config } from '../config'; |
| 6 | + |
| 7 | +export function prayerRequestPOST(): Router.IMiddleware<unknown, unknown> { |
| 8 | + return async (ctx, _next) => { |
| 9 | + // Invokes the method to send emails given the above data with the helper library |
| 10 | + try { |
| 11 | + const { email, prayFor } = ctx.request.body; |
| 12 | + |
| 13 | + if (!config.apiKey || !config.domain) { |
| 14 | + throw new Error('APPSETTINGS_API_KEY and / or APPSETTINGS_DOMAIN not configured'); |
| 15 | + } |
| 16 | + |
| 17 | + if (!config.prayerRequestFromEmail || !config.prayerRequestRecipientEmail) { |
| 18 | + throw new Error( |
| 19 | + 'APPSETTINGS_PRAYER_REQUEST_FROM_EMAIL and / or APPSETTINGS_PRAYER_REQUEST_RECIPIENT_EMAIL not configured' |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + // We pass the api_key and domain to the wrapper, or it won't be able to identify + send emails |
| 24 | + // const mailgun = new Mailgun({ apiKey, domain }); |
| 25 | + const mailgun = new Mailgun(FormData); |
| 26 | + const mg = mailgun.client({ username: 'api', key: config.apiKey }); |
| 27 | + |
| 28 | + const prayerRequest = { |
| 29 | + from: email, // prayerRequestFromEmail, |
| 30 | + to: config.prayerRequestRecipientEmail, |
| 31 | + subject: 'Please could you pray for me', |
| 32 | + text: `Hi, |
| 33 | +
|
| 34 | +I'd love it if you could pray for me about this: |
| 35 | +
|
| 36 | +${prayFor}`, |
| 37 | + }; |
| 38 | + // await mailgun.messages().send(prayerRequest); |
| 39 | + await mg.messages.create(config.domain, prayerRequest); |
| 40 | + |
| 41 | + const text = `Thank you for your prayer request. |
| 42 | +
|
| 43 | +You are in our thoughts and prayers. |
| 44 | +
|
| 45 | +Your Poor Clare sisters, Arundel.`; |
| 46 | + |
| 47 | + const html = `<html> |
| 48 | +<head> |
| 49 | + <title>Thank you for your prayer request.</title> |
| 50 | +</head> |
| 51 | +<body> |
| 52 | + <div> |
| 53 | + <img src="https://www.poorclaresarundel.org/prayer-request-image.webp" /> |
| 54 | + </div> |
| 55 | + <div style="padding:10px;font-family: Verdana, Helvetica, Sans-Serif;"> |
| 56 | + <p>Thank you for your prayer request.</p> |
| 57 | +
|
| 58 | + <p>You are in our thoughts and prayers.</p> |
| 59 | +
|
| 60 | + <p>Your Poor Clare sisters, Arundel.</p> |
| 61 | + </div> |
| 62 | +</body> |
| 63 | +</html>`; |
| 64 | + |
| 65 | + const reassuringResponse = { |
| 66 | + from: config.prayerRequestFromEmail, |
| 67 | + to: email, |
| 68 | + subject: 'Your prayer request', |
| 69 | + text, |
| 70 | + html, |
| 71 | + }; |
| 72 | + // await mailgun.messages().send(reassuringResponse); |
| 73 | + await mg.messages.create(config.domain, reassuringResponse); |
| 74 | + |
| 75 | + ctx.body = { ok: true, text: 'Thanks for sending your prayer request - we will pray.' }; |
| 76 | + } catch (exc) { |
| 77 | + console.error(exc instanceof Error ? exc.message : exc); |
| 78 | + |
| 79 | + ctx.body = { |
| 80 | + success: false, |
| 81 | + text: `Your prayer request has not been sent - please try mailing: ${config.prayerRequestFromEmail}`, |
| 82 | + }; |
| 83 | + } |
| 84 | + }; |
| 85 | +} |
0 commit comments