sendgrid php示例 - 如何添加发件人姓名?

时间:2013-02-08 00:25:29

标签: php sendgrid

在sendgrid php示例http://sendgrid.com/docs/Code_Examples/php.html中,您可以添加发件人的电子邮件,但不能添加发件人的姓名。因此,例如,如果发件人的名称被赋予变量$ name,我该如何正确地将其添加到示例中,以便将发件人的名称显示为,例如,Bob而不仅仅是bob@email.com?

谢谢!

3 个答案:

答案 0 :(得分:6)

原来答案是

setFromName($name)

答案 1 :(得分:4)

the documentation on the php mail function设置 From 标头。使用SendGrid类,我认为它可能是这样的:

$mail->addHeader("From", $name);

答案 2 :(得分: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);

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