Sendgrid-nodejs: 眮換が機胜しない

䜜成日 2018幎04月10日  Â·  43コメント  Â·  ゜ヌス: sendgrid/sendgrid-nodejs

問題の抂芁

倉曎を加えずに、トランザクション電子メヌルで䟋に埓いたした。 眮換タグは削陀されたすが、テキストは挿入されたせん。 これが私が受け取る電子メヌルのスクリヌンショットです

image

再珟する手順

  1. クロヌンhttps://github.com/panayi/test-sg/
  2. 䟋からテンプレヌト本䜓をコピヌし、SendGridUIでトランザクションテンプレヌトを䜜成したす。
  3. test.jsを線集しお、APIキヌ、メヌルアドレス、および䞊蚘で䜜成したtemplateIdを挿入したす。
  4. node test.js実行したす。

技術的な詳现

  • sendgrid-nodejsバヌゞョン@ sendgrid / [email protected]
  • Node.jsバヌゞョン8.6.0
unknown or a help wanted question

最も参考になるコメント

こんにちは、 @ himanshupntのコメントを読んだ埌、レガシヌテンプレヌトでテストしたずころ、機胜したした。 ありがずう@himanshupnt 。

埌で、 Sendgridのドキュメントである動的トランザクションテンプレヌトを䜿甚したメヌル送信を芋぀けたした。新しいテンプレヌトレガシヌテンプレヌトではないが機胜しおいるようです。 同じHTMLテンプレヌトを䜿甚し、コヌドを次のように曎新したした。

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "[email protected]",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "[email protected]"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

「substitutions」を「dynamic_template_data」に眮き換え、ドキュメントの芁求に応じお「personalizations」内に「to」を配眮したこずに泚意しおください。 たた、サブゞェクトを「dynamic_template_data」の1぀ずしお扱い、機胜したす。 テンプレヌトの件名フィヌルドに{{subject}}を入力しおください

ありがずう。

党おのコメント43件

こんにちは@panayi 、

チェックするいく぀かの事柄

  1. テンプレヌトIDが正しいこずを確認したす
  2. テンプレヌトがUIでアクティブ

HTMLがどのように芋えるかも共有できたすか

ありがずう

敬具、

゚ルマヌ

こんにちは、 @ thinkingserious 、 @ panayiが述べたのず同じ問題を再珟するこずができたした。
私のHTMLコヌドは次のようになりたす

こんにちはfirst_name埓業員employee_id
いく぀かのHTMLコヌド

コヌド

sgMail.setApiKey('apikey');
sgMail.setSubstitutionWrappers('{{', '}}'); // Triied sgMail.setSubstitutionWrappers('-', '-'); as well`

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: 'Hello HTML world!',
  templateId: 'template-id',
  substitutions: {
    first_name: 'Tejas',
    employee_id: 'emp123',
  },
};
sgMail.send(msg);

泚 substitutionWrappers: ['{{', '}}']を䜿甚しおみたした
msg内ですが、運がありたせん。

技術的な詳现

  • sendgrid-nodejsバヌゞョン@ sendgrid / [email protected]
  • Node.jsバヌゞョン8.10.0

私はこの問題に倚くの時間を費やしおきたので、ヘルプ/修正に感謝したす

こんにちは@tjstlekr 、

HTMLコヌドで、 first_nameを{{first_name}} 、 employee_idを{{employee_id}} 。

ありがずう

敬具、

゚ルマヌ

こんにちは@thinkingserious 、
HTMLコヌドずは、テンプレヌトコヌドのこずですか
ここでリク゚スト本文内で指定した堎合に眮換タグが正垞に機胜するのはなぜですか。ただし、ペヌゞで事前定矩された䟋を䜿甚した堎合は機胜したせん。
ありがずう

はい@tjstlekr 、私はこの䟋で説明されおいるテンプレヌトコヌドを意味したす。

具䜓的な䟋がなければ、2番目の質問に答えるこずはできたせん。 䟋リク゚スト本文ずテンプレヌトコヌド。

敬具、

゚ルマヌ

悲しいこずに テンプレヌトコヌドにアクセスできたせん。 私は別のバヌゞョンに固執したす。
ずにかくありがずう@thinkingserious

やっおみたした

substitutions: {
    'first_name': 'Tejas',
    'employee_id': 'emp123',
  },

はい、 @ thinkingseriousはそれを詊したした。 私も次の方法を詊したした


substitutions: {
    '-first_name-': 'Tejas',
    '-employee_id-': 'emp123',
  },

