8

I'm trying to send emails from my application using nodemailer.

My setup look like this:

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport ({
  host: 'smtp.companyname.dk',
  secureConnection: true,
  port: 587,
  auth: {
        user: 'support@companyname.dk',
        pass: '****'
  }
}));

var mailOptions = {
    from: 'Olaf <ob@companyname.dk>',
    to: 'john@test.dk',
    subject: 'This is a test ',
        text: 'Hello world ',
        html: '<b>Hello world </b>'
    };

transporter.sendMail(mailOptions, function(error, info){
    if(error){
       console.log(error);
    }else{
    console.log('Message sent: ' + info.response);
    }
});

After running this, it gives me the following error:

{ [Error: Hostname/IP doesn't match certificate's altnames: "Host: smtp.companyname.dk. is not in the cert's altnames: DNS:*.mailcloud.dk, DNS:mailcloud.dk"]
  reason: 'Host: smtp.companyname.dk. is not in the cert\'s altnames: DNS:*.mailcloud.dk, DNS:mailcloud.dk',
  host: 'smtp.companyname.dk.',
  cert: 
   { subject: { OU: 'Domain Control Validated', CN: '*.mailcloud.dk' },
     issuer: { O: 'AlphaSSL', CN: 'AlphaSSL CA - G2' },
     subjectaltname: 'DNS:*.mailcloud.dk, DNS:mailcloud.dk',
     infoAccess: { 'CA Issuers - URI': [Object], 'OCSP - URI': [Object] },
     modulus: 'AFFDE405E26382FC1F9A126B51C763A21DCB90228DF9B9BFF466B00DA17BCBDA48E0315E0A6E4E8E0C65B3E5A9BF9A98448DF8006869B913C8D644B78B2176C240AA703DF7C5109F7FC9EDBAE0A69E35E00A81D753990A7DF531CC1F47A778A1CC18271298DBB71E341E846139951D9B119C237BDD9AA6F7D16A62FC5D4D6BEA0288DAC700E5C87E472B75365C522B0342185F16854659EE48637B6124269C4E18CC040344928D115350805EF66738922230349DEC97E2D68D0B4175EA6B63BC161B841C150B7009AAC0E8A06CD34D3912926D98CEBF83F6C187AD08A5045F9868512EFDD478FAA81FA0402F5C44B3FD70F82BA4F102352A01B02B77F0FA096D',
     exponent: '10001',
     valid_from: 'Oct  3 08:22:02 2014 GMT',
     valid_to: 'Dec 21 11:07:29 2017 GMT',
     fingerprint: '35:3A:87:5C:91:8C:B2:7C:4E:87:D1:E3:26:84:57:E5:A1:73:62:F3',
     ext_key_usage: [ '1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2' ],
     serialNumber: '****',
     raw: <Buffer 30 82 04 a7 30 82 03 8f a0 03 02 01 02 02 12 11 21 75 e4 f8 11 73 a4 c5 05 36 5f fe 9b b3 0b 4f 86 30 0d 06 09 2a 86 48 86 f7 0d 01 01 05 05 00 30 2e ... > } }

I've been stuck with this for days now and looked everywhere, so I hope somebody can help me with this (or at least point at the right direction).

Backer
  • 1,094
  • 1
  • 20
  • 33

3 Answers3

14

Try adding:

    tls: {
        rejectUnauthorized: false
    },

To your nodemailer options object.

rjmacarthy
  • 2,164
  • 1
  • 13
  • 22
  • Just tried that and it gives me this error `{ [Error: Message failed] code: 'EMESSAGE', response: '550 5.7.1 Client does not have permissions to send as this sender', responseCode: 550 }` I don't know if it has anything to say, but right now I'm trying to send the email from my local machine!? – Backer Mar 17 '15 at 13:08
  • Keep in mind this option leaves you vulnerable to [MITM attacks on SSL](https://stackoverflow.com/a/16311147/4068278). – 3ch01c Aug 16 '17 at 06:40
  • This option worked for me. But is it a safe way? My port is 587. What are the impacts of setting this option to false ? – Abhilasha Aug 24 '17 at 07:43
3

After adding what @Richard Macarthy suggested, and looking up the new error I got back, I figured out that the from email in my mailOptions has to be the same as the email I'm using to send the emails from - which actually makes perfectly sense now.

In this case:

var mailOptions = {
    from: 'Olaf <support@companyname.dk>',
    to: 'john@test.dk',
    ...
Backer
  • 1,094
  • 1
  • 20
  • 33
3
var transporter = nodemailer.createTransport({<br>
host: 'hostSTMP',
secure: false, //disable SSL    
requireTLS: true, //Force TLS
    tls: {
        rejectUnauthorized: false
    },
    port: port, //Port of STMP service
    auth: {
      user: 'user@user.com',
      pass: 'password'
   }
});

rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An error event is emitted if verification fails; Default: true.

rejectUnauthorized: If false, you're saying "I don't care if I can't verify the server's identity."

Pablo D. A.
  • 31
  • 1
  • 2