Mail sends from localhost but not from live site on digital ocean - node.js

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: 'info#example.com',
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?

Have you exported this "SENDGRID_API" in the environment. Then only you can use process.env.SomeEnvironmentVar.
If you are using PM2. You will have to add it to the ecosystem file. Then it will use that config and boot your application. Making all the vars available.

I had the same issue using SendGrid, Digitalocean (Ubuntu) and PM2.
It seems that PM2 doesn't automatically detect changes in .env file as #himank mentioned in his answer, so i had to delete the project from PM2, then started it again.
Kindly note:
I followed this guide to set global Environment variables:
https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/
I also followed this guide to configure PM2 with my Nodejs (Nestjs) project:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

Related

Heroku PostgreSQL - could not send SSL negotiation packet: Resource temporarily unavailable

I'm using Heroku's Postgres add-on in one of my NodeJS apps. The code seems to work but any connections to the database using this module hangs and never responds with neither a success or a failure.
To investigate the issue, I tried connecting to the database through my terminal using pgcli <db url> but got the following error:
could not send SSL negotiation packet: Resource temporarily unavailable.
Further details:
Heroku's console shows that the database is healthy, up, and running.
Last time I used the database (around 3 months ago) it was working perfectly.
Any newly created databases from the same add-on work as expecting.
Any help would be appreciated.
Go to heroku settings, find your environmental variables.
Find the DATABASE_URL variable, copy its value
Go to your project add .env file and add the environmental variable there
Install and follow instructions on env module .e.g https://www.npmjs.com/package/dotenv
Add your connection config
.env
DATABASE_URL=verylongstring
index.js
require('dotenv').config()
const pg = require('pg'
const pgPool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})
If your using github or some other service to store your code make sure to not send the .env file with you since its only for local development. Heroku has the environmental variables in your app.
.gitignore
.env

When I run npm run develop on server, I am unable to access it on the server IP, why?

I am trying to run Strapi project on AWS Lightsail server. When I run the command npm run develop, i can not access the admin dashboard. However, it works fine on my local computer. Am I missing something?
In your config/server.js file, change the host ip directly or through environment variables like :
module.exports =
({ env }) = > ({
port: env.int('MYPORT','Your default value if your port not
accessible'),
host: env('MYHOSTIP','default value'),
});
Now in your .env create your variables:
MYPORT=8080
MYHOSTIP=192.168.172.112

Using dotenv with bundled client side code

I am creating a node js application as a Contentful UI Extension. The code is hosted here: https://github.com/doodybrains/media-tagging-extension
A lot of the gulp file is boiler plate but in the end everything gets bundled into an index.html file. I know that env variables shouldn't be called or processed in the client code but I don't know how to get them in there before the project is built. When I run the repo in development and call process.env.NAME_OF_TOKEN from src/index.js it returns undefined. I have tried import dotenv, creating a gulp env pipeline etc.
ANY ADVICE will be so helpful. The app is being deployed to Netlify and I already have the env variables set up there as well.
thank you
You can create another js file which can use NODE_ENV to set correct variable.
I prefer calling a service to get all my properties on app start and set in some map. I use the map to set the value in different places in my code.
Some sample code...
const env = process.env.NODE_ENV || 'local';
const sit = {
URL: 'sit url',
HOST: 'sit host',
ENV: 'sit'
};
const uat = {
URL: 'uat url',
HOST: 'uat host',
ENV: 'uat'
};
var property_service_url = config[env].URL;
var property_service_host = config[env].HOST;
Before starting your app, you can set the NODE_ENV=environment. For example in linux.
export NODE_ENV=uat
This will make sure that environment set correctly. Now in your app.js for , you can call the service to load your properties. If you don't want to call a service, you can set it in the same way the URL and HOST are set.

How to catch email from local app server. Nodejs

I have lite nodejs server. I am trying to implement password reset flow. I use nodemailer to send email with reset password link. I want to test if I send email properly. I don't want to use any remote smpt.
What I can use on my local environment to catch emails and check if they are good?
Try this command:
python -m smtpd -n -c DebuggingServer localhost:1025
I used mailcatcher. It is a super simple SMTP server which catches any message sent to it to display in a web interface.
I have following configuration:
let smtpConfig = {
host: '127.0.0.1',
port: 1025,
secure: false, // upgrade later with STARTTLS
auth: {
user: 'user',
pass: 'password'
}
};
Do not forget to add user and password in auth section. It can be even white space. But it can't be empty string in this case you'll get error.
const smtpTransport = nodemailer.createTransport(smtpConfig);
And now you can see all sent emails here http://127.0.0.1:1080

vmc push Errno::ENOENT: No such file or directory

I am trying to deploy this example to Cloud Foundry
https://github.com/andris9/Nodemailer/blob/master/examples/example_smtp.js
Here is what I did:
npm install nodemailer
Rename example_smtp.js to app.js and edit fields for right email credential
Tested node app.js locally and it works / email sent
vmc push and got below error
Uploading mytestmailer... FAILED Upload failed. Try again with 'vmc
push'. Errno::ENOENT: No such file or directory -
C:/Users/username/AppData/Local/Temp/.
vmc_hocmailer_files/node_modules/nodemailer/node_modules/simplesmtp/node_modules
/xoauth2/node_modules/request/node_modules/form-data/node_modules/combined-strea
m/node_modules/delayed-stream/test/integration/test-delayed-http-upload.js
For more information, see ~/.vmc/crash
I search and found this thread to figure out what is in ~/.vmc/crash
how to access ~/.vmc/crash folder on Cloud foundry
But I am using Windows so I cannot find that crash file.
Can someone help me to troubleshoot this issue? Seem to be a simple deployment.
You need to make sure you create a package.json file that specifies the dependencies of the application, in this case it should look something like this;
{
"name": "node-mailer-example",
"version": "0.0.1",
"dependencies": {
"nodemailer": "*"
}
}
With nothing but this and app.js in the folder, install the dependencies for the application with npm;
npm install
This should create a new 'node_modules' folder with the dependencies included inside.
Also, you need to change the first line of your app.js file so it reads;
var nodemailer = require('nodemailer');
I did it like this and it deployed ok, I got this message in the application log;
SMTP Configured
Sending Mail
Error occured
Invalid login - 535-5.7.1 Please log in with your web browser and then try again. Learn more at
535 5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 hr1sm24898342qeb.3 - gsmtp
BTW, when you send mail on mass through gMail, where each e-mail has the same content, gMail will eventually require you log in via the web interface before you can continue.

Resources