1

我想在我的應用程序中發送通知電子郵件,我正在嘗試使用sendgrid。我的應用程序是在CoffeeScript中編寫的。Sendgrid替換標籤

enter code here 
from_address = '[email protected]' 
subject = 'This Is The Subject' 
html_body = '<table style="font-family: verdana, tahoma, sans-serif; color: #000;"> 
      <tr> <td> 
      <h2>Hello,</h2> <p>Hello!!</p> 
          <p>%%name%% %%surname%% send you a message</p> 
      </table>' 
sendgrid = require('sendgrid')(api_key) 
Email = sendgrid.Email 
email = new Email(
    to: to_address 
    from: from_address 
    subject: subject 
    text: text_body 
    html: html_body) 

recipients = [ 
    '[email protected]' 
    '[email protected]' 
] 
i = 0 
while i < recipients.length 
    email.addTo recipients[i] 
    i++ 

substitutions = { 
    "%%name%%": [ 
    "name1", 
    "name2" 
    ], 
    "%%surname%%": [ 
    "surname1", 
    "surname2" 
    ] 
} 

for tag in substitutions 
    email.addSubstitution(tag, substitutions[tag]) 

email.setFilters({ 
    "templates": { 
    "settings": { 
     "enable": "1", 
     "template_id": "XXXXXX-XXX-XXXX-XXXX-XXXXXXXXXX" 
    } 
} 
}) 


sendgrid.send email, (err, json) -> 
    if err 
    return console.log(err) 
    console.log json 
return 

當我執行代碼時,將我的電子郵件發送到電子郵件地址。但消息是:

你好!

%% name %% %% surname %%給你發消息。

替換不起作用。我嘗試更改%%的%, - 和#。但是,這些似乎工作。另外我嘗試使用setSections。

更新

這是sendgrid對象我發送。

{ to: [ '[email protected]', '[email protected]' ], 
    from: '[email protected]', 
    smtpapi: 
    { version: '1.2.0', 
    header: 
     { to: [], 
     sub: {}, 
     unique_args: {}, 
     category: [Object], 
     section: {}, 
     filters: [Object], 
     send_at: '', 
     send_each_at: [], 
     asm_group_id: {}, 
     ip_pool: '' } }, 
    subject: 'This Is The Subject', 
    text: 'Hello!\n\nThis is a test message from SendGrid. We have sent this to you because you requested a test message be sent from your account.\n\n This is a link to google.com: http://www.google.com\n This is a link to apple.com: http://www.apple.com\n This is a link to sendgrid.com: http://www.sendgrid.com\n\n Thank you for reading this test message.\n\nLove,\nYour friends at SendGrid', 
    html: '<table style="font-family: verdana, tahoma, sans-serif; color: #000;"> <tr> <td> <h2>Hello,</h2> <p>Hello!!</p> <p>%%name%% %%surname%% send you a message</p> </table>', 
    bcc: [], 
    cc: [], 
    replyto: '', 
    date: '', 
    headers: {}, 
    toname: undefined, 
    fromname: undefined, 
    files: [] } 

我在做什麼錯了?

預先感謝您。

+0

你能打印出什麼sendgrid對象最終看起來像?好像你沒有正確地在'x-smtpapi'頭中添加替換,但看到完整的東西會有幫助 – jacobmovingfwd

+0

嗨,@jacobmovingfwd在使用sendgrid.send方法之前,我添加了sendgrid對象。感謝您的幫助。 – jasc

+0

沒有人知道哪個可能是問題? – jasc

回答

0

我回答我自己的問題,因爲已經發現了問題。

該問題與Sendgrid無關,但與coffeeScript相關。在我的一個聲明中,我使用這個:

for tag in substitutions 
    email.addSubstitution(tag, substitutions[tag]) 

我嘗試在這裏添加替換標記的電子郵件對象。但是調試我的代碼我發現循環沒有遍歷替換變量。所以任何標籤都被添加到電子郵件對象中。

我已經改變了以前的代碼爲這一個:

Object.keys(substitutions).forEach((tag)-> 
    email.addSubstitution(tag, sub[tag]) 
    return 

而且隨着這一變化的替代標籤添加到電子郵件對象。