2017-09-13 250 views
0

我有这个Mailer.js文件SendGrid不发送电子邮件(Node.js的)

const sendgrid = require('sendgrid'); 
    const helper = sendgrid.mail; 
    const keys = require('../config/keys'); 

    class Mailer extends helper.Mail { 
     constructor({ subject, recipients }, content) { 
      super(); 

      this.sgApi = sendgrid(keys.sendGridKey); 
      this.from_email = new helper.Email('[email protected]'); 
      this.subject = subject; 
      this.body = new helper.Content('text/html', content); 
      this.recipients = this.formatAddresses(recipients); 

      this.addContent(this.body); 
      this.addClickTracking(); 
      this.addRecipients(); 
     } 

     formatAddresses(recipients) { 
      return recipients.map(({ email }) => { 
       return new helper.Email(email); 
      }); 
     } 

     addClickTracking() { 
      const trackingSettings = new helper.TrackingSettings(); 
      const clickTracking = new helper.ClickTracking(true, true); 

      trackingSettings.setClickTracking(clickTracking); 
      th 

is.addTrackingSettings(trackingSettings); 
    } 

    addRecipients() { 
     const personalize = new helper.Personalization(); 

     this.recipients.forEach(recipient => { 
      personalize.addTo(recipient); 
     }); 
     this.addPersonalization(personalize); 
    } 

    async send() { 
     const request = this.sgApi.emptyRequest({ 
      method: 'POST', 
      path: '/v3/mail/send', 
      body: this.toJSON() 
     }); 

     const response = await this.sgApi.API(request); 
     return response; 
    } 
} 

module.exports = Mailer; 

还有,我有surveyRoutes.js文件,关于路线哪个容器信息

const mongoose = require('mongoose'); 
const requireLogin = require('../middlewares/requireLogin'); 
const requireCredits = require('../middlewares/requireCredits'); 
const Mailer = require('../services/Mailer'); 
const surveyTemplate = require('../services/emailTemplates/surveyTemplate'); 

const Survey = mongoose.model('surveys'); 

    module.exports = app => { 
     app.post('/api/surveys', requireLogin, requireCredits, async (req, res) => { 
      const {title, subject, body, recipients} = req.body; 

      const survey = new Survey({ 
       title, 
       subject, 
       body, 
       recipients: recipients.split(',').map(email => ({ email: email.trim() })), 
       _user: req.user.id, 
       dateSent: Date.now() 
      }); 

      const mailer = new Mailer(survey, surveyTemplate(survey)); 
      try { 
       await mailer.send(); 
      } 
      catch(e){ 
       console.log(e); 
      } 
      }); 
    }; 

而且,里面的主要文件,我用这个scructure使用surveyRoutes

require('./routes/surveyRoutes')(app); 

所以,当我试图发送电子邮件,与axios.post熊,没有错误,它似乎是okey,但电子邮件未交付。

如果您发现代码存在任何问题,请告诉我。

+0

可能是发送网格有问题。从sendgrip发送电子邮件时,我也面临同样的问题。 – sohamdodia

+0

@sohamdodia你是什么意思,“发送电网的问题”?请解释。 – Remzes

+0

可能你已经超过了发送网格的限制,或者你发送的发送网格邮件太多,所有邮件都会发送到垃圾邮件。所以你不能发送邮件。 – sohamdodia

回答

0

一个纯粹的轶事点,但是下面的解释似乎阻止了一些人成功地发送电子邮件从他们的Express服务器到SendGrid API。

许多人在注册SendGrid服务的免费层之后报告,由于他们没有在其帐户设置中输入可验证的公司名称和公司网站地址,因此SendGrid不会批准免费层访问,并且它表现出在他们的应用程序无法通过SendGrid处理电子邮件。一个人实际上声称直接使用SendGrid支持验证了这种情况。

另外,我假设您的SendGrid API密钥已准确复制到Express Server上的密钥存储区。

相关问题