SendGrid 电子邮件替换不适用于 Gmail 电子邮件
SendGrid email substitutions does not work for Gmail emails
我正在使用 SendGrid API 发送电子邮件,并且我正在使用动态电子邮件模板。
我的电子邮件模板如下所示。
电子邮件同时发送到 Gmail 和使用我的域创建的电子邮件,例如 admin@mydomain.com。
我正在使用电子邮件替换来替换模板中的 {{substitutables}}
。这些替换似乎适用于来自我域的电子邮件,但不适用于 Gmail 电子邮件。
Gmail 电子邮件如下所示。我正在为我的 Gmail 电子邮件使用网络浏览器。
这是我的域电子邮件的样子。我使用 MS Outlook 作为域电子邮件的邮件客户端。
以下是我用来发送电子邮件的代码。
public function sendEmail(
string $templateId,
array $recipiants,
array $substitutions = [],
array $ccs = [],
array $bccs = [],
array $replyTo = [],
array $attachments = []
){
$emailParams = $this->params->get('email');
$apiKey = $emailParams['sendgrid']['api_key'];
$sendgrid = new \SendGrid($apiKey);
$email = new Mail();
if (!$recipiants || !is_array($recipiants) || count($recipiants) == 0){
throw new NotFoundHttpException("Email Recipients Not Found!");
}
$email->setTemplateId($templateId);
$email->setFrom(new From($emailParams['from']['email'], $emailParams['from']['name']));
// adding recients to the emial
foreach ($recipiants as $recipiant){
$personalization = new Personalization();
$toEmail = new To($recipiant['email'], $recipiant['name']);
$personalization->addTo($toEmail);
$email->addPersonalization($personalization);
}
// adding CCs to email
if (count($ccs) > 0) {
foreach ($ccs as $cc) {
$ccEmail = new Cc($cc['email'], $cc['name']);
$email->addCc($ccEmail);
}
}
// adding BCCs to email
if (count($ccs) > 0) {
foreach ($bccs as $bcc) {
$bccEmail = new Bcc($bcc['email'], $bcc['name']);
$email->addBcc($bccEmail);
}
}
// adding template substitutions
if (count($substitutions) > 0) {
$email->addDynamicTemplateDatas($substitutions);
}
// sending email
/** @var Response $response */
$response = $sendgrid->send($email);
if ($response->statusCode() != 202){
throw new AccessDeniedHttpException("Failed to Send Email!");
}
}
有人可以指出我做错了什么以及为什么会这样吗?谢谢你的时间。
终于弄清楚我的代码出了什么问题。显然,即使您使用 $email = new Mail();
创建一个电子邮件对象,您也可以有多个收件人。
当您使用个性化设置时,它会向每个收件人发送单独的电子邮件。因此,您必须分别为每个个性化设置 DynamicTemplateData、CC 和 BCC。除非它只替代 $recipiants
数组中的第一个收件人。
以下是更新后的代码。
public function sendEmail(
string $templateId,
array $recipiants,
array $substitutions = [],
array $replyTo = [],
array $attachments = []
){
$emailParams = $this->params->get('email');
$apiKey = $emailParams['sendgrid']['api_key'];
$sendgrid = new \SendGrid($apiKey);
$email = new Mail();
if (!$recipiants || !is_array($recipiants) || count($recipiants) == 0){
throw new NotFoundHttpException("Email Recipients Not Found!");
}
$email->setTemplateId($templateId);
$email->setFrom(new From($emailParams['from']['email'], $emailParams['from']['name']));
// adding recients to the emial
foreach ($recipiants as $recipiant){
$personalization = new Personalization();
$toEmail = new To($recipiant['email'], $recipiant['name']);
$personalization->addTo($toEmail);
// adding substitutions to individual personalization
foreach ($substitutions as $key => $value) {
$personalization->addDynamicTemplateData($key, $value);
}
// adding CCs to email
if (array_key_exists('cc', $recipiant)) {
foreach ($recipiant['cc'] as $cc) {
$ccEmail = new Cc($cc['email'], $cc['name']);
$personalization->addCc($ccEmail);
}
}
// adding BCCs to email
if (array_key_exists('bcc', $recipiant)) {
foreach ($recipiant['bcc'] as $bcc) {
$bccEmail = new Bcc($bcc['email'], $bcc['name']);
$personalization->addBcc($bccEmail);
}
}
// bcc every email to Zone101
$bccEmail = new Bcc($emailParams['bcc']['email'], $emailParams['bcc']['name']);
$personalization->addBcc($bccEmail);
$email->addPersonalization($personalization);
}
// sending email
/** @var Response $response */
$response = $sendgrid->send($email);
if ($response->statusCode() != 202){
throw new AccessDeniedHttpException("Failed to Send Email!");
}
}
我正在使用 SendGrid API 发送电子邮件,并且我正在使用动态电子邮件模板。
我的电子邮件模板如下所示。
电子邮件同时发送到 Gmail 和使用我的域创建的电子邮件,例如 admin@mydomain.com。
我正在使用电子邮件替换来替换模板中的 {{substitutables}}
。这些替换似乎适用于来自我域的电子邮件,但不适用于 Gmail 电子邮件。
Gmail 电子邮件如下所示。我正在为我的 Gmail 电子邮件使用网络浏览器。
这是我的域电子邮件的样子。我使用 MS Outlook 作为域电子邮件的邮件客户端。
以下是我用来发送电子邮件的代码。
public function sendEmail(
string $templateId,
array $recipiants,
array $substitutions = [],
array $ccs = [],
array $bccs = [],
array $replyTo = [],
array $attachments = []
){
$emailParams = $this->params->get('email');
$apiKey = $emailParams['sendgrid']['api_key'];
$sendgrid = new \SendGrid($apiKey);
$email = new Mail();
if (!$recipiants || !is_array($recipiants) || count($recipiants) == 0){
throw new NotFoundHttpException("Email Recipients Not Found!");
}
$email->setTemplateId($templateId);
$email->setFrom(new From($emailParams['from']['email'], $emailParams['from']['name']));
// adding recients to the emial
foreach ($recipiants as $recipiant){
$personalization = new Personalization();
$toEmail = new To($recipiant['email'], $recipiant['name']);
$personalization->addTo($toEmail);
$email->addPersonalization($personalization);
}
// adding CCs to email
if (count($ccs) > 0) {
foreach ($ccs as $cc) {
$ccEmail = new Cc($cc['email'], $cc['name']);
$email->addCc($ccEmail);
}
}
// adding BCCs to email
if (count($ccs) > 0) {
foreach ($bccs as $bcc) {
$bccEmail = new Bcc($bcc['email'], $bcc['name']);
$email->addBcc($bccEmail);
}
}
// adding template substitutions
if (count($substitutions) > 0) {
$email->addDynamicTemplateDatas($substitutions);
}
// sending email
/** @var Response $response */
$response = $sendgrid->send($email);
if ($response->statusCode() != 202){
throw new AccessDeniedHttpException("Failed to Send Email!");
}
}
有人可以指出我做错了什么以及为什么会这样吗?谢谢你的时间。
终于弄清楚我的代码出了什么问题。显然,即使您使用 $email = new Mail();
创建一个电子邮件对象,您也可以有多个收件人。
当您使用个性化设置时,它会向每个收件人发送单独的电子邮件。因此,您必须分别为每个个性化设置 DynamicTemplateData、CC 和 BCC。除非它只替代 $recipiants
数组中的第一个收件人。
以下是更新后的代码。
public function sendEmail(
string $templateId,
array $recipiants,
array $substitutions = [],
array $replyTo = [],
array $attachments = []
){
$emailParams = $this->params->get('email');
$apiKey = $emailParams['sendgrid']['api_key'];
$sendgrid = new \SendGrid($apiKey);
$email = new Mail();
if (!$recipiants || !is_array($recipiants) || count($recipiants) == 0){
throw new NotFoundHttpException("Email Recipients Not Found!");
}
$email->setTemplateId($templateId);
$email->setFrom(new From($emailParams['from']['email'], $emailParams['from']['name']));
// adding recients to the emial
foreach ($recipiants as $recipiant){
$personalization = new Personalization();
$toEmail = new To($recipiant['email'], $recipiant['name']);
$personalization->addTo($toEmail);
// adding substitutions to individual personalization
foreach ($substitutions as $key => $value) {
$personalization->addDynamicTemplateData($key, $value);
}
// adding CCs to email
if (array_key_exists('cc', $recipiant)) {
foreach ($recipiant['cc'] as $cc) {
$ccEmail = new Cc($cc['email'], $cc['name']);
$personalization->addCc($ccEmail);
}
}
// adding BCCs to email
if (array_key_exists('bcc', $recipiant)) {
foreach ($recipiant['bcc'] as $bcc) {
$bccEmail = new Bcc($bcc['email'], $bcc['name']);
$personalization->addBcc($bccEmail);
}
}
// bcc every email to Zone101
$bccEmail = new Bcc($emailParams['bcc']['email'], $emailParams['bcc']['name']);
$personalization->addBcc($bccEmail);
$email->addPersonalization($personalization);
}
// sending email
/** @var Response $response */
$response = $sendgrid->send($email);
if ($response->statusCode() != 202){
throw new AccessDeniedHttpException("Failed to Send Email!");
}
}