Created
August 21, 2024 08:48
-
-
Save sartimo/af81d3c56d93dd50d20eb83fa28139c4 to your computer and use it in GitHub Desktop.
Local Devenv Nodemailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
mailhog: | |
image: mailhog/mailhog | |
container_name: mailhog | |
ports: | |
- "1025:1025" | |
- "8025:8025" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nodemailer from 'nodemailer'; | |
import dotenv from 'dotenv'; | |
dotenv.config(); | |
async function main() { | |
let transporter; | |
if (process.env.DEVENV === 'development') { | |
transporter = nodemailer.createTransport({ | |
host: 'localhost', | |
port: 1025, | |
secure: false, // Note that secure should be false for local testing | |
auth: null, // No auth in local development with Mailhog | |
}); | |
} else { | |
transporter = nodemailer.createTransport({ | |
host: 'smtp.resend.com', | |
port: 465, | |
secure: true, | |
auth: { | |
user: 'resend', | |
pass: 're_123456789', | |
}, | |
}); | |
} | |
const info = await transporter.sendMail({ | |
from: '[email protected]', | |
to: '[email protected]', | |
subject: 'Hello World', | |
html: '<strong>It works!</strong>', | |
}); | |
console.log('Message sent: %s', info.messageId); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment