简体   繁体   中英

Sending gmail mail with Nodemailer

I am trying to set up email forwarder for gmail with Nodemailer, but I want reliable and simple authentication process. Idea is to user simply provides email and pass without need to mess with allowing less secure apps option or using OAuth2.

I guess solution would be to make google see my app as secure one, but how do I achieve it?

I have saw similar services asking user to "Send email on your behalf", how can I apply that?

Am I using wrong technology to achieve this?

Any advice would be welcome.

Thank you!

You need to generate App Password in Google ( Sign in with App Passwords )

Then create a transporter using following configuration

const transporter = nodemailer.createTransport({
    "host": "gsmtp.gmail.com",
    "port": 587,
    "service": "gmail",
    "secure": false,
    "auth": { 
        "user": "<email>",  // your gmail address
        "pass": "********"  // app password
    },
    "debug": false,
    "logger": true,
    "tls": {
        "rejectUnauthorized": false
    }
});

Then use that transporter to send mails from nodemailer:

transporter.sendMail({
    to:         'target@example.com',
    from:       `Source <source@email.com>`,
    subject:    `Testing Nodemailer with Gmail`,
    text:       `Hi there! This email is being sent from a Node.JS app using gmail`
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM