sendgrid php示例-如何添加发件人';她叫什么名字?

sendgrid php示例-如何添加发件人';她叫什么名字?,php,sendgrid,Php,Sendgrid,在sendgrid php示例中,您可以添加发件人的电子邮件,但不能添加发件人的姓名。比如说,如果发送者的名字被分配了变量$name,我如何才能正确地将它添加到这个例子中,使发送者的名字显示为,例如,Bob,而不仅仅是Bobbob@email.com? 谢谢 Per,设置From标题。对于SendGrid类,我认为可能是这样的: $mail->addHeader("From", $name); 答案是 setFromName($name) 内联图像的完整示例,look(查看cid:my

在sendgrid php示例中,您可以添加发件人的电子邮件,但不能添加发件人的姓名。比如说,如果发送者的名字被分配了变量$name,我如何才能正确地将它添加到这个例子中,使发送者的名字显示为,例如,Bob,而不仅仅是Bobbob@email.com?

谢谢

Per,设置From标题。对于SendGrid类,我认为可能是这样的:

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

答案是

setFromName($name)

内联图像的完整示例,look(查看cid:myimagecid是如何使用的)

$sg=new\SendGrid(“”);
$from=new SendGrid\Email(“某人”etc@example.com");
$subject=“Bla Bla”;
$to=新建SendGrid\Email(“其他人”dest@example.com");
$content=new SendGrid\content(“text/html”,“您的html”);
$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\u编码);
$attachment->setType(“图像/jpeg”);
$attachment->setContentID(“myimagecid”);
$attachment->setDisposition(“内联”);
$attachment->setFilename(“filename.jpg”);
$mail->addAttachment($attachment);
试一试{
$response=$sg->client->mail()->send()->post($mail);
}捕获(例外$e){
回显“捕获的异常:”,$e->getMessage(),“\n”;
}
var_dump($response);

使用模板时效果相同。

抱歉,我刚刚尝试过,但仍然只看到了电子邮件地址:(谢谢你的回答。如果你能在github repo上打开一个问题,这样我们就可以将其记录下来,那就太棒了!很乐意提供帮助,但根据OP的说法,这根本不起作用。你希望我在github上打开什么类型的问题?(注意:我现在没有费心测试我的或OPs的答案)。这看起来像是PHP API的早期版本。在当前版本中,您使用电子邮件地址设置名称,如以下示例所示:
PHP$mail->setFrom(“user@example.org“,”乔治·哈里森“;
$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);