I am using the Sendgrid API to send emails from my Node.js project. When running on my local machine this works just fine but now I have deployed to Digital Ocean (Ubuntu) the mails do not send. I have read that Digital Ocean blocks smtp ports by default and you can apparently open them through the command line but I can't find an easy to understand explanation on how to do this.
const nodemailer = require("nodemailer");
const sendgridTransport = require('nodemailer-sendgrid-transport');
const transporter = nodemailer.createTransport(sendgridTransport({
auth: {
api_key: process.env.SENDGRID_API
}
}));
return transporter.sendMail({
to: '[email protected]',
from: email,
subject: subject,
html: `<h1>Contact Form</h1>
<p>Name: ${name}</p>
<p>Email: ${email}</p>
<p>${comments}</p>
`
});
UPDATE
If I remove the .env for the api key and actually hardcode it in eg:
const transporter = nodemailer.createTransport(sendgridTransport({
auth: {
api_key: '12345677788999'
}
}));
then my emails send. This is my .env file ( I am using dotenv)
DB_USER=username
DB_PASSWORD=password
DB_NAME=mydbname
SENDGRID_API=12345677788999
So, not sure why that would be?