简体   繁体   English

使用 nodemailer 发送邮件

[英]sending mail with nodemailer

whenever i run the code I'm getting the error " Error: Greeting never received at SMTPConnection._formatError...."每当我运行代码时,我都会收到错误“错误:在 SMTPConnection._formatError 处从未收到过问候......”

function automatedMailSender(req,res){

var mailOptions = {
    from: "abc <abc@test.com>", // sender address
    to: 'nit@test.com', // list of receivers
    subject: 'Hello ', // Subject line
    text: 'Hello world ?', // plain text body
    html: '<b>Hello world ?</b>' // html body
 };


var mailer=nodemailer.createTransport({
    host: 'mail.test.com',
    port: 465,
    secure: false, // secure:true for port 465, secure:false for port 587
    auth: {
        user: 'mymail@test.com',
        pass: '1234'
    }
});

mailer.sendMail(mailOptions, (error, response)=>{
    if(error){
        console.log('mail not sent \n',error);
    }
    else{
        console.log("Message sent: " ,response.message);
    }   
 });
}

I'm I doing something wrong in the code.我在代码中做错了什么。

The answer in right in your code答案就在您的代码中

port: 465,
secure: false, // secure:true for port 465, secure:false for port 587

The value for "secure" should be true for port 465对于端口 465,“secure”的值应为 true

This is due to your SMTP security for unauthorised user so for overcome this problem you need set rejectUnauthorized:false这是由于您对未经授权用户的 SMTP 安全,因此为了克服这个问题,您需要设置rejectUnauthorized:false

Hope this will helpyou.希望这会帮助你。

Try something like that, it work perfectly for me:尝试这样的事情,它对我来说非常适合:

//Create reusable transporter object using the default SMTP transport 
var transporter = nodemailer.createTransport(
'smtps://USERNAME%40gmail.com:PASSWORD@smtp.gmail.com');

// setup e-mail data 
var mailOptions = {
        from: '"Michel 👥" <recipe@example.com>', // sender address 
        to: 'bernard@example.com', // list of receivers 
        subject: 'Hello', // Subject line 
        text: 'Bonjour!' // plaintext 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);
});

If is still doesn't work you have a little tuto here如果仍然不起作用,你有一个小教程在这里

If you are using Gmail to send mails then try this.如果您使用 Gmail 发送邮件,请尝试此操作。

Allow less secure apps to connect to Gmail.允许安全性较低的应用连接到 Gmail。

var mailer=nodemailer.createTransport({
    host: 'mail.test.com',
    port: 465,
    secure: true, // secure:true for port 465, secure:false for port 587
    transportMethod: 'SMTP',
    auth: {
        user: 'mymail@test.com',
        pass: '1234'
    }
});

The value for secure should be true for port 465 and use transportMethod: 'SMTP'端口465 secure值应为 true 并使用transportMethod: 'SMTP'

tls:{
        rejectUnauthorized:false
    }

fixed it by using the above code in mailer variable.通过在 mailer 变量中使用上面的代码来修复它。 Thanks guys谢谢你们

const express = require('express');
const router = express.Router();
const { Email } = require('../models/email.model');
const { User } = require('../models/user.model');
const nodemailer = require('nodemailer');




//post email 
router.post('/send', async (req, res) => {

    if (req.query) {
        var query = {}
        if (req.query.lastname) { query.lastname = req.query.lastname }
        if (req.query.firstname) { query.firstname = req.query.firstname }
        if (req.query.email) { query.email = req.query.email }
        if (req.query.isactive) { query.isActive = req.query.isactive }
        if (req.query.isgain) { query.isGain = req.query.isgain }
    }

    const email = new Email({
        title: req.body.title,
        subject: req.body.subject,
        text: req.body.context,
        description: req.body.description
    });



    if (req.body.title && req.body.subject && req.body.context) {

        const user_db = await User.find(query)
        const user_db_email = user_db.map(x => x.email)

        var smtpTransport = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 465,
            secure: true,
            auth: { user: 'ghiles@gmail.com', pass: 'g' },
            tls: { rejectUnauthorized: false }, // do not fail on invalid certs 
        });

        var mailOptions = {
            to: user_db_email,
            from: 'fatb@gmail.com',
            subject: req.body.subject,
            text: req.body.context
        };

        email.save().then(() => {

            smtpTransport.sendMail(mailOptions)
                .then(() =>  res.status(200).json(email))
                .catch(() => res.status(500).json({ success: false, message: `erreur dans l'envoi de mail` }))

        }).catch(() => res.status(200).json({  success: false, message: `erreur dans le serveur` }))

    } else {
        res.status(400).json({ success: false, message: `Veuillez renseigner le titre et l'objet et le contexte du mail` })
    }





module.exports = router; 
host: 'smtp.host.com',
port: 587,     // 587 or 25 or 2525
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
    user: 'mymail@test.com',
    pass: '1234'
}

Try this.试试这个。 prefixing smtp and changing the port.为 smtp 添加前缀并更改端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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