SendGrid PHP示例 - 如何添加发件人姓名?

5
在这个sendgrid php示例中http://sendgrid.com/docs/Code_Examples/php.html,您可以添加发件人的电子邮件地址,但无法添加发件人的姓名。例如,如果将发送者的名称分配给变量$name,那么该怎么正确地将其添加到示例中,以便显示发送者的名称,例如Bob而不仅仅是bob@email.com?
谢谢!
3个回答

7
原来的答案是:
setFromName($name)

这看起来像是早期版本的PHP API。在当前版本中,您可以使用电子邮件地址设置名称,如以下示例:$mail->setFrom("user@example.org", "George Harrison"); - Matthew Setter

4
根据php邮件函数的文档,设置发件人头部。使用SendGrid类,可能是这样的:
$mail->addHeader("From", $name);

抱歉,我刚刚尝试了一下,仍然只能看到电子邮件地址 :( - AZhu
感谢您的回答。如果您能在Github存储库上开个问题,我们就可以将其记录下来! - Swift
很高兴能帮忙,但根据 OP 的说法,这甚至都没有起作用。您想让我在 Github 上开什么类型的问题?(注意:此时我还没有测试我的或 OP 的答案)。 - quickshiftin

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);

使用模板时,功能相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接