2017-09-13 247 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上的密鑰存儲區。

相關問題