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

时间:2015-06-16 19:24:46

标签: php sendgrid

我正在使用 <?php if (is_category('3')) : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php elseif (is_category('1096')) : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php elseif (is_category('1437')) : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php elseif (is_category('1150')) : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php elseif (is_category('1138')) : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php elseif (is_category('3')) : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php else : ?> <td class="logo-alts"><a href="/"><img src="<?php bloginfo('template_directory'); ?>/_images/image.png"></a></td> <?php endif; ?> lib通过Sendgrid发送电子邮件,现在我想使用一些图像,但GMail拒绝打开它们,因为它们在服务器端。为了克服这种情况,我想将图像附加到电子邮件中,并将它们显示在电子邮件的正文中。

这是我目前的代码:

sendgrid-php

3 个答案:

答案 0 :(得分:5)

您可以使用图片代码上的内联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);

如果您还有其他问题,请与我们联系!

答案 1 :(得分:0)

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

使用模板时工作原理相同。

答案 2 :(得分:0)

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