简体   繁体   English

如何通过mailgun-js发送电子邮件并表达

[英]How to send email via mailgun-js and express

My form. 我的表格

form(action="/contact" method="POST")
          h3 Contact Form
          .form-group
            label.sr-only(for='name') Name
            input#name.form-control(placeholder='Name', type='text' name='name')
          .form-group
            label.sr-only(for='email') Email
            input#email.form-control(placeholder='Email', type='email' name='email')
          .form-group
            label.sr-only(for='phone') Phone
            input#phone.form-control(placeholder='Phone', type='text' name='phone')
          .form-group
            label.sr-only(for='message') Message
            textarea#message.form-control(name='message', cols='30', rows='5', placeholder='Message')
          .form-group
            input.btn.btn-primary.btn-lg(value='Send Message', type='submit')

My express route. 我的快递路线。

var express = require('express');
var mailRouter = express.Router();
var Mailgun = require('mailgun-js');

var api_key = '-------------------------------';
var domain = '------------------------------------------------';

var router = function(){

  mailRouter.post('/contact', function(req, res){
    console.log(req.body);

    var from_who = req.body.email;

    var mailgun = new Mailgun({apiKey: api_key, domain: domain});

    var data = {
      from: from_who,
      to: 'wewe99@yopmail.com',
      subject: 'Inquiry',
      text: req.body.message
    };

    mailgun.messages().send(data, function(err, body){
      if(err){
        res.render('error', {error: err});
        console.log("got an error:", err);
      } else{
        res.render('submitted', {email: req.body.email});
        console.log(body);
      };
    });
  });
};


module.exports = router;

I am using the sandbox domain of mailgun. 我正在使用mailgun的沙箱域。 I also authorized the email where I should sent the message. 我还授权了我应在其中发送邮件的电子邮件。 There is no error showing on the console and also no logs on req.body. 控制台上没有显示错误,req.body也没有日志。 The browser is just infinitely loading. 浏览器只是无限加载。 Anyone who can help? 谁能帮忙?

EDIT: After sometime, browser finished loading and here what it says http://prntscr.com/cixg3w 编辑:一段时间后,浏览器完成加载,这里显示的内容为http://prntscr.com/cixg3w

var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

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

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