[英]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.