如何使用 Sendgrid 为电子邮件添加内联图像 API

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 邮件程序块的代码库,大多数变量都是从其他脚本提供的,以供读取。我的代码有效,但有一个问题,图像在发送到电子邮件时会通过,但作为附件不会内嵌在发送的电子邮件中,这不是我想要实现的,所以,我需要一个指南来让它工作。

预期:电子邮件已发送,收件人会收到一封电子邮件,其中包含电子邮件上下文中的图像,而不是作为电子邮件的附件图像。

     $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 函数中对图像文件进行编码使其工作。虽然它仍然显示为附件,但它也反映在电子邮件中。