【发布时间】:2021-07-06 08:30:48
【问题描述】:
我想发送一封带有图片作为附件的电子邮件,并将其嵌入正文中。
<?php
$email = "to@example.com";
$name = "some_name";
$img = file_get_contents('image.jpg');
$body = '<img src = "cid:image">';
$subject = "Test email";
$headers = array(
'Authorization: Bearer API_KEY',
'Content-Type: application/json'
);
$data = array(
"personalizations" => array(
array(
"to" => array(
array(
"email" => $email,
"name" => $name
)
)
)
),
"from" => array(
"email" => "from@example.com"
),
"subject" => $subject,
"content" => array(
array(
"type" => "text/html",
"value" => $body
)
),
"attachments" => array(
array(
"content" => base64_encode($img),
"type" => "image/jpeg",
"filename" => "image",
"disposition" => "inline",
"content_ID" => "image",
//"disposition" => "attachment"
)
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;?>
问题是它要么作为内联内容要么作为附件,但不是两者兼而有之。我不知道如何解决这个问题。 我尝试为内联和附件添加两个单独的 attachments 块,但它总是考虑第二个块。 似乎没有任何效果。
【问题讨论】:
标签: php image api curl sendgrid