Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending mail with nodemailer - email from field incorrect

Trying to set up a contact form with nodemailer. Here's what's in my 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" });
});

And my contact.jade template looks like this:

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

The email now works, but comes from myemailaddress rather than the one I enter into the email field on the template. Any ideas

like image 998
babbaggeii Avatar asked Jun 05 '13 15:06

babbaggeii


People also ask

How do we use nodemailer?

How do we use Nodemailer? 8. Nodemailer is a Node.js module that makes it simple to send emails from a server. Whether you want to interact with your users or simply inform yourself when something goes wrong, the mail is one of your alternatives.

What is the node mail project?

Whether you want to interact with your users or simply inform yourself when something goes wrong, the mail is one of your alternatives. The project began in 2010 when there was no logical way to send email messages, and it is now the default solution for most Node.js users.

Why does the attachment or message node return an error?

If an attachment or message node tries to fetch something from a file the sending returns an error. If this field is also set in the transport options, then the value in mail data is ignored

Does nodemailer abort the already opened stream?

**Memory leak warning!** When using readable streams as content and sending fails then Nodemailer does not abort the already opened but not yet finished stream, you need to do this yourself. Nodemailer only closes the streams it has opened itself (eg. file paths, URLs)


1 Answers

Gmail and many other email services don't allow you to send messages with various FROM field.

like image 168
wachme Avatar answered Oct 24 '22 21:10

wachme