yangli-io
yangli-io

Reputation: 17334

Nodejs send mails

Hi I've sent some mails using express mailer. My problem is, when I need to send mails it's asking for my credentials and using that email to send. However, I want to send emails from a no-reply email but this would mean sending emails from an account thats not set up.

I know in other application servers you can pretty much send emails from any email address, even one which is not your own. I'm wondering how I can do this with nodejs or expressjs.

Edit:

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: '[email protected]',
        pass: '****'
    }
});

var mailOptions = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Hello ✔',
    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);
    }
});

I've tried this but it's still sending from [email protected] which is the server I logged in with

Upvotes: 0

Views: 1276

Answers (2)

James Game
James Game

Reputation: 547

Google SMTP servers automatically set your sending address to the one you logged in with.

If you want to send e-mail with an address which isn't your own, you should use a different SMTP server or set up your own one (Haraka, smtp-server, etc.)

Upvotes: 1

Henry Tseng
Henry Tseng

Reputation: 3333

You should be able to set the "reply-to" or "from" to whatever you need to. It's independent of which email account you use to send the email. Like an address on a postage mail envelope.

Nodemailer is a bit more robust in my personal experience: https://www.npmjs.org/package/nodemailer

And for higher volume setting up a service like Amazon SES as your transport.

Upvotes: 0

Related Questions