【发布时间】:2017-07-27 13:33:19
【问题描述】:
我是 SendGrid 的新手,我正在尝试弄清楚为什么电子邮件总是以纯文本而不是我制作的设计 HTML 事务模板发送。如果我使用 sendgrid“预览/测试”功能向自己发送电子邮件,它会通过查看它应该如何发送、图像、HTML 等来实现。
但是,当我使用 PHP API 发送电子邮件时,电子邮件会以纯文本形式发送。我用这一行告诉 SendGrid 使用哪个模板:
$mail->setTemplateId([my template ID]);
在我最终调用以下命令之前,我还应该通过 PHP API 设置一些其他的东西吗?
$sg->client->mail()->send()->post($mail);
下面是我所有的 SendGrid 代码:
$from = new SendGrid\Email([website name], [website email address]);
$subject = "test subject";
$to = new SendGrid\Email(null, $email);
$content = "";
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$mail->setTemplateId([my template ID]);
//$mail->setASMGroupId(3057);
$mail->personalization[0]->addSubstitution("%email%", $email);
$mail->personalization[0]->addSubstitution("%hash%", $hash);
$apiKey = [my api key];
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
我没有在 $content 中添加任何内容,因为内容是在模板中指定的。
如果有人知道为什么从 PHP 发送时它只发送我的模板电子邮件的纯文本版本,任何建议将不胜感激。
谢谢
编辑
我想我可能必须在标题中设置内容类型,所以我添加了:
$mail->addHeader("Content-Type", "text/html");
但这只是给我一个错误。我有什么遗漏吗?
【问题讨论】:
-
我尝试添加“Content-Type”标头,但仍有问题。
标签: sendgrid sendgrid-api-v3 sendgrid-templates