2

I am trying to use nodemailer to send email using a gmail account. I have looked at many places and seem to have the syntax correct, but no matter what example I use I keep getting a type error. So here is the code I'm using:

    var nodemailer = require('nodemailer');

    var transporter = nodemailer.createTransport("SMTP",{
        service: 'gmail',
        auth: {
        user: '[email protected]',
        pass: 'pass'
    }
    }, {
    // default values for sendMail method
     from: '[email protected]',
     headers: {
      'My-Awesome-Header': '123'
    }
    });
    transporter.sendMail({
        to: '[email protected]',
        subject: 'hello',
        text: 'hello world!'
    });

This ends up giving me the following error:

    C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:241
    this.mailcomposer.setMessageOption(mailOptions);
                  ^

    TypeError: this.mailcomposer.setMessageOption is not a function
        at Nodemailer.setGeneralOptions (C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:241:23)
        at Nodemailer.generateMailObject (C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:196:10)
        at Nodemailer.sendMail (C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:184:10)
        at C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:69:20
        at Nodemailer.validateSettings (C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:173:5)
        at sendMail (C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:63:12)
        at Transport.transport.sendMail (C:\Program Files\nodejs\node_modules\nodemailer\lib\nodemailer.js:40:9)
        at Object.<anonymous> (C:\Program Files\nodejs\hooks_examples\in testing\test2.js:17:13)
        at Module._compile (module.js:435:26)
        at Object.Module._extensions..js (module.js:442:10)

Any ideas would be appreciated... I'm new to node.js but not programming.

3 Answers 3

2

For Custom SMTP Try This...

var transporter = nodemailer.createTransport({
                                    host: 'mail.server.in',
                                    port: 587,
                                    auth: {
                                        user: '[email protected]',
                                        pass: 'password'
                                    },            
                                    authMethod:'NTLM',
                                    secure:false,
                                    tls: {rejectUnauthorized: false},
                                    debug:true
                                });

Node.js can not verify the certificate of your SMTP server. You can skip certificate verification by using rejectUnauthorized: false option

Unable to send email from custom smtp server

1

Have you read the official documentation here? It states:

var nodemailer = require('nodemailer');

// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: '[email protected]',
        pass: 'userpass'
    }
});

// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails

You don't need to specify the "SMTP" part at:

var transporter = nodemailer.createTransport("SMTP",{
        service: 'gmail',
        auth: {
        user: '[email protected]',
        pass: 'pass'
    }

as it is the default setting for nodemailer. Also, read the README of the SMTP Transport for options.

3
  • 1
    I have tried removing the SMTP as you show and it gives me an error saying: [Error: No transport method defined] I have also adding the debugging code from the read me with no feedback. Is there someplace I can look for some logs or further debugging?
    – dsxbertz
    Nov 19, 2015 at 18:39
  • Strange, I copied your code and it did not throw any error, in fact I tested it with my gmail account and it sent the email. Maybe a bug on Windows? I'm using Ubuntu btw.
    – Juan Stiza
    Nov 19, 2015 at 19:09
  • that could be a window related I am running Windows 7 with the latest patches etc. Here are the versions of node and npm that I'm running: 'Node v 4.2.2 and NPM 2.14.7. It almost seems like it is something the nodemailer is node interpreting correctly or getting an argument its expecting... I'm lost, I'll try on an ubuntu machine that I have and see if the results are any better.
    – dsxbertz
    Nov 19, 2015 at 23:30
0

I seem to have stumbled on to a fix by trying a few more things. I installed nodejs on a new machine and then loaded the js files to a different directory and the test email worked with out a problem. What was different in the first install was the test files I loaded into the C:\Program Files\nodejs\test directory. I guess nodejs didn't like that. When I move the same file to C:\test\test.js the problem goes away. Lesson learned don't place files in the nodejs dir, run them from another location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.