1

I'm using nodemailer to try to send an email to myself via commandline:

var nodemailer = require('nodemailer');

// config
var smtpConfig = {
    host: 'smtp.myhost.com',
    port: 465,
    secure: false, // dont use SSL
    tls: {rejectUnauthorized: false} 
};

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpConfig);

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '"Fred Foo " <foo@blurdybloop.com>', // sender address
    to: 'person@gmail.com', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

When I try to run this code I get the following error:

Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/answer/14257

The link takes you to a page that tells you to register your app inside Google Console. But this is not what I'm trying to do.

There are loads of email clients that can send an email to a gmail inbox without having to sign into that email account. This is what I'm trying to do. I'm trying to turn my terminal into an smtp client that can send a mail message to any inbox. This shouldn't require extensive authentication. How do I do this?

NOTE

Just to provide some perspective, I'm trying to replicate in node whats possible with the unix sendmail command:

sendmail person@gmail.com < testemail.txt

How can I do this using nodemailer?

Community
  • 1
  • 1
dopatraman
  • 13,416
  • 29
  • 90
  • 154
  • 1
    sending to gmail is entirely different than using gmail to send an email. – Marc B Apr 22 '16 at 14:50
  • I know... I am trying to send _to_ gmail. – dopatraman Apr 22 '16 at 14:51
  • @Mark B. I think you summed up the issue in one brief thought. I didn't "see it" early enough me thinks. – granadaCoder Apr 22 '16 at 15:18
  • @MarcB Although this may be true, to someone who doesn't understand it sounds like a riddle. What is the difference? – dopatraman Apr 22 '16 at 15:21
  • You're asking why [insert SMTP server here] isn't an [Open Mail Relay](https://en.wikipedia.org/wiki/Open_mail_relay), and why Open Mail Relays no longer are common. That link should splain you. – James Apr 22 '16 at 19:01
  • @James thank you, that definitely shed some light on it. But why does the `sendmail` command work then? Also, how am I able to use gmail to send an email to any host? Does google have to authenticate with every 3rd party SMTP server? That seems ludicrous. – dopatraman Apr 22 '16 at 19:49
  • Password is required to send mail via gmail's smtp - [see this](http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server) - only 1 smtp server is needed to send an email to a recipient. – James Apr 22 '16 at 22:09
  • PHP's sendmail works because a smtp server (and probably username/password for it) are set in the php.ini file, and it makes use of those. – James Apr 22 '16 at 22:10
  • I'm talking about the unix sendmail command... not php... – dopatraman Apr 23 '16 at 14:28

1 Answers1

-1

Try port 587.

Here are my gmail settings.

smtpServerName="smtp.gmail.com" 
portNumber="587" 
authenticationMode="SSL" 
smtpUserName="somebody@gmail.com" 
smtpUserPassword="xxxxxxxxxx"

I would experiment with ssl: false and ssl: true

nodemailer.SMTP = {
    host: 'smtp.gmail.com',
    port: 587,
    ssl: false,
    use_authentication: true,
    user: 'my.username@gmail.com',
    pass: 'my.password'
}

Make sure you read this:

https://support.google.com/accounts/answer/6010255

It looks like there are two ways with gmail.

Port 587 is (non ssl) but with TLS.

Port 465 is SSL (but not TLS).

Go figure.

    //singleServer = new SmtpServerSettings("smtp.gmail.com",
    //      "myname@gmail.com", "587", AuthenticationType.Basic,
    //    "myname@gmail.com", "mypassword", "");

    //singleServer = new SmtpServerSettings("smtp.gmail.com",
    //      "myname@gmail.com", "465", AuthenticationType.SSL,
    //      "myname@gmail.com", "mypassword", "");
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • So here's the thrust of my question: I dont see why i need to provide a user name and password. I'm not signing into a service. I'm basically trying to wrap the `sendmail` command with a from and to address. Also, I get the same error even after switching to port 587. – dopatraman Apr 22 '16 at 14:52
  • (most times, especially with a "cloud" smtp server) :: You need to provide credentials to the smtp service. This is how they know you are "legit". If you didn't supply credentials, anybody/everybody would sent emails via the gmail smtp servers.....aka, spam-city. I'm not sure why you would think otherwise. – granadaCoder Apr 22 '16 at 14:54
  • How can they authenticate a service that does not belong to google? What if I am using a third party unidentified smtp service? How would they authenticate? – dopatraman Apr 22 '16 at 14:55
  • "Trying to send to gmail". What is your smtp server to plan on using? That's your fundamental question. Each smtp server has its own setup information. – granadaCoder Apr 22 '16 at 14:55
  • I am trying to write one from scratch. But for the time being I'm using a simple client to send a mail message. `sendmail` is able to do this without authentication. Why cant I replicate that in node? – dopatraman Apr 22 '16 at 14:56
  • "I am trying to write one from scratch" You don't "write one from scratch". You need to use one available to you. Like, if you are at home, and you use time warner cable, then your smtp server might be "mail.twc.com". Or you can use one from the clould (like gmail). I think you need to review the basics of smtp https://www.google.com/search?btnG=1&pws=0&q=how+does+smtp+work&gws_rd=ssl – granadaCoder Apr 22 '16 at 15:06
  • Why is it mandatory to use an available one? This defeats the point of what I'm trying to do, which is send an email from a service-agnostic client. True, that my mail server could be mail.gmail.com, but it could also be mail.whateveriwant.com. I'm trying to construct the latter. Why? For fun mostly. The only way to gain an understanding of technology is to use it on a low level. Please help me understand if you can. – dopatraman Apr 22 '16 at 15:14
  • No matter what client you have, you're going to have to have configuration on that client to know how to talk to "mail.whateveriwant.com". I don't think I can help you beyond that. You want something to exist that does not exist. – granadaCoder Apr 22 '16 at 15:17
  • What do you mean by "configuration on that client"? What does that actually mean in terms of authentication? Suppose I have an MX record on my domain that I want to link to an smtp server of my own. Eventually that smtp server will send an email to a gmail user's inbox. I can do this from commandline using `sendmail`. How do I replicate that on my server? – dopatraman Apr 22 '16 at 15:20
  • Who downvoted my answer? Geeze Louise. I'm off this question. No good deed goes unpunished. – granadaCoder Apr 22 '16 at 16:53