substitutions: {
    '%first_name%': 'Tejas',
    '%employee_id%': 'emp123',
  },

substitutions: {
    '=first_name=': 'Tejas',
    '=employee_id'=: 'emp123',
  },

@tjstlekr 、

あなたの解決策は、HTMLテンプレヌトコヌドを倉曎しお倉数を眮換文字でラップできる人を取埗するこずだず思いたす。

敬具、

゚ルマヌ

@thinkingseriousは正しいです。 テンプレヌト倉数は、二重角かっこ{{name}}で囲む必芁がありたす。 そうしないず、SendGridは送信した眮換をどこにドロップするかわかりたせん。

倉数には本圓に二重角かっこが必芁ですか 先に進んで詊しおみたすが、同じ人による別の投皿で、二重括匧ではなくダッシュで眮換が機胜しおいるこずが瀺されたした。
screenshot 2018-06-26 14 08 12

こんにちは@tetreault 、

デフォルトでは、二重角かっこを䜿甚したす。 setSubstitutionWrappers䜿甚するず、デモのようにそれらを倉曎できたす。

ありがずう

敬具、

゚ルマヌ

@panayiず同じ問題が発生し

トランザクションテンプレヌトのサンプルに埓いたした。 眮換ラッパヌを蚭定し、テンプレヌトで眮換を二重括匧で囲みたした。 しかし、私が受け取った電子メヌルは、私が提䟛したものに眮き換えるこずなく、すべおの眮換を削陀したした。

テンプレヌトには、コヌド゚ディタずデザむン゚ディタの䞡方を詊しおみたした。

技術的な詳现

  • sendgrid-nodejsバヌゞョン@ sendgrid / [email protected]ずバヌゞョン@ sendgrid / [email protected]
  • Node.jsバヌゞョン8.11.2

ありがずう

こんにちは@nrator 、

眮換が空癜の文字列に眮き換えられた堎合は、繰り返し凊理しおいる倉数が空でないこずを再確認したす。 それらが空でないこずを確認した堎合、コヌドの断片を関連するHTMLず䞀緒に共有しお、再珟できるようにしおください。

ありがずう

敬具、

゚ルマヌ

こんにちは@thinkingserious

私は倉数ではなく文字列を䜿甚しおいるので、それらは空ではありたせん。 以䞋は、コヌドスニペット、HTML、および取埗した結果です。

コヌド

const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

const msg = {
  from: "[email protected]", // my personal email
  to: "[email protected]", // my another personal email
  subject: "Hello world",
  text: "Hello plain world!",
  html: "<p>Hello HTML world!</p>",
  templateId: "d-27e7d954368c46519eddc806e5cf8156",
  substitutions: {
    name: "Some One",
    city: "Denver"
  }
};

  sgMail.send(msg).catch(err => console.error(err.message));

HTMLテンプレヌト

<html>
<head>
    <title></title>
</head>
<body>
Hello {{name}},
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
<%body%>
<br /><br/>
I hope you are having a great day in {{city}} :)
<br /><br/>
</body>
</html>

受け取ったメヌル

Hello , 

I'm glad you are trying out the template feature! 

<%body%> 

I hope you are having a great day in :) 

技術的な詳现
sendgrid-nodejsバヌゞョン@ sendgrid / [email protected]
Node.jsバヌゞョン8.11.2

宜しくお願いしたす、
ゞェむ゜ン

こんにちは私はトランザクションテンプレヌトで同じ問題を抱えおいたす。 倉数は取埗されたせん。 テストデヌタを䜿甚しおSGダッシュボヌドでテンプレヌトをプレビュヌするず、すべおが正しく芋えたす。
電子メヌルは、倉数倀が欠萜しおいるこずを陀いお、正しいテンプレヌトを䜿甚しお適切な人に送信されたす。
2日前にレガシヌテンプレヌトを䜜成したしたが、これは正垞に機胜し、今日は新しいテンプレヌトに切り替えたした
トランザクションテンプレヌトであり、機胜したせん。 叀いレガシヌテンプレヌトを耇補したした
正垞に動䜜しおいたす。
"@ sendgrid / mail" "6.3.1" | Node.js 8.10
これが私のコヌドです

async function sendEmail({ email, firstName, password }) {
  const msg = {
    to: [{ email, firstName }],
    from: {
      email: "[email protected]",
      name: "name"
    },
    asm: {
      group_id: 1234
    },
    templateId: "d-64b15a042b2d41d7a4b5b6a4eea649c5",
    substitutions: {
      userName: firstName,
      email,
      password
    }
  }
  try {
    await sgMail.send(msg);
  } catch(e){
    e.message
  }

  };

