0

I am using nodemailer to send mail
It works perfectly on the local instance as it is the same device
NOTE: This is just a representation of the mailer program and there are different values in the actual codebase
My code
nodule.js

const env = require("dotenv").config();
const nodemailer = require("nodemailer");
module.exports = {
  email: (html) => {
    var mail = nodemailer.createTransport({
      service: "gmail",
      auth: {
        user: "notanactualmail@gmail.com",
        pass: process.env.DEMO_ACCOUNT_PASSWORD
      }
    });

    var mailOptions = {
      from: "notanactualmail@gmail.com",
      to: process.env.TARGET_MAIL_ID,
      subject:
        "my title",
      html: html
    };

    mail.sendMail(mailOptions, (error, info) => {
      if (error) {
        console.log(error);
      }
    });
  }
}

app.js

const rh = require(`./module.js`);
rh.email("<h1>Hello World</h1>");

Problem

So to the main point Every day google says Unauthorised login prevented Suspicious activity etc...etc...
Heroku logs

2021-10-08T03:24:30.383820+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-10-08T03:24:31.277868+00:00 app[web.1]: app is listening on port 19616
2021-10-08T03:24:32.059451+00:00 heroku[web.1]: State changed from starting to up
2021-10-08T03:24:32.726090+00:00 heroku[router]: at=info method=GET path="/autotrg/ifttt/auth/<Hardcoded name>" host=myapp.herokuapp.com request_id=0032f1f2-7641-475c-85eb-70e1d7d94fe0 fwd="45.248.22.6" dyno=web.1 connect=0ms service=6ms status=304 bytes=149 protocol=https
2021-10-08T03:24:34.224740+00:00 app[web.1]: Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt
2021-10-08T03:24:34.224757+00:00 app[web.1]: 534-5.7.14 suR76UfS5DkBscwE1tOwUYwx2Z6T5MR13HZ8QXHGtOMTfYBSdNVVOvhVItO5_3XwC-NYO
2021-10-08T03:24:34.224758+00:00 app[web.1]: 534-5.7.14 gerVJfsXtIw9zM5OALS3cng_HqJZOQRp5pr1ZyB7xPF7kbXcX8whhc2BDOtoMxsC>
2021-10-08T03:24:34.224758+00:00 app[web.1]: 534-5.7.14 Please log in via your web browser and then try again.
2021-10-08T03:24:34.224759+00:00 app[web.1]: 534-5.7.14  Learn more at
2021-10-08T03:24:34.224759+00:00 app[web.1]: 534 5.7.14  https://support.google.com/mail/answer/78754 l19sm1065983qkk.12 - gsmtp
2021-10-08T03:24:34.224759+00:00 app[web.1]:     at SMTPConnection._formatError (/app/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
2021-10-08T03:24:34.224759+00:00 app[web.1]:     at SMTPConnection._actionAUTHComplete (/app/node_modules/nodemailer/lib/smtp-connection/index.js:1536:34)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at SMTPConnection.<anonymous> (/app/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at SMTPConnection._onData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at TLSSocket.SMTPConnection._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
2021-10-08T03:24:34.224761+00:00 app[web.1]:     at TLSSocket.emit (events.js:400:28)
2021-10-08T03:24:34.224761+00:00 app[web.1]:     at addChunk (internal/streams/readable.js:293:12)
2021-10-08T03:24:34.224762+00:00 app[web.1]:     at readableAddChunk (internal/streams/readable.js:267:9)
2021-10-08T03:24:34.224762+00:00 app[web.1]:     at TLSSocket.Readable.push (internal/streams/readable.js:206:10) {
2021-10-08T03:24:34.224762+00:00 app[web.1]:   code: 'EAUTH',
2021-10-08T03:24:34.224762+00:00 app[web.1]:   response: '534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt\n' +
2021-10-08T03:24:34.224762+00:00 app[web.1]:     '534-5.7.14 suR76UfS5DkBscwE1tOwUYwx2Z6T5MR13HZ8QXHGtOMTfYBSdNVVOvhVItO5_3XwC-NYO\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534-5.7.14 gerVJfsXtIw9zM5OALS3cng_HqJZOQRp5pr1ZyB7xPF7kbXcX8whhc2BDOtoMxsC>\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534-5.7.14 Please log in via your web browser and then try again.\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534-5.7.14  Learn more at\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534 5.7.14  https://support.google.com/mail/answer/78754 l19sm1065983qkk.12 - gsmtp',
2021-10-08T03:24:34.224764+00:00 app[web.1]:   responseCode: 534,
2021-10-08T03:24:34.224764+00:00 app[web.1]:   command: 'AUTH PLAIN'
2021-10-08T03:24:34.224764+00:00 app[web.1]: }

What I Have tried

What am I missing?

  • 1
    Gmail isn't designed to be a mail relay for programmatic use. I strongly advise you to use another service like SendGrid or Mailgun. Both have free tiers and both will give you far fewer headaches. See [Laravel password reset email not sending using gmail on Heroku](https://stackoverflow.com/a/55240850/354577) and [My Node.js not sending mails after uploading it to Heroku](https://stackoverflow.com/a/63440133/354577), for example. – ChrisGPT was on strike Oct 13 '21 at 13:13
  • will oauth 2.0 work as I see it redundant to sign up to these services if yes please give some code samples –  Oct 15 '21 at 02:15
  • ...what does OAuth have to do with anything? And we're not here to "give you code samples". Please take the [tour]. – ChrisGPT was on strike Oct 15 '21 at 02:23
  • dosen't oauth provide access to your account like i grant my app acess to write emails on my behalf [like so](https://console.cloud.google.com/apis/library/gmail.googleapis.com) –  Oct 15 '21 at 04:12
  • You're conflating things. That is for accessing _your users'_ Gmail accounts. – ChrisGPT was on strike Oct 16 '21 at 01:21
  • Why can't I be my own user? [Like so](https://www.npmjs.com/package/gmail-sender-oauth) –  Oct 16 '21 at 03:10
  • You _can_, but you're over-complicating things. And you are not following the rules of Stack Overflow. [One question per post](https://meta.stackexchange.com/q/39223/248627), please. [Comments aren't for extended discussion or new questions](https://stackoverflow.com/help/privileges/comment). Please take a few moments to re-take the [tour] and read [ask]. – ChrisGPT was on strike Oct 16 '21 at 14:11
  • In any case, my point was that you should not just turn on "less secure apps" and use Gmail. There are services that are better suited for this. Whether you choose SendGrid, Mailgun, Gmail enterprise, or anything else that's _actually designed to be used programmatically_ doesn't really matter. Just pick one. – ChrisGPT was on strike Oct 16 '21 at 14:24
  • The problem is that those services need activation by raising a support ticket and because I don't want to reveal my Identity to these companies (as it is ment for only one user) my account doesn't get activated –  Oct 16 '21 at 14:59
  • The idea of my app is simple I fetch a data from podcast's api and put it along with a quote and mail it to the owner –  Oct 16 '21 at 15:00
  • SendGrid and Mailgun are both [available as Heroku addons](https://elements.heroku.com/addons/#email-sms). If you have a verified Heroku account it's as easy as running `heroku addons:create`. In any case, I'm done here. You seem to want to argue more than to learn. Do whatever you want. – ChrisGPT was on strike Oct 16 '21 at 15:04
  • Yes but it doesn't allow me to test locally and it doesn't change the fact that I have to register to these services –  Oct 16 '21 at 15:09
  • You shouldn't be actually sending mail locally in the first place. Imagine accidentally sending mail to a real person while developing! Use [Mailtrap](https://mailtrap.io/) or [MailCatcher](https://mailcatcher.me/) or similar for that. Now I'm done for real. Good luck. – ChrisGPT was on strike Oct 16 '21 at 15:12

1 Answers1

0

You can try using google apis oauth 2.0 and setting yourself as a tester and sending mail on your behalf docs