我正在使用sendgrid-phplib通过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);
Run Code Online (Sandbox Code Playgroud)
小智 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);
Run Code Online (Sandbox Code Playgroud)
如果您还有其他问题,请与我们联系!