让我发疯,我使用sendgrid发送电子邮件,我想在PHP中发送带有文本/纯文本/html变体的电子邮件。
我尝试的是:
我已经分析了一封具有2种内容类型的电子邮件。我看到:
---- == _ mimepart_35656456787
content-type:text/plain;charset = utf-8
[纯文本版本....]
---- == _ mimepart_67868876878
content-type:text/html;charset = utf-8
[html版本....]
然后我尝试添加这样的两个变体:
...
$from = new SendGridEmail(null, $from);
$email = new SendGridEmail(null, $to);
$content = new SendGridContent("text/plain",$body_plain);
$content1 = new SendGridContent("text/html",$body_html);
$mail = new SendGridMail($from, $subject, $email, $content1, $content);
结果:
这是我得到的:
---- == _ 35656456787
content-type:text/plain;charset = utf-8
[纯文本版本....]
---- == _ 67868876878
content-type:text/html;charset = utf-8
[html版本....]
MimePart缺少。
sendgrid也建议(此处:https://sendgrid.com/docs/classroom/build/format_content/html_formatting_issues.html)使用普通和html变体发送电子邮件。所以可能...
,但我试图找到如何做到这一点,但我没有找到STHG ..
问题:有人有同样的问题吗?如何使用平原和HTML发送电子邮件??
有什么想法?
我检查了源代码,发现邮件()函数仅需4个参数,
public function __construct($from, $subject, $to, $content)
所以您的代码
$mail = new SendGridMail($from, $subject, $email, $content1, $content);
不应该工作。
您可以同时发送HTML和纯文本,而无需使用助手类:
// If you are using Composer (recommended)
require 'vendor/autoload.php';
// If you are not using Composer
// require("path/to/sendgrid-php/sendgrid-php.php");
$to_email="recepient@somedomain.com";
$to_name="John Smith";
$subject="Testing sendgrid. See you in spam folder!";
$html="and easy to do anywhere, even with PHP<a href='https://someurl.com'>Really</a>";
$text="and easy to do anywhere, even with PHP";
$json=<<<JSON
{
"personalizations": [
{
"to": [
{
"email": "$to_email",
"name": "$to_name"
}
],
"subject": "$subject"
}
],
"from": {
"email": "youremail@somedomain.com",
"name": "Your Name"
},
"content": [
{
"type": "text/plain",
"value": "$text"
},
{
"type": "text/html",
"value": "$html"
}
]
}
JSON;
$request_body = json_decode($json);
$apiKey = "yourapikey";
$sg = new SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());