次のペイロードで「BadRequest」エラーが発生します。
cosnt msg = {
"to": "[email protected]",
"from": "[email protected]",
"subject": "Support Pay Transparency at PayCheck",
"templateId": "24ae3147-4faa-4380-8613-c5be144f4542",
"customArgs": {
"ally_id": "cj6zlh7yd000001qir4r5suuk"
}
};
sgMail.send(msg)
{
"message": "Bad Request",
"code": 400,
"response": {
"headers": {
"server": "nginx",
"date": "Wed, 30 Aug 2017 22:30:41 GMT",
"content-type": "application/json",
"content-length": "365",
"connection": "close",
"access-control-allow-origin": "https://sendgrid.api-docs.io",
"access-control-allow-methods": "POST",
"access-control-allow-headers": "Authorization, Content-Type, On-behalf-of, x-sg-elas-acl",
"access-control-max-age": "600",
"x-no-cors-reason": "https://sendgrid.com/docs/Classroom/Basics/API/cors.html"
},
"body": {
"errors": [
{
"message": "Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required.",
"field": "content",
"help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"
}
]
}
}
}
ドキュメントではtemplateId
表示されていますが、エラーメッセージではtemplate_id
と表示されています。 両方を試しましたが、同じエラーメッセージが表示されました。 sendgridで正しく構成されていることを確認するために、健全性チェックとしてテンプレート画面のスクリーンショットを添付しました。
@onesien新しいノードのJSメーラーAPIは大文字と小文字を区別しないため、 templateId
またはtemplate_id
両方を使用できます。 キャメルケースはJSランドでより一般的であるため、API自体がスネークケースを消費する一方で、そのサポートを追加しました。
テンプレートIDで何が起こっているのか見てみましょう
こんにちは@onesien 、
サポートに確認することもできます。 この問題は古いテンプレートエディタに関連している可能性があると思います。
新しいテンプレートを作成してそのIDを使用することもできますか?
敬具、
エルマー
@thinkingseriousあなたはあなたの側でこれをチェックできますか? リクエストとともにtemplate_id
パラメータを正しく送信しているようです。 新しいテンプレートを作成したばかりで、この問題も発生しています。
@adamreisnz 、
ローカルテストは機能していますが、アカウントには新しいテンプレートしかありません。
再確認します
いいえ、新しく作成した新しいテンプレートを使用しましたが、同じ問題が発生しました
以下は、ここの例に従って(バージョンv6.1.1を使用して)私のために機能し
// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');
//build object
var mailOptions = {
personalizations:[{
to: '[email protected]',
substitutions: {'name':'John', 'city':'Denver'}
}],
from: '[email protected]',
reply_to: '[email protected]',
subject: 'Hello',
html: 'email text goes here',
templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
};
//send
sendGridMail.send(mailOptions);
ああ、APIがすべての場合にコンテンツブロックを必要とする古いバグのリサーフェシングがあると思います。 したがって、これは機能します。
// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');
//build object
var mailOptions = {
to: '[email protected]',
from: '[email protected]',
reply_to: '[email protected]',
subject: 'Hello',
templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
html: ' ',
substitutions: {
name: 'Some One',
city: 'Denver',
}
};
//send
sendGridMail.send(mailOptions);
しかし、これではありません:
// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');
//build object
var mailOptions = {
to: '[email protected]',
from: '[email protected]',
reply_to: '[email protected]',
subject: 'Hello',
templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
substitutions: {
name: 'Some One',
city: 'Denver',
}
};
//send
sendGridMail.send(mailOptions);
内部で確認しています...
テンプレートを送信するには、コンテンツブロックを追加する必要がありました。 新しいhtmlエディターで作成されたテンプレートを使用しているときに、_Bad Request(400) "有効なtemplate_idが指定されていない限り、contentパラメーターが必要です。少なくとも1つの定義済みコンテンツブロックが必要です..." _を受け取りました。
sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
to: email,
from: '[email protected]',
templateId: 'xxxxxxx',
substitutions: {
verifyUrl: createAccountLink
}
};
sgMail.send(msg)
コンテンツブロックを追加すると、テンプレートの電子メールはhtmlエディターでフォーマットされたとおりに送信されます。
sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
to: email,
from: '[email protected]',
content: [{"type":"text/html","value":"0"}],
templateId: 'xxxxxx',
substitutions: {
verifyUrl: createAccountLink
}
};
sgMail.send(msg)
OK、これはバグとして開いたままにしておきます。
私の推測では、このSDKは、設定されていないときに空のコンテンツオブジェクトを追加しています。 htmlとtxtが設定されていない場合は、これらの関数をスキップする必要があると思います。
今のところ、上記の回避策はあなたが必要とすることをします。
@thinkingserious私はあなたが正しいと思います。 APIドキュメントを読み、フィールドは必須だと思いました。 それは私が見逃したよく隠されたコメント(驚くほどスタイリングされています!)だけを持っています:
これに対処するためのPRを作成します!
ふふ、アダムに感謝!
PRがアップしています。これで問題が修正され、コンテンツが提供されていない場合はリクエストとともにコンテンツフィールドが送信されません。
助けてくれてありがとう。 私は文字通り頭がおかしくなっていました。 私はついに昨夜遅くに回避策を見つけて、より多くの情報を報告するためにここに来ていました、しかしあなた方全員がすでにそれに取り組んでいるようです:-)
その@onesienについて申し訳ありません、
修正はnpm(v6.1.2)にプッシュされました。
@thinkingseriousの応答から逐語的にコードを使用しましたが、まだ問題が発生しています。
sengridで作成したテンプレートで最も基本的な置換を取得して送信しようとしています。 CloudWatchのレスポンス(これはLambdaで実行されているため)は202と表示されていますが、受信するメールアドレスはA message was received from this address by our systems that had errors in the smtpapi header, and cannot be processed. The error detected was: The template id must be a valid template id for your account.
私はここで何が間違っているのですか? sendgridのドキュメントをふるいにかけようとしていますが、すべてが本当に断片的に感じられます。
テンプレートIDをどのように送信しますか? ラムダが呼び出されたとき、それは環境変数を介していますか?
速い返信@cbilliauに感謝し
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(
"XXXXX"
);
sgMail.setSubstitutionWrappers("%", "%"); // Configure the substitution tag wrappers globally
const msg = {
to: formFields.toEmail,
from: formFields.fromEmail,
subject: "Hello world",
text: "test",
html: "<p>test</p>",
templateId: "be2cc0da-5b2c-428f-8e45-c140f6cfb6eb",
substitutions: {
name1: formFields.fromName,
name2: formFields.fromName,
date: formFields.date
}
};
sgMail.send(msg);
@tetreault上記の私の答えをチェックアウトしてください。関数にcontent: [{"type":"text/html","value":"0"}],
を追加すると、機能しました。 理由は不明です。
試してみましたが、残念ながら最終結果は変更されませんでした@cbilliau :(。同じメールが返ってきました:
A message was received from this address by our systems that had errors in the smtpapi header, and cannot be processed.
The error detected was: The template id must be a valid template id for your account. You provided be2cc0da-5b2c-428f-8e45-c140f6cfb6eb
うわー、ちょっとお辞儀をさせてください@ cbilliau🤣私はただ物事を急いでいて、それ以上の注意を払っていませんでした。 「マーケティング」で作成したテンプレートの一意のIDを取得しているが、「トランザクションテンプレート」では取得していないことに気付きました。 私はsendgridUIをあまり使用せず、これを完全に失敗させましたlol
顔全体の手のひら、私の側を修正し、うまくいけばそれが機能することを期待しています。
kk-特に@cbilliauが言及したcontent: [{ type: "text/html", value: "0" }]
行を使用して、現在動作していることを確認します👍
最も参考になるコメント
助けてくれてありがとう。 私は文字通り頭がおかしくなっていました。 私はついに昨夜遅くに回避策を見つけて、より多くの情報を報告するためにここに来ていました、しかしあなた方全員がすでにそれに取り組んでいるようです:-)