5

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

babbaggeii
  • 7,577
  • 20
  • 64
  • 118

2 Answers2

5

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

wachme
  • 2,327
  • 20
  • 18
  • 1
    Ah, ok. So I'll put it in the body of the message text instead. Thanks for that. – babbaggeii Jun 05 '13 at 15:56
  • 1
    Only thing you can modify is name of the sender, so `Joe Doe ` may be `My mailing service ` – wachme Jun 05 '13 at 16:05
  • Thanks @wachme! I'm pretty new to using email services. Do you have a short list of email providers who do allow this customization? Would Amazon SES be able to do this? And can you share the original docs or other resources that specify gmail doesn't do this? Thanks! – Adam Aug 26 '17 at 00:20
0

you can use postmark, they provide an excellent api for sending emails and there is a node module for it (https://npmjs.org/package/postmark)