【问题标题】:How to Add Inline Image for Email with Sendgrid API如何使用 Sendgrid API 为电子邮件添加内联图像
【发布时间】:2021-08-21 03:22:16
【问题描述】:
<?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 邮件块的代码库,大多数变量都是从包含的其他脚本提供的以供阅读。我的代码有效,但是一个问题,发送到电子邮件时的图像会通过,但作为附件不在发送的电子邮件中,这不是我想要实现的,所以,我需要一个指南来让它工作。

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

【问题讨论】:

    标签: php sendgrid sendgrid-api-v3


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多