SendGridのコンテンツをNodejsでテンプレート化するにはどうすればよいですか?
私はSendGridを使用してアプリケーションの連絡フォームから電子メールを送信しようとしています。私は、HTTP投稿を使って呼び出すGoogle Cloud機能を持っています。 JSONオブジェクトとしてのフォームデータをGoogle Cloud Functionに渡し、メールコンテンツに未処理のJSONオブジェクトを表示できますが、SendGridコンテンツのテンプレートを作成しようとすると、JSONオブジェクトのプロパティは未定義の状態に戻ります。 SendGridの電子メールコンテンツに異なるformData
のプロパティを表示するにはどうすればよいですか?SendGrid Content Templating with Nodejs
const functions = require('firebase-functions');
const sg = require('sendgrid')(
process.env.SENDGRID_API_KEY || '<my-api-key-placed-here>'
);
exports.contactMail = functions.https.onRequest((req, res) => {
contactMail(req.body);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send("Mail Successfully Sent!");
})
function contactMail(formData) {
const mailRequest = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [{
to: [{ email: '[email protected]' }],
subject: 'Contact Us Form Submitted'
}],
from: { email: '[email protected]' },
content: [{
type: 'text/plain',
value: `
You have received a contact us form submission. Here is the data:
Name: ${formData.userFirstName} ${formData.userLastName}
Email: ${formData.userEmail}
Subject: ${formData.formSubject}
Message: ${formData.formMessage}
`
}]
}
});
sg.API(mailRequest, function (error, response) {
if (error) {
console.log('Mail not sent; see error message below.');
} else {
console.log('Mail sent successfully!');
}
console.log(response);
});
}
これは、各テンプレート式の未定義表示されます。ここでは
コードがあります。
私のコンテンツセクションはこれに設定されている場合は:
content: [{
type: 'text/plain',
value: formData
}]
そして、電子メールの内容は、生のJSONオブジェクトとして表示されます。
SendGridの電子メールコンテンツをクリーンアップして、JSONデータをフォーマットされた、よりクリーンな方法で表示するにはどうすればよいですか?