使用nodemailer发送邮件 - 来自字段的电子邮件不正确

时间:2013-06-05 15:30:24

标签: node.js express pug nodemailer

尝试与nodemailer建立联系表单。这是我的app.js中的内容:

// EMail configuration
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "myemailaddress",
        pass: "xxx"
    }
});

// CONTACT FORM
app.get('/contact', function (req, res) {
    res.render("contact");
});

app.post('/contact', function (req, res) {
    var mailOptions = {
        from: req.body.email, // sender address
        to: "myemailaddress", // list of receivers
        subject: req.body.subject, // Subject line
        text: req.body.message, // plaintext body
    }
    smtpTransport.sendMail(mailOptions, function(error, response){
        if(error){
            console.log(error);
        }else{
            console.log("Message sent: " + response.message);
        }
        smtpTransport.close(); // shut down the connection pool, no more messages
    });
    res.render("contact", { success: "building web app" });
});

我的contact.jade模板如下所示:

form#contact-form(action="/contact", method="POST")
div.span5
    p Full name:
        input#name(type="text", name="name")
    p Email address:
        input#email(type="email", name="email")
    p Subject:
        input#subject(type="text", name="subject")
    p Message:
        textarea#message(type="text", name="message", rows="5")
    p: button(type="submit") Send message

电子邮件现在有效,但来自myemailaddress而不是我在模板上输入电子邮件字段的电子邮件。任何想法

2 个答案:

答案 0 :(得分:4)

Gmail和许多其他电子邮件服务不允许您发送包含各种FROM字段的邮件。

答案 1 :(得分:0)

您可以使用邮戳,它们为发送电子邮件提供了出色的API,并且有一个节点模块(https://npmjs.org/package/postmark