【问题标题】:Sendgrid use attachment on body with sendgrid-phpSendgrid 通过 sendgrid-php 在正文上使用附件
【发布时间】:2015-06-16 19:24:46
【问题描述】:

我正在使用sendgrid-php lib 通过 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);

【问题讨论】:

    标签: php sendgrid


    【解决方案1】:

    您可以使用图像标签上的内联 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);
    

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

    【讨论】:

    • 感谢您快速准确的回复!
    【解决方案2】:

    如果您正在寻找内嵌图像,请查看(查看 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);
    

    使用模板时效果相同。

    【讨论】:

      【解决方案3】:

      在更新版本的 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”的环境变量。

      【讨论】:

        猜你喜欢
        • 2016-08-05
        • 1970-01-01
        • 2014-05-02
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        • 2016-11-09
        相关资源
        最近更新 更多