Skip to content

Commit

Permalink
use resend iso nodemailer, and upgrade typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ekulno committed Jun 28, 2024
1 parent c342fa1 commit ffc1113
Show file tree
Hide file tree
Showing 3 changed files with 489 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
},
"dependencies": {
"@types/express": "^4.17.21",
"@types/nodemailer": "^6.4.14",
"body-parser": "^1.20.2",
"email-validator": "^2.0.4",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"nodemailer": "^6.9.10",
"resend": "^3.4.0",
"typescript": "^5.3.3"
},
"type": "module",
Expand All @@ -20,5 +19,6 @@
"main": "lib/index.js",
"repository": "https://github.com/ekulno/contact-form-mailer",
"author": "ekulno",
"license": "MIT"
"license": "MIT",
"packageManager": "[email protected]+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
}
29 changes: 11 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'express-async-errors';
import express, {NextFunction, Request, Response} from "express";
import bodyParser from "body-parser";
import nodemailer from 'nodemailer';
import { validate as validateEmail } from 'email-validator';

import { Resend } from 'resend';
const app = express();

app.use(bodyParser.urlencoded({ extended: false }))
Expand All @@ -12,19 +11,12 @@ app.use(bodyParser.json())
const port = process.env['PORT'] || 3000;

const toMail = process.env['TO_EMAIL'];

const smtp = {
service: process.env['SMTP_SERVICE']!,
host: process.env['SMTP_HOST']!,
port: parseInt(process.env['SMTP_PORT']!),
secure: !!(process.env['SMTP_SECURE'] && process.env['SMTP_SECURE'] != 'false'),
auth: {
user: process.env['SMTP_AUTH_USER']!,
pass: process.env['SMTP_AUTH_PASS']!,
}
const fromMail = process.env['FROM_EMAIL'];
if (!toMail||!fromMail){
throw new Error("Incomplete mail config");
}

const transporter = nodemailer.createTransport(smtp);
const resend = new Resend(process.env['RESEND_API_KEY'])

interface ContactRequest {
email: string;
Expand Down Expand Up @@ -56,23 +48,24 @@ app.post("/contact", async (req, res) => {
res.status(400).send("No message");
return;
}
const info = await transporter.sendMail({
from: `<${smtp.auth.user}>`,
replyTo: `${name?`"${name}"`:''} <${email}>`,
const info = await resend.emails.send({
from: fromMail,
reply_to: `${name?`"${name}"`:''} <${email}>`,
to: toMail,
subject: subject || "Contact form message",
text: message
});

if (info.rejected.length) {
if (info.error) {
console.error(info.error)
res.status(500).send("Failed to send message");
return;
}
res.sendFile(process.cwd()+'/assets/thanks.html')
});

app.use(async (err:Error, req:Request, res:Response, next:NextFunction) => {
console.error(err.stack)
console.error(err)
res.status(500).send('Internal server error');
return;
});
Expand Down
Loading

0 comments on commit ffc1113

Please sign in to comment.