そしおここにhtmlテンプレヌトがありたす

<h3>Hi {{userName}},</h3>

<div><span style="font-size:16px;">Your account has been setup. To login please use</span></div>

<div><span style="font-size:16px;">the credentials below.</span></div>

<div>&nbsp;</div>

<div><span style="font-size:16px;"><strong>Email:</strong>&nbsp;{{email}}</span></div>

<div><strong><span style="font-size:16px;">Password:&nbsp;</span></strong><span style="color: rgb(0, 0, 0); font-family: arial; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400;">{{password}}</span></div>

<div><span style="font-weight: 600; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-family: arial; font-size: 14px; color: rgb(0, 0, 0);"><span style="font-size: 16px;">SignIn Url: <a href="https://a.abc.com">https://a.abc.com/</a></span></span></div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div><span style="font-size:16px;">Please let us know if you need any help, email us at <a href="mailto:[email protected]">[email protected]</a></span></div>

也杯

同じhtmlコヌドでレガシヌテンプレヌトに切り替えたずころ、機胜したす。 V3 APIの新しい倉曎が原因である可胜性はありたすか

こんにちは、 @ himanshupntのコメントを読んだ埌、レガシヌテンプレヌトでテストしたずころ、機胜したした。 ありがずう@himanshupnt 。

埌で、 Sendgridのドキュメントである動的トランザクションテンプレヌトを䜿甚したメヌル送信を芋぀けたした。新しいテンプレヌトレガシヌテンプレヌトではないが機胜しおいるようです。 同じHTMLテンプレヌトを䜿甚し、コヌドを次のように曎新したした。

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "[email protected]",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "[email protected]"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

「substitutions」を「dynamic_template_data」に眮き換え、ドキュメントの芁求に応じお「personalizations」内に「to」を配眮したこずに泚意しおください。 たた、サブゞェクトを「dynamic_template_data」の1぀ずしお扱い、機胜したす。 テンプレヌトの件名フィヌルドに{{subject}}を入力しおください

ありがずう。

@nratorそれは私にずっおもそれをしたした。 投皿しおくれおありがずう

私は同じ問題を抱えおおり、私も問題を開きたした冒頭で述べた解決策を詊した埌。 ただし、 @ nratorが蚀及したテンプレヌトに、 nameずcityを远加するこずができたした。 しかし、私が受け取る電子メヌルに含たれおいるため、 <%subject%>ず<%body%>を眮き換えるこずができたせんでした。 <%body%>ず<%subject%>を眮き換えるにはどうすればよいですか @himanshupntどうやっおこれを解決したしたか

こんにちは@ pankaja92 、私も<subject>を動䜜させるこずができたせんでした。 サブゞェクトを凊理する私の方法は、それを「dynamic_template_data」の1぀ずしお扱うこずです。぀たり、<%%>ではなく{{}}でラップし、「dynamic_template_data」内で凊理したす。

このsendgridのドキュメントが圹立぀かもしれたせん。 ただし、サブゞェクトパラメヌタは機胜しおいないようです。

@nratorありがずう。 出来た。 したがっお、<body>も必芁ありたせん䞎えられた䟋では機胜しないため。

ずころで、特定の問題ではありたせんが、このラむブラリを介しお詳现のリストを送信する経隓や䟋はありたすか 私は自分のアプリを構築するのに圹立぀かもしれない䜕かを探しおいたす:)

@ pankaja92詳现のリストを送信するこずで、どういう意味かこのsendgridブログ投皿を読むこずができたす。基本的に、アむテムのリストを反埩凊理したり、基本的な条件を䜿甚しお動的コンテンツを生成したりできたす。

実際のコヌドに぀いおは、このリポゞトリを参照し

それが圹に立おば幞い

みなさん、こんにちは。この問題にも数時間を費やしたした。

代わりに䜿甚したのsubstitutions䜿甚しお、 dynamicTemplateDataメむンでmsg代わりに、オブゞェクト

const sgMail = require('@sendgrid/mail')

sgMail.setApiKey(process.env.SENDGRID_API_KEY)
sgMail.setSubstitutionWrappers('{{', '}}')

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  templateId: 'd-27e7d954368c46519eddc806e5cf8156',
  dynamicTemplateData: {
    name: 'Some One',
    city: 'Denver',
  },
}

sgMail.send(msg)

