From b28ed7711fe83b51f86ce0f356ab82b4a9defd0d Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Mon, 25 Nov 2024 23:00:55 +0100 Subject: [PATCH] Send donation receipt via mailersend --- package.json | 1 + src/donation/email.ts | 82 ++++++++++++++----------------- src/types/environment.d.ts | 2 + yarn.lock | 98 +++++++++++++++++++++++++++++++++++--- 4 files changed, 130 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 0bc95c5..f46a4b0 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "date-fns": "^4.1.0", "html-to-text": "^9.0.5", "juice": "^11.0.0", + "mailersend": "^2.3.0", "next": "^15.0.3", "nodemailer": "^6.9.16", "pg": "^8.13.1", diff --git a/src/donation/email.ts b/src/donation/email.ts index f7836c0..411b845 100644 --- a/src/donation/email.ts +++ b/src/donation/email.ts @@ -1,5 +1,6 @@ import { htmlToText } from "html-to-text"; import juice from "juice"; +import { EmailParams, MailerSend, Recipient } from "mailersend"; import path from "node:path"; import { type BankTransferInfo, @@ -18,6 +19,10 @@ import { setDonationEmailed, } from "src"; +const mailerSend = new MailerSend({ + apiKey: process.env.MAILERSEND_API_KEY, +}); + export async function sendNewEmails() { await dbExecuteInTransaction(async (db) => { const donationsToEmail = await getDonationsToEmail(db); @@ -94,55 +99,40 @@ export async function sendPaymentEmail( donation: DonationToEmail, bank?: BankTransferInfo, ) { - const { email } = donation; - - const htmlNoInline = paymentReceipt(donation, bank); - const text = htmlToText(htmlNoInline); - const html = juice(htmlNoInline); - const prefix = process.env.VERCEL_ENV === "production" ? "" : "DEV: "; - const bcc = - (donation.frequency === "once" && - donation.amount >= - Number.parseInt(process.env.BCC_DONATION_ONCE_LARGE_AMOUNT ?? "0")) || - (donation.frequency === "monthly" && - donation.amount >= - Number.parseInt(process.env.BCC_DONATION_MONTHLY_LARGE_AMOUNT ?? "0")) - ? `<${process.env.BCC_DONATION_LARGE_EMAIL}>` - : undefined; - - const letter = { - from: '"Giv Effektivt" ', - replyTo: '"Giv Effektivt Donation" ', - to: `<${email}>`, - bcc, - subject: `${prefix}Kvittering for donation via Giv Effektivt`, - attachments: [ + const emailParams = new EmailParams() + .setTo([new Recipient(donation.email)]) + .setTemplateId(process.env.MAILERSEND_TEMPLATE_DONATION) + .setPersonalization([ { - filename: "t.png", - path: path.join(process.cwd(), "public", "t.png"), - cid: "twitterLogo", + email: donation.email, + data: { + amount: donation.amount.toLocaleString("da-DK"), + donation_id: donation.id, + tax_deductible: donation.tax_deductible, + recipient: donation.recipient, + bank_msg: bank?.msg, + }, }, - { - filename: "G.png", - path: path.join(process.cwd(), "public", "G.png"), - cid: "giveffektivtLogo", - }, - { - filename: "f.png", - path: path.join(process.cwd(), "public", "f.png"), - cid: "facebookLogo", - }, - { - filename: "in.png", - path: path.join(process.cwd(), "public", "in.png"), - cid: "linkedinLogo", - }, - ], - text, - html, - }; + ]); - await sendReceiptEmail(letter); + const isDonationOnceLarge = + donation.frequency === "once" && + donation.amount >= + Number.parseInt(process.env.BCC_DONATION_ONCE_LARGE_AMOUNT ?? "0"); + + const isDonationMonthlyLarge = + donation.frequency === "monthly" && + donation.amount >= + Number.parseInt(process.env.BCC_DONATION_MONTHLY_LARGE_AMOUNT ?? "0"); + + if ( + process.env.BCC_DONATION_LARGE_EMAIL && + (isDonationOnceLarge || isDonationMonthlyLarge) + ) { + emailParams.setBcc([new Recipient(process.env.BCC_DONATION_LARGE_EMAIL)]); + } + + await mailerSend.email.send(emailParams); } export async function sendFailedRecurringDonationEmail( diff --git a/src/types/environment.d.ts b/src/types/environment.d.ts index 088b3d7..f642290 100644 --- a/src/types/environment.d.ts +++ b/src/types/environment.d.ts @@ -24,6 +24,8 @@ declare global { EMAIL_DONATION_USERNAME: string; EMAIL_DONATION_PASSWORD: string; DEV_WEBSITE_DOMAINS: string; + MAILERSEND_API_KEY: string; + MAILERSEND_TEMPLATE_DONATION: string; } } } diff --git a/yarn.lock b/yarn.lock index 7e5f730..45e8fd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -768,6 +768,13 @@ acorn@^8.14.0: resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.0.tgz#063e2c70cac5fb4f6467f0b11152e04c682795b0" integrity sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA== +agent-base@6: + version "6.0.2" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" + integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== + dependencies: + debug "4" + ajv@^6.12.4: version "6.12.6" resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4" @@ -1287,6 +1294,13 @@ dayjs@^1.10.7: resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.9.tgz#9ca491933fadd0a60a2c19f6c237c03517d71d1a" integrity sha512-QvzAURSbQ0pKdIye2txOzNaHmxtUBXerpY0FJsFXUMKbIZeFm5ht1LS/jFsrncjnmtv8HsG0W2g6c0zUjZWmpA== +debug@4, debug@^4.3.1, debug@^4.3.7: + version "4.3.7" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52" + integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ== + dependencies: + ms "^2.1.3" + debug@^3.2.7: version "3.2.7" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a" @@ -1301,13 +1315,6 @@ debug@^4.1.1, debug@^4.3.2, debug@^4.3.3, debug@^4.3.4: dependencies: ms "2.1.2" -debug@^4.3.1, debug@^4.3.7: - version "4.3.7" - resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52" - integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ== - dependencies: - ms "^2.1.3" - decache@^3.0.5: version "3.1.0" resolved "https://registry.yarnpkg.com/decache/-/decache-3.1.0.tgz#4f5036fbd6581fcc97237ac3954a244b9536c2da" @@ -2005,6 +2012,11 @@ expect-type@^1.1.0: resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-1.1.0.tgz#a146e414250d13dfc49eafcfd1344a4060fa4c75" integrity sha512-bFi65yM+xZgk+u/KRIpekdSYkTB5W1pEf0Lt8Q8Msh7b+eQ7LXVtIB1Bkm4fvclDEL1b2CZkMhv2mOeF8tMdkA== +extend@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa" + integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g== + fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" @@ -2153,6 +2165,16 @@ functions-have-names@^1.2.2, functions-have-names@^1.2.3: resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834" integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ== +gaxios@^5.0.1: + version "5.1.3" + resolved "https://registry.yarnpkg.com/gaxios/-/gaxios-5.1.3.tgz#f7fa92da0fe197c846441e5ead2573d4979e9013" + integrity sha512-95hVgBRgEIRQQQHIbnxBXeHbW4TqFk4ZDJW7wmVtvYar72FdhRIo1UGOLS2eRAKCPEdPBWu+M7+A33D9CdX9rA== + dependencies: + extend "^3.0.2" + https-proxy-agent "^5.0.0" + is-stream "^2.0.0" + node-fetch "^2.6.9" + get-caller-file@^2.0.5: version "2.0.5" resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e" @@ -2395,6 +2417,14 @@ htmlparser2@^9.1.0: domutils "^3.1.0" entities "^4.5.0" +https-proxy-agent@^5.0.0: + version "5.0.1" + resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz#c59ef224a04fe8b754f3db0063a25ea30d0005d6" + integrity sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA== + dependencies: + agent-base "6" + debug "4" + human-signals@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0" @@ -2720,6 +2750,14 @@ isexe@^2.0.0: resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" integrity sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw== +isomorphic-unfetch@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz#87341d5f4f7b63843d468438128cb087b7c3e98f" + integrity sha512-geDJjpoZ8N0kWexiwkX8F9NkTsXhetLPVbZFQ+JTW239QNOwvB0gniuR1Wc6f0AMTn7/mFGyXvHTifrCp/GH8Q== + dependencies: + node-fetch "^2.6.1" + unfetch "^4.2.0" + iterator.prototype@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/iterator.prototype/-/iterator.prototype-1.1.3.tgz#016c2abe0be3bbdb8319852884f60908ac62bf9c" @@ -2864,6 +2902,15 @@ magic-string@^0.30.12: dependencies: "@jridgewell/sourcemap-codec" "^1.5.0" +mailersend@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/mailersend/-/mailersend-2.3.0.tgz#56e4511528441c25931bbe1edb51556085f8ac86" + integrity sha512-pe498Ry7VaAb+oqcYqmPw1V7FlECG/mcqahQ3SiK54en4ZkyRwjyxoQwA9VU4s3npB+I44LlQGUudObZQe4/jA== + dependencies: + gaxios "^5.0.1" + isomorphic-unfetch "^3.1.0" + qs "^6.11.0" + mensch@^0.3.4: version "0.3.4" resolved "https://registry.yarnpkg.com/mensch/-/mensch-0.3.4.tgz#770f91b46cb16ea5b204ee735768c3f0c491fecd" @@ -2982,6 +3029,13 @@ next@^15.0.3: "@next/swc-win32-x64-msvc" "15.0.3" sharp "^0.33.5" +node-fetch@^2.6.1, node-fetch@^2.6.9: + version "2.7.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" + integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A== + dependencies: + whatwg-url "^5.0.0" + nodemailer@^6.9.16: version "6.9.16" resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.9.16.tgz#3ebdf6c6f477c571c0facb0727b33892635e0b8b" @@ -3433,6 +3487,13 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.0.tgz#f67fa67c94da8f4d0cfff981aee4118064199b8f" integrity sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA== +qs@^6.11.0: + version "6.13.1" + resolved "https://registry.yarnpkg.com/qs/-/qs-6.13.1.tgz#3ce5fc72bd3a8171b85c99b93c65dd20b7d1b16e" + integrity sha512-EJPeIn0CYrGu+hli1xilKAPXODtJ12T0sP63Ijx2/khC2JtuaN3JyNIpvmnkmaEtha9ocbG4A4cMcr+TvqvwQg== + dependencies: + side-channel "^1.0.6" + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -4023,6 +4084,11 @@ toposort@^2.0.2: resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330" integrity sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg== +tr46@~0.0.3: + version "0.0.3" + resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" + integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== + traverse-chain@~0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/traverse-chain/-/traverse-chain-0.1.0.tgz#61dbc2d53b69ff6091a12a168fd7d433107e40f1" @@ -4178,6 +4244,11 @@ undici@^6.19.5: resolved "https://registry.yarnpkg.com/undici/-/undici-6.21.0.tgz#4b3d3afaef984e07b48e7620c34ed8a285ed4cd4" integrity sha512-BUgJXc752Kou3oOIuU1i+yZZypyZRqNPW0vqoMPl8VaoalSfeR0D8/t4iAS3yirs79SSMTxTag+ZC86uswv+Cw== +unfetch@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-4.2.0.tgz#7e21b0ef7d363d8d9af0fb929a5555f6ef97a3be" + integrity sha512-F9p7yYCn6cIW9El1zi0HI6vqpeIvBsr3dSuRO6Xuppb1u5rXpCPmMvLSyECLhybr9isec8Ohl0hPekMVrEinDA== + untildify@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/untildify/-/untildify-4.0.0.tgz#2bc947b953652487e4600949fb091e3ae8cd919b" @@ -4268,6 +4339,11 @@ web-resource-inliner@^7.0.0: mime "^2.4.6" valid-data-url "^3.0.0" +webidl-conversions@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" + integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== + whatwg-encoding@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/whatwg-encoding/-/whatwg-encoding-3.1.1.tgz#d0f4ef769905d426e1688f3e34381a99b60b76e5" @@ -4280,6 +4356,14 @@ whatwg-mimetype@^4.0.0: resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-4.0.0.tgz#bc1bf94a985dc50388d54a9258ac405c3ca2fc0a" integrity sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg== +whatwg-url@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" + integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw== + dependencies: + tr46 "~0.0.3" + webidl-conversions "^3.0.0" + which-boxed-primitive@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"