Nodemailer send email without smtp transport
Asked Answered
B

7

23

I am trying to send emails via nodemailer without SMTP transport. So i've done that:

var mail = require("nodemailer").mail;

mail({
    from: "Fred Foo ✔ <[email protected]>", // sender address
    to: "******@gmail.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
});

But when I run I get that :

> node sendmail.js
Queued message #1 from [email protected], to [email protected]
Retrieved message #1 from the queue, reolving gmail.com
gmail.com resolved to gmail-smtp-in.l.google.com for #1
Connecting to gmail-smtp-in.l.google.com:25 for message #1
Failed processing message #1
Message #1 requeued for 15 minutes
Closing connection to the server

Error: read ECONNRESET
    at errnoException (net.js:901:11)
    at TCP.onread (net.js:556:19)

I am on windows 7 32.

EDIT This seems to be a windows related bug for it worked on linux

EDIT #2

On the git shell, if I enter telnet smtp.gmail 587 it is blocked here:

220 mx.google.com ESMTP f7...y.24 -gsmtp
Bicuspid answered 11/4, 2014 at 15:3 Comment(4)
Make sure you have allowed the connection through ports which connects to gmail smtp, first test telnet smtp.gmail.com 587Royster
i am on windows so the telnet command doesnt work.Bicuspid
Aw the Windows users :D, well you can enable it social.technet.microsoft.com/wiki/contents/articles/…. Or use some other client like Putty.Royster
nobody has an anwser? I don't want to waste 100 reputation...Bicuspid
R
17

From your example output it seems to connecting to wrong port 25, gmail smtp ports which are opened are 465 for SSL and the other 587 TLS.

Nodemailer detects the correct configuration based on the email domain, in your example you have not set the transporter object so it uses the default port 25 configured. To change the port specify in options the type.

Here's the small example that should work with gmail:

var nodemailer = require('nodemailer');

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
        service: 'Gmail',
        auth: {
            user: "[email protected]",
            pass: "Nodemailer123"
        }
    });

console.log('SMTP Configured');

// Message object
var message = {

    // sender info
    from: 'Sender Name <[email protected]>',

    // Comma separated list of recipients
    to: '"Receiver Name" <[email protected]>',

    // Subject of the message
    subject: 'Nodemailer is unicode friendly ✔', 

    // plaintext body
    text: 'Hello to myself!',

    // HTML body
    html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
         '<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'
};

console.log('Sending Mail');
transport.sendMail(message, function(error){
  if(error){
      console.log('Error occured');
      console.log(error.message);
      return;
  }
  console.log('Message sent successfully!');

  // if you don't want to use this transport object anymore, uncomment following line
  //transport.close(); // close the connection pool
});
Royster answered 19/4, 2014 at 18:1 Comment(4)
thank you it worked... But I don't want to login. And If I change it from SMTP to direct it tells me that it was successfuly sended but I do not receive anything...Bicuspid
What you mean by you don't want to login ? The gmail SMTP server requires it, if you want to send email without authentication you could setup your own SMTP server.Royster
I wanted to use direct transport so i could set a fake address such as [email protected]Bicuspid
above transport object created support in v0.7.1. In latest v2.7.0 to create transport object use var transporter = nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');Stigmasterol
S
12

Looks like the the current nodemailer does not have the option of sending mail without smtp any more. I would recommend take a look at the sendmail library which does NOT need any smtp/auth to send email. It gives you similar experience to using sendmail in linux.

const sendmail = require('sendmail')();

sendmail({
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Hello World',
  html: 'Hooray NodeJS!!!'
}, function (err, reply) {
  console.log(err && err.stack)
  console.dir(reply)
})

You can also turn on the silent to make it less verbose: const sendmail = require('sendmail')({silent: true});. One thing to notice is the sender cannot be those domains that has DMARC policy like [email protected].

Sayles answered 25/9, 2017 at 23:3 Comment(6)
how do I force sendmail to send directly to the host in the address? e.g. I was using direct because otherwise I get the error "queryMx ENODATA" but now with sendmail I am seeing this error again.Chefoo
@Chefoo not sure about that, you might want to do a dig or nslookup for your mx server first.Sayles
ah ok I see the problem. but argh! the "err" argument isn't something that I can easily parse to determine if the error is 4xx - I have to retry these myself, don't I?Chefoo
@Chefoo Perhaps there is a errro code on the err object. I do not quite remember the detail. debug or do a console.log on err will give you the full picture of that object. :)Sayles
I struggled for 2 days with nodemailer and emailjs before finding this! Note that gmail will put these mails in your spam folder because they can't verify the sender, but at least it works with all the increasing security checks.Carminacarminative
sendmail package is using nodemailer mailcomposer for attachment which is deprecated.Disavowal
V
11

use nodemailer 0.7.1.
if you previously installed nodemailer then remove it by

npm remove nodemailer

now install it by

npm install [email protected]

the following code send mail without login, but it will work only in production environment, it won't work locally

var mail = require("nodemailer").mail;

mail({
    from: "Fred Foo ✔ <[email protected]>", // sender address
    to: "[email protected], [email protected]", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
});
Veronaveronese answered 5/5, 2016 at 16:49 Comment(3)
If you are really not interesting in saving the email credentials on code, then nodemailer 0.7.1 with the above code snippet works well. Thanks!Aintab
can this be done with nodemailer 2.7.2? If not, is it possible for both versions to co-exist for other modules in the same project that need it?Chefoo
Reverting to v0.7.1 works, but the email message is not encrypted. How do I encrypt the message?Carlenacarlene
P
5

probably it is windows firewall or antivirus preventing outgoing access. try to enable nodemailer debug messages.

Enabling debug

var nodemailer = require("nodemailer"),
  transport = nodemailer.createTransport('direct', {
    debug: true, //this!!!
  });

transport.sendMail({
    from: "Fred Foo ✔ <[email protected]>", // sender address
    to: "******@gmail.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}, console.error);
Prepotent answered 11/4, 2014 at 15:10 Comment(1)
I disabled windows firewall and it changed nothing. How do you enable debug ?Bicuspid
P
3

This appears to be possible in nodemailer. Not sure if this wasn't around when the other answers were provided. As documented here: https://nodemailer.com/transports/sendmail/ you can you use the built in sendmail if available to do your sending. I've tested this for my own use and works fine. There are obviously advantages of SMTP but to say it's not possible is not true.

Poultice answered 8/8, 2018 at 19:43 Comment(0)
B
0

You need to have a SMTP server, an email server which forwards the emails on your behalf. So either set up your own SMTP server like Haraka or provide credentials to Nodemailer to connect to one e.g. Gmail,MSN,Yahoo. Even I have started learning NodeJs and was trying to include an emailing feature in my project and this was the same problem I faced.

Barrier answered 9/2, 2015 at 11:19 Comment(3)
I wanted to use Direct for this reason. BTW there is no need to have a proper email to send mails. That's why you can send emails to someone with his email even though you don't know its credentialsBicuspid
But then wouldn't the receiving service (SMTP server) simply reject your mail or spam it. It expects to get emails from a authentic source.Barrier
Emails with no reply doesn't existsBicuspid
C
0

when you on your setting , it allows to send mails. https://www.google.com/settings/security/lesssecureapps this one worked for me

Cropeared answered 3/6, 2015 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.