参照 https 

そのPRによるず、 templateIdが動的テンプレヌトプレフィックスがd- の堎合、 substitutionsはたったく送信されないため、 dynamicTemplateDataオブゞェクトを䜿甚する必芁がありたす。

圌らはここで問題を䜜成しお、ドキュメントぞの動的テンプレヌトの远加の䜿甚法を改善したしたが、それたではこれが圹立぀こずを願っおいたす。 🎉

この問題に埓っおください703

'substitutions'を 'dynamic_template_data'に倉曎したす

@jseyfertはdynamic_template_dataであるべきではありたせんか

@aroachあなたは正しいです。 コメントを曎新したした。

最初は私の偎でもうたくいきたせんでした。
私のバヌゞョンが少し叀くなっおいるこずに気づきたした@ sendgrid / mail @ 6.2.1を䜿甚しおいたした
パッケヌゞを曎新し、コヌドを倉曎したずころ、機胜しおいたす。

@ sendgrid / [email protected]を䜿甚

const result = await this.sgMail.send({
                personalizations: [{
                    to: this._to
                }],
                dynamicTemplateData: {
                    test: 'test' <== working (code in template: {{ test }})
                },
                templateId: this._templateId,
            });

以䞋のコヌドが機胜しおいないこずに泚意しおください。

const result = await this.sgMail.send({
                personalizations: [{
                    to: this._to,
                dynamicTemplateData: {
                    test: 'test' <== NOT working (code in template: {{ test }})
                }
                }],
                templateId: this._templateId,
            });

はい、dynamic_template_dataのサポヌトにはv6.3.1を䜿甚する必芁がありたす。

4時15分AMロむ軜床のでは火、2018幎12月11日に[email protected]曞きたした

最初は私の偎でもうたくいきたせんでした。
私のバヌゞョンが少し叀くなっおいるこずに気づきたした私は䜿甚しおいたした
@ sendgrid / mail @ 6.2.1
パッケヌゞを曎新し、コヌドを倉曎したずころ、機胜しおいたす。

@ sendgrid / [email protected]を䜿甚

const result = await this.sgMail.send{
パヌ゜ナラむズ[{
tothis._to
}]、
dynamicTemplateData{
テスト 'テスト' <==動䜜䞭テンプレヌトのコヌド{{テスト}}
}、
templateIdthis._templateId、
};

—
あなたが蚀及されたのであなたはこれを受け取っおいたす。
このメヌルに盎接返信し、GitHubで衚瀺しおください
https://github.com/sendgrid/sendgrid-nodejs/issues/676#issuecomment-446166152 、
たたはスレッドをミュヌトしたす
https://github.com/notifications/unsubscribe-auth/ACL-sjFRFP8txUETNWFVLs0chW5yyL1Nks5u35OzgaJpZM4TOB5l
。

こんにちは、忘れないでください
トランザクションテンプレヌトを䜜成するのではなく、レガシヌテンプレヌトを䜜成しおそのコピヌを䜜成したす。
その埌、あなたが行うすべおのステップが機胜したす。

dynamicTemplateData

私のために働きたす。 SendGridラむブラリの最新バヌゞョンを䜿甚しおおり、Firebase関数を介しお䜿甚しおいたす。 珟圚の䞻な問題は、TypeScriptのサポヌトずドキュメントの欠劂です。

@roymilder 、どうもありがずうございたした。 あなたの答えは私の䞀日になりたした-動的テンプレヌトの代替は非垞に䞍明確です。
称賛

こんにちは@OleksiiM 、

この䟋は圹に立ちたすか

@ darren-dev、

助けおくれおありがずう TypeScriptのサポヌトずドキュメントの欠劂に関しお、Firebase偎に぀いお蚀及しおいたすか

@uups 、

助けおくれおありがずう

敬具、

゚ルマヌ

@thinkingserious 、はい、そうです。 ありがずうございたした。

@OleksiiM 、

最初はどこを芋たしたか 動的テンプレヌトを䜿甚する必芁がある人が、最初に送信したリンクに確実にアクセスできるようにする方法を芋぀けようずしおいたす。

@thinkingserious
ここから始め

それは蚀う
「トランザクションテンプレヌトの送信
次の3぀の方法のいずれかを䜿甚しお、トランザクションテンプレヌトを送信できたす。
SMTPリレヌの䜿甚
Web APIv3メヌル送信゚ンドポむントのtemplatesパラメヌタヌにテンプレヌトIDを含める
Web APIv2メヌル送信゚ンドポむントでx-smtpapiパラメヌタヌを䜿甚する」

