Files
portfolio/api/src/lib/email.ts

40 lines
958 B
TypeScript

import * as nodemailer from 'nodemailer'
interface Options {
to: string | string[]
subject: string
html: string
text: string
}
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL,
pass: process.env.GMAIL_SMTP_PASSWORD,
},
})
export async function sendEmail({ to, subject, text, html }: Options) {
return await transporter.sendMail({
from: `"${process.env.NAME} (noreply)" <${process.env.GMAIL}>`,
to: Array.isArray(to) ? to : [to],
subject,
text,
html,
})
}
export function censorEmail(email: string): string {
const [localPart, domain] = email.split('@')
if (localPart.length <= 2) return `${localPart}@${domain}`
const firstChar = localPart[0]
const lastChar = localPart[localPart.length - 1]
const middleLength = Math.min(localPart.length - 2, 7)
const middle = '#'.repeat(middleLength)
return `${firstChar}${middle}${lastChar}@${domain}`
}