I was curious about the new nodejs_compat
mode and found that nodemailer
now works in it. SMTP transport still doesn’t work, but you can import the library, build an email payload, and probably use other transports like Amazon SES.
I previously tried building my own email encoding, but it’s quite tricky. It’s nice to be able to rely on nodemailer
for this now.
I was able to build a mail payload like this. GitHub Gist for reference.
import nodemailer from 'nodemailer'
import { Readable } from 'node:stream'
const transporter = nodemailer.createTransport({
streamTransport: true,
newline: 'windows',
})
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = new URL(request.url)
const from = url.searchParams.get('from')
const to = url.searchParams.get('to')
const subject = url.searchParams.get('subject')
const text = url.searchParams.get('text')
const html = url.searchParams.get('html')
const mail = await transporter.sendMail({
from: from ?? undefined,
to: to ?? undefined,
subject: subject ?? undefined,
text: text ?? undefined,
html: html ?? undefined,
})
const readableWebStream = Readable.toWeb(mail.message as Readable)
return new Response(readableWebStream as any, {
headers: {
'content-type': 'text/plain',
},
})
},
} satisfies ExportedHandler<Env>