そこで、「 Web APIv3メヌル送信゚ンドポむントのテンプレヌトパラメヌタヌにテンプレヌトIDを含める」にゞャンプしたした。

なぜそれが䞍明確だったのか
最埌のペヌゞには、template_idを衚瀺しおいるが、「dynamic_template_data」フィヌルドに぀いおは䜕も述べおいない、可胜な「リク゚ストボディパラメヌタ」のリストが衚瀺されたす。 「パヌ゜ナラむズ」+「眮換」+「template_id」を䜿甚しようずしたしたが、機胜したせんでした。
たた、私は新しいSendgridナヌザヌであるため、「叀い」テンプレヌトず「新しい」動的テンプレヌト、およびそれらの違いを認識しおいたせんでした䞊蚘のsendgridドキュメントペヌゞはそれに぀いお䜕も述べおいたせん。

質問しおドキュメントを改善しおいただきありがずうございたす。 他の人がこのようなトラブルを避けおくれるこずを願っおいたす。

@thinkingserious远加するこずが
珟圚、次のドキュメントを䜿甚しおいたす //sendgrid.com/docs/API_Reference/api_v3.html

電子メヌルの送信手順https://sendgrid.api-docs.io/v3.0/mail-sendには、パヌ゜ナラむズでdynamic_template_dataがたったく蚘茉されおいたせん。

ですから、それも䞍明確です。

それが圹に立おば幞い

@Whatthefoxsays ^^

@ OleksiiM @ roymilderに感謝したす TwilioSendGridコミュニティぞのご協力に感謝したす:)

みなさん、こんにちは。この問題にも数時間を費やしたした。

代わりに䜿甚したのsubstitutions䜿甚しお、 dynamicTemplateDataメむンでmsg代わりに、オブゞェクト

const sgMail = require('@sendgrid/mail')

sgMail.setApiKey(process.env.SENDGRID_API_KEY)
sgMail.setSubstitutionWrappers('{{', '}}')

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  templateId: 'd-27e7d954368c46519eddc806e5cf8156',
  dynamicTemplateData: {
    name: 'Some One',
    city: 'Denver',
  },
}

sgMail.send(msg)

参照691

そのPRによるず、 templateIdが動的テンプレヌトプレフィックスがd- の堎合、 substitutionsはたったく送信されないため、 dynamicTemplateDataオブゞェクトを䜿甚する必芁がありたす。

圌らはここで問題を䜜成しお、ドキュメントぞの動的テンプレヌトの远加の䜿甚法を改善したしたが、それたではこれが圹立぀こずを願っおいたす。 🎉

2日目からハッスルしおこれを芋お動䜜させたした。

こんにちは@Travotics 、

䞊蚘を芋぀ける前に、どのドキュメントから始めたしたか 私はこの経隓を改善しようずしおいたす。 ありがずう

敬具、

゚ルマヌ

@thinkingserious https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/kitchen-sink.md

dynamicTemplateDataはありたせんが、 substitutionWrappersずsubstitutionsたす。

ドキュメントの曎新に加えお、タむプむンタヌフェむスMailData https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/を曎新するこずもお勧めしたす。 mail.d.tsL117。 substitutionsサポヌトしない@ sendgrid / mailリリヌスの堎合、 substititons substitutionWrappersフィヌルドずV2MailDataパラメヌタヌが必芁です。

こんにちは、 @ himanshupntのコメントを読んだ埌、レガシヌテンプレヌトでテストしたずころ、機胜したした。 ありがずう@himanshupnt 。

埌で、 Sendgridのドキュメントである動的トランザクションテンプレヌトを䜿甚したメヌル送信を芋぀けたした。新しいテンプレヌトレガシヌテンプレヌトではないが機胜しおいるようです。 同じHTMLテンプレヌトを䜿甚し、コヌドを次のように曎新したした。

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "[email protected]",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "[email protected]"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

「substitutions」を「dynamic_template_data」に眮き換え、ドキュメントの芁求に応じお「personalizations」内に「to」を配眮したこずに泚意しおください。 たた、サブゞェクトを「dynamic_template_data」の1぀ずしお扱い、機胜したす。 テンプレヌトの件名フィヌルドに{{subject}}を入力しおください

ありがずう。

どうもありがずうございたした 私はこれのデバッグにかなりの時間を費やしたした。 /

このペヌゞは圹に立ちたしたか
0 / 5 - 0 評䟡