0

I want to send an email from NodeJs. I did a quick search on npmjs.org and found 2 promising libraries:

Nodemailer

and

EmailJs

First, I followed the instructions to install and run NodeMailer, but It does not work. I wanted to send email from Gmail, thus, I enabled the "less secured" apps and also enabled captcha as suggested in the documentation of Nodemailer. However, even though I did this I could not make it work.

In addition, I tried to switch to my University's email, so I found its SMTP server and Port and configured the settings. The error persisted, mainly:

(node:825) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): Error: connect EAGAIN 134.91.56.35:587 - Local (0.0.0.0:58442)
(node:825) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

I am not exactly sure how to fix it, so I tried the next client, EmailJs. The error there is:

{ Error: connection encountered an error (connect EAGAIN 134.91.56.35:587 ...
code: 5,
previous: ...
... smtp: undefied}

I can clearly see that it is written that smtp is undefined, but I have verified that it is the same.

To clarify, I am running NodeJs on Bash on Windows, but since I know there are some issues with it I also run the code on a Raspberry PI 3.

Thank you!

AGoranov
  • 2,114
  • 3
  • 15
  • 27

1 Answers1

0

I managed to configure it.

As suggested I used Gmail-node. To do this you have to follow some steps:

  • First, install it using npm install gmail-node
  • Open link https://console.developers.google.com/flows/enableapi?apiid=gmail
  • Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API.
  • Click Continue, then Go to credentials.
  • At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
  • Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  • Select the application type Other, enter the any name "gmail-node-app", and click the Create button.
  • Click OK to dismiss the resulting dialog.
  • Click the file_download (Download JSON) button to the right of the client ID.
  • Move this file to your working directory and use it with any name like client_secret.json.

Now your program would look like this where you have to change the information in accordance to the file you just downloaded:

var gmailNode = require('gmail-node');
var clientSecret = {
    installed: {
        client_id: "k677725446467-6li25pcqgkcllsoh6f6dijcvse64n9pf.apps.googleusercontent.com",
        project_id: "clean-node-129606",
        auth_uri: "https://accounts.google.com/o/oauth2/auth",
        token_uri: "https://accounts.google.com/o/oauth2/token",
        auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
        client_secret: "kF7DvoA_ZrNfa65GnU2zQBgw7",
        redirect_uris: [
            "urn:ietf:wg:oauth:2.0:oob",
            "http://localhost"
        ]
    }
};
gmailNode.init(clientSecret, './token.json', function(err,data){ ... });

However, after taking this from the website, I still had an unknown problem, namely Error: No access or refresh token is set..

In order to solve it, you need to generate url with a "TokenURL" by log this code:

console.log(gmailNode.generateUrl(clientSecret));

Follow the generated URL and copy the "TokenURL" there.

Lastly, send an email with:

gmailNode.generateToken(clientSecret, tokenURL, (err, data) => {
        gmailNode.sendWithToken(emailMessage, clientSecret, data, function(err,data) {
                console.log(err, data)
        })
})

And you are done!

Note that we don't need init anymore.

AGoranov
  • 2,114
  • 3
  • 15
  • 27