1

私のアプリケーションで通知メールを送信したいのですが、sendgridを使用しようとしています。私のアプリケーションはCoffeeScriptで書かれています。Sendgrid Substitutionsタグ

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

こんにちは、@gacobmovingfwd sendgrid.sendメソッドを使う直前にsendgridオブジェクトを追加しました。ご協力ありがとうございました。 – jasc

+0

誰が問題なのか誰も知りませんか? – jasc

答えて

0

私はすでに問題を見つけたので私自身の質問に答えます。

問題はSendgridとは関係ありませんが、coffeeScriptとは関係ありません。私の声明の1つでは、以下を使用します:

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

私は電子メールオブジェクトに置換タグを追加しようとしました。しかし、私のコードをデバッグすると、ループが置換変数を反復していないことがわかりました。したがって、任意のタグが電子メールオブジェクトに追加されました。

私はこの1つの前のコードを変更しました:

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

そして、この変更に置換タグが電子メールオブジェクトに追加されました。