Search code examples
phpsendgridsendgrid-api-v3

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("[email protected]", "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";
    }
}
?>

The Code Above is a Codebase of my Current Sendgrid mailer chunk, Most variables are fed from other scripts included to be read. My code works, but a problem, image when sent to an email, goes through but as an attachment not inline within the email sent, and that's not what I want to achieve, so, I need a guide in getting it to work.

Expectation: Email sent, the receiver gets an email with the image within the email context, not as an attached image to the email.


Solution

  •      $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"
            )
        ];
    

    Encoding the Image file in the actual base64 function made it work. Although it's still showing as attachment file, but it's also reflecting within the email.