Sendgrid 在 body 上使用附件与 sendgrid-php

Sendgrid use attachment on body with sendgrid-php

我正在使用 sendgrid-php 库通过 Sendgrid 发送电子邮件,现在我想使用一些图像,但 GMail 拒绝打开它们,因为它们在服务器端。为了克服这种行为,我想将图像附加到电子邮件中,并将它们显示在电子邮件正文中。

这是我当前的代码:

require("./sendgrid-php/sendgrid-php.php");

$html = '
    <!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,300" rel="stylesheet" type="text/css">
</head>
<body style="background-color: #7bb241; padding: 10px 0;">
    <img alt="Projekt Fitness" height="56" src="http://domain/title.png" width="350" style="margin: 40px auto 0 auto; display: block;"/>
    <div style=\'width: 600px; margin: 40px auto; background-color: white; border-radius: 3px; padding: 20px; font-family: "Source Sans Pro", Helvetica, sans-serif; font-weight: 300; font-size: 18px;\'>
       <p>Hello <b>Jonh Doe</b>,</p>
        <p style="text-align: justify;">
         Thanks for subscribing! Soon you will receive our first newsletter!
        </p>
    </div>
</body>
</html>
';

$sendgrid = new SendGrid('myuser', 'mypwd');

$email = new SendGrid\Email();
$email
    ->addTo('arandomguy@gmail.com')
    ->setFrom('contato@domain')
    ->setFromName("Cool Newsletter")
    ->setSubject('Welcome to our newsletter')
    ->setHtml($html)
;

$sendgrid->send($email);

您可以使用图片标签上的内联 CID 来完成此操作。

...
<img src="cid:logo-cid"></div>
...

$sendgrid = new SendGrid('myuser', 'mypwd');

$email = new SendGrid\Email();
$email
    ->addTo('arandomguy@gmail.com')
    ->setFrom('contato@domain')
    ->setFromName("Cool Newsletter")
    ->setSubject('Welcome to our newsletter')
    ->setHtml($html)
    ->addAttachment('./path/to/logo.png', 'logo.png', 'logo-cid')
;

$sendgrid->send($email);

如果您还有其他问题,请告诉我!

如果您正在寻找内联图像,请查看(查看如何使用 cid:myimagecid

$sg = new \SendGrid("<YOUR API KEY>");
$from = new SendGrid\Email("Somebody", "etc@example.com");
$subject = "Bla bla";
$to = new SendGrid\Email("Somebody Else", "dest@example.com");
$content = new SendGrid\Content("text/html", 'Your html <img src="cid:myimagecid">');
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$file = 'path/file.jpg';
$file_encoded = base64_encode(file_get_contents($file));
$attachment = new SendGrid\Attachment();
$attachment->setContent($file_encoded);
$attachment->setType("image/jpeg");
$attachment->setContentID("myimagecid");
$attachment->setDisposition("inline");
$attachment->setFilename("filename.jpg");

$mail->addAttachment($attachment);

try {
    $response = $sg->client->mail()->send()->post($mail);
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}
var_dump($response);

使用模板时效果相同。

在较新版本的 sendgrid 中,上述方法不起作用,但以下方法起作用:

$path = './assets/logo.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);        
    
$email = new \SendGrid\Mail\Mail();     
$email->setFrom("youremail@gmail.com", "you");
$email->setSubject("Subject of your email");
$email->addTo("recipientemail@gmail.com", "recipient");
$email->addContent("text/html", "Content of your email in HTML");
$email->addAttachment(base64_encode($data), 'image/'.$type, 'logo.png' , 'inline' , 'logo-cid');        
        
$sendgrid = new \SendGrid(file_get_contents(getenv('SENDGRID_API_KEY')));        
try {
   $response = $sendgrid->send($email);            
   print $response->statusCode() . "\n";
   print_r($response->headers());
   print $response->body() . "\n";            
} catch (Exception $e) {
   echo 'Caught exception: '. $e->getMessage() ."\n";            
}         

基本上,解决方案是您必须将图像编码为 base64。 “SENDGRID_API_KEY”是我对“sengrid.env”的环境变量。