简体   繁体   中英

How can I get nodemailer to work with heroku?

I am using nodemailer and gmail on my portfolio site which is used to send messages directly to my email. This works on my local server (localhost:3000) but doesn't work when I deploy it to heroku. I did however get a message from gmail 'someone has accessed your account' after I deployed with heroku incase that's relevant.

Here is my app.js:

const nodemailer = require('nodemailer');

let port = process.env.PORT || 3000;
let transporter = nodemailer.createTransport({
    service: 'gmail',
    secure: false,
    port: 25,
    auth:{
        user: 'myownemail@gmail.com',
        pass: '**************'
    }
})

let mailOptions = {
    from: '"Portfolio Message" <myotheremail@gmail.com> ',
    to: 'myownemail@gmail.com',
    subject: '',
    text: '',
    html: ''
}

app.post('/message/new',function(req,res){
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.write(JSON.stringify({ status: "OK" }));
    res.end();
    mailOptions['subject'] = 'new portfolio message from ' + req.body.name ;
    mailOptions['text'] = req.body.message + " \n      " + " phone number or email : " + req.body.phone_or_email;

    transporter.sendMail(mailOptions,(error,info)=>{
        error?console.log('error',error):console.log('success');
    });

})

I've abstracted away some of my other code in app.js that I think is irrelevant to this question. Can anyone tell me how to get this to work with heroku?

You have to Enable less secure apps from google.

I had same issue, but I solved this by going to the following url (while connected to google with the account I want to send mail from):

https://www.google.com/settings/security/lesssecureapps

There I enabled less secure apps.

And your are done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM