[英]How to Add Inline Image for Email with Sendgrid API
<?php
// Load Composer's autoloader
require_once __DIR__.'/../vendor/autoload.php';
use SendGrid\Mail\Asm;
use SendGrid\Mail\Attachment;
use SendGrid\Mail\BatchId;
use SendGrid\Mail\Bcc;
use SendGrid\Mail\BccSettings;
use SendGrid\Mail\BypassListManagement;
use SendGrid\Mail\Category;
use SendGrid\Mail\Cc;
use SendGrid\Mail\ClickTracking;
use SendGrid\Mail\Content;
use SendGrid\Mail\CustomArg;
use SendGrid\Mail\Footer;
use SendGrid\Mail\From;
use SendGrid\Mail\Ganalytics;
use SendGrid\Mail\GroupId;
use SendGrid\Mail\GroupsToDisplay;
use SendGrid\Mail\Header;
use SendGrid\Mail\HtmlContent;
use SendGrid\Mail\IpPoolName;
use SendGrid\Mail\Mail;
use SendGrid\Mail\MailSettings;
use SendGrid\Mail\OpenTracking;
use SendGrid\Mail\PlainTextContent;
use SendGrid\Mail\ReplyTo;
use SendGrid\Mail\SandBoxMode;
use SendGrid\Mail\Section;
use SendGrid\Mail\SendAt;
use SendGrid\Mail\SpamCheck;
use SendGrid\Mail\Subject;
use SendGrid\Mail\SubscriptionTracking;
use SendGrid\Mail\Substitution;
use SendGrid\Mail\TemplateId;
use SendGrid\Mail\To;
use SendGrid\Mail\TrackingSettings;
function sendEmail($tos, $subject, $html)
{
// Instantiation and passing `true` enables exceptions
$email = new \SendGrid\Mail\Mail();
$email->setFrom("hi@pandascrow.io", "Pandascrow");
$email->setSubject($subject);
$tos = [
"tom***@gmail.com" => "Precious Tom",
"jamb***@gmail.com" => "Jamb Tom"
];
$email->addTos($tos);
$email->addContent("text/html", $html);
$attachments = [
new Attachment(
"base64 encoded content2",
"../../logo.png",
"image/png",
"inline",
"logo"
),
new Attachment(
"content3",
"../../assets/img/social-icons/twitter.png",
"image/png",
"inline",
"twitter"
),
new Attachment(
"encoded",
"../../assets/img/social-icons/linkedin.png",
"image/png",
"inline",
"linkedin"
),
new Attachment(
"base64",
"../../assets/img/social-icons/instagram.png",
"image/png",
"inline",
"instagram"
)
];
$email->addAttachments($attachments);
$sendgrid = new \SendGrid(SENDGRID_API_KEY);
try {
$response = $sendgrid->send($email);
return ($response->statusCode() == 202) ? 200 : $response->statusCode() ; // If 202 which means Accepted return 200
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
return 'Caught exception: '. $e->getMessage() ."\n";
}
}
?>
上面的代码是我当前的 Sendgrid 邮件程序块的代码库,大多数变量都是从其他要读取的脚本中获取的。 我的代码有效,但有一个问题,当发送到 email 时,图像会通过,但作为发送的 email 中的附件不内联,这不是我想要实现的,因此,我需要一个指南来让它工作。
预期:Email 已发送,接收方获得 email,其中图像在 email 上下文中,而不是作为 email 的附加图像。
$attachments = [
new Attachment(
base64_encode(file_get_contents('../../logo.png')),
"image/png",
"inline",
"logo"
),
new Attachment(
base64_encode(file_get_contents('../../assets/img/social-icons/twitter.png')),
"image/png",
"inline",
"twitter"
),
new Attachment(
base64_encode(file_get_contents('../../assets/img/social-icons/linkedin.png')),
"image/png",
"inline",
"linkedin"
),
new Attachment(
base64_encode(file_get_contents('../../assets/img/social-icons/instagram.png')),
"image/png",
"inline",
"instagram"
)
];
在实际 base64 function 中编码图像文件使其工作。 虽然它仍然显示为附件,但它也在 email 中反映出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.