Search code examples
javascriptnode.jsherokugmailnodemailer

send body form to gmail via node.js


I'm trying few days to composing mail sending with node.js to gmail with no success :-(

at the beginning of tries I try to send form submittion to my gmail but first I need to understand how do I sent simple mail from body to gmail, perhaps I wrong with syntax or missing something?

actually I uses "heroku" as storage and domain for my site

I tried several plugins (such as mailgun, send grid and more) but the process was too complicated integrate their API's into my site.

actually, I find this article in stack overflow that describe how to send the via nodemailer in relation to my error - URL: node.js nodemailer gmail error

when i copy the answer1 to my code i still receiving error.

I' also pass trough all gmail setup how to turn off security block for less secured apps but i'm still receiving error. all the requires installed over my npm npm install --save

** code **

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

function handleSayHello(req, res) {

var transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
  user: '[email protected]', // my mail
  pass: 'myPassword'
}
}));

var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Email Example' // Subject line
//text: text //, // plaintext body
//html: '<b>Hello world ?</b>' // You can choose to send an HTML body                       instead
};

transport.sendMail(mailOptions, function (error, info) {
if (error) {
  console.log(error);
  res.json({
    yo: 'error',
    err: error
  });
} else {
  console.log('Message sent: ' + info.response);
  res.json({
    yo: info.response,
    info: info
  });
};
});
}

router.get('/', function (req, res, next) {
  handleSayHello(req, res);
});

then i receive this error:
{
"yo": "error",
"err": {
    "code": "EAUTH",
    "response": "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtx\n534-5.7.14 e8jnXXDaDg-dpFfc3H5ljeaBdmnxbXBDXMh-aXS-mmV4gQsXMSZLiiJHDXuOr3wy-IR2Zp\n534-5.7.14 xnznWLf5y5e3xTbJNlZBNcxBMajM9-SFQGy1MQM2XZRpHgtywuDtEj5iPiP0b2T6Wjbsxg\n534-5.7.14 hgehfzufG6h13qhjQK5IgRPNQsSICRQBtRCl3E1J62wFo8bnvZv4peY5aK55JMpwhSavJb\n534-5.7.14 ho-b9ExGGsXFmw_Er6lc8m3vCmO_Q> Please log in via your web browser and\n534-5.7.14 then try again.\n534-5.7.14  Learn more at\n534 5.7.14  https://support.google.com/mail/answer/78754 t35sm887733qtc.40 - gsmtp",
    "responseCode": 534,
    "command": "AUTH PLAIN"
}

}

if someone can take my code and fix it (if needed) and explain simply what do i need to modify in my google account I'll be great-full so much!


Solution

  • to allow from google/gmail, you should follow these links and allow to less secure apps access: unlock account

    and Less secure apps

    Let me know if still have issue, I'll check/debug your code.