2015-12-07 42 views
2

我正在開發一個Node.js應用程序,它將Sendgrid事務電子郵件模板包含在用戶邀請函中。具體來說,我在Ubuntu w /官方sendgrid v2 npm上使用了nodejs v0.10.25。我的功能,以實際發送的消息如下:發送消息時,Sendgrid SMTP API不會替換替換標記

function sendEmail(payload, options, cb){ 
    //for debugging purposes 
    console.log(options); 

    var email = new sendgrid.Email({ 
      subject: payload.subject, 
      html: payload.html, 
      from: global.config.sendgrid.mailFrom, 
      fromname: global.config.sendgrid.mailName, 
      replyto: options.replyto ? options.replyto : '[email protected]' + global.config.sendgrid.mailhost 
    }); 

    email.setSmtpapiTos(options.addressees); 

    if(options.filters) 
      email.setFilters(options.filters); 

    if(options.subs) 
      email.setSubstitutions(options.subs); 

    if(options.category) 
      email.setCategories(options.category); 

    if(options.unique_args) 
      email.setUniqueArgs(options.unique_args); 

    sendgrid.send(email, function(err, json){ 
      return cb(err, json); 
    }); 
} 

選項對象,其中包括替代,看起來是這樣的:

{ category: 'Support Invite', 
    addressees: [ %EMAIL1%, %EMAIL2% ], 
    sub: 
    { '-name-': [ 'Shawn 1', 'Shawn 2' ], 
    '-id-': [ 1, 2 ], 
    '-pic-': 
     [ '<img height="100" src="%PICTURE URL%?sz=50" style="width: 100px; height: 100px;" width="100" />', 
     '<img height="100" src="%PICTURE URL%?sz=50" style="width: 100px; height: 100px;" width="100" />' ] }, 
    filters: 
    { 
    templates: 
     { 
     settings: 
     { 'enable': 1, 
     'template_id': global.config.sendgrid.transactions.supportInvite} } }, 
    unique_args: 
    { 
    sender: '104294048950213400380' } } 

最後,這裏是從該測試模板中的HTML:

<html> 
 
<head> 
 
\t <title></title> 
 
</head> 
 
<body> 
 
<div style="text-align: center;"><span>-pic-</span></div> 
 

 
<div><span style="font-family:tahoma,geneva,sans-serif;">Hello -name-,</span></div> 
 

 
<div>&nbsp;</div> 
 

 
<div><span style="font-family:tahoma,geneva,sans-serif;">&lt;%body%&gt;</span></div> 
 

 
<div>&nbsp;</div> 
 

 
<div>&nbsp;</div> 
 

 
<div>&nbsp;</div> 
 

 
<div><span style="font-family:tahoma,geneva,sans-serif;"><span style="font-size:9px;">Invitation ID:</span>&nbsp;-id-</span></div> 
 
</body> 
 
</html>

郵件以正確的模板,主題和發件人信息發送給相應的收件人。我還可以從我的SendGrid統計資料中驗證它們是否標有適當的類別和SMTP參數(「發件人」)。 但是替換沒有完成。例如,-name-標籤仍然存在於我的測試電子郵件框中的結果電子郵件中。

我已經嘗試過SendGrid的諮詢文檔,包括他們的SMTP API以及查詢他們的支持數據庫。我也搜索過StackExchange,但最相關的問題(this,thisthis)並不完全提供答案。特別有趣的是,每個人都使用不同的字符替代標記。不知道這純粹是由選擇還是依賴於語言。我嘗試了不同的字符( - ,:和%),但結果相同。

回答

1

我看到你的選項對象有一個屬性sub,而不是subs。 option.subs永遠是不確定的,因此在

假,如果(options.subs)

無論是嘗試改變,要

如果(options.sub)

或重命名選項屬性從子到子。

+1

是的,我在發佈這個問題一小時後才發現。我覺得自己像一個白癡,我的第一個StackExchange問​​題是一個骨頭錯字。不管怎麼說,還是要謝謝你。 –