无法使用节点邮件程序发送电子邮件

时间:2017-05-30 19:30:27

标签: javascript node.js forms http nodemailer

我是使用node.js的新手。我创建了一个表单,访问者可以通过该表单将电子邮件地址发送到我已设置为接收电子邮件的地址。电子邮件使用正确的邮件内容和主题行发送,但它显示我自己的电子邮件帐户作为发件人和收件人。如果您看到一个简单的解决方案,我会忽略,请告诉我我的错误在哪里。



var http = require('http');
var express = require('express');
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');

var urlencodedParser = bodyParser.urlencoded({extended: true});
var app = express();

var smtpTransport = nodemailer.createTransport({
    service: "gmail",
    host: "smtp.gmail.com",
    auth: {
        user: "ommitted",
        pass: "ommitted"
    }
});

app.use(express.static('public'));
app.use(bodyParser.json());

app.post('/', urlencodedParser,function(req, res){
    console.log(req.body);
    var mailOptions={
        to: req.body.to,
        from: req.body.from,
        subject: req.body.subject,
        text: req.body.message,
    }
    console.log(mailOptions);
    smtpTransport.sendMail(mailOptions, function(error, response){
        if(error){
            console.log(error);
            res.end("error");
        }else{
            console.log("Message sent");
            res.redirect('http://www.google.com');
        }
    });
});
var server = http.createServer(app).listen(8081, function() {
    console.log("Express started on port 8081");
});

<div class="col-md-offset-1 col-md-10 col-sm-12">
            <form action="/" method="POST" class="wow fadeInUp" data-wow-delay="0.6s">
                <div class="hidden">
                    <input type="text" class="form-control" name="to" id="to" value="csm@syntriagesolutions.com">
                </div>
                <div class="col-md-4 col-sm-6">
                    <input type="text" class="form-control" placeholder="Name" name="name" id="name">
                </div>
                <div class="col-md-4 col-sm-6">
                    <input type="email" class="form-control" placeholder="Email" name="from" id="from">
                </div>
                <div class="col-md-4 col-sm-12">
                    <input type="text" class="form-control" placeholder="Subject" name="subject" id="subject">
                </div>
                <div class="col-md-12 col-sm-12">
                    <textarea class="form-control" placeholder="Message" rows="7" name="message" id="message"></textarea>
                </div>
                <div class="col-md-offset-2 col-md-8 col-sm-offset-2 col-sm-8">
                    <input type="submit" class="form-control" value="Shoot Message" id="send_email">
                </div>
            </form>
        </div>
&#13;
&#13;
&#13;

0 个答案:

没有答案