如何在sendgrid中添加headers?
How to add headers in sendgrid?
我正在尝试使用 php 和 sendgrid 在 outlook 上发送日历邀请。所以我需要创建一个 ics 文件,这不是问题。问题是我需要设置 headers。 Gmail 将 ics 文件识别为日历邀请,但 outlook 不识别。这是我想出的完整代码,但我无处可去。请帮忙。我搜索了每个博客,以了解如何在 sendgrid 中添加 headers,例如 content-type 和 content-disposition,但无济于事。
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
include("/Users/aaa/Downloads/sendgrid-php/sendgrid-php.php");
include('/Users/aaa/Downloads//smtpapi-php/smtpapi-php.php');
$sendgrid = new SendGrid("uname", "pass");
$email = new SendGrid\Email();
$ical = "
Content-Type: text/calendar;method=request
MIME-Version: 1.0
BEGIN:VCALENDAR
METHOD:REQUEST
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@time.co
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20150429T170000Z
DTEND:20150429T035959Z
SUMMARY:New event has been added
END:VEVENT
END:VCALENDAR";
$filename = "invite.ics";
$file = fopen($filename, 'w');
fwrite($file, $ical);
fclose($file);
$email->addTo("aaa@outlook.com")
->setFrom("aaa@example.com")
->setSubject("Subject")
->setAttachment($filename)
->addHeader('Content-Type', 'multipart/alternative')
->addHeader('Content-Disposition', 'inline');
$sendgrid->send($email);
var_dump($sendgrid);
try {
$sendgrid->send($email);
} catch(\SendGrid\Exception $e) {
echo $e->getCode();
foreach($e->getErrors() as $er) {
echo $er;
}
}
?>
</body>
</html>
不幸的是,这是当前网络端点的限制。对于此用例,您需要通过 SMTP 而不是 HTTP 发送。如果您正在使用 smtpapi-php
库来构建您的 X-SMTPAPI headers。然后使用您选择的库构建您的 SMTP 消息,添加您的自定义 headers(如果需要,包括 X-SMTPAPI),然后发送它。
Example using Swift Mailer as the SMTP transport
use Smtpapi\Header;
$transport = \Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
$transport->setUsername('sendgrid_username');
$transport->setPassword('sendgrid_password');
$mailer = \Swift_Mailer::newInstance($transport);
$message = new \Swift_Message();
$message->setTos(array('bar@blurdybloop.com'));
$message->setFrom('foo@blurdybloop.com');
$message->setSubject('Hello');
$message->setBody('%how% are you doing?');
$header = new Header();
$header->addSubstitution('%how%', array('Owl'));
$message_headers = $message->getHeaders();
$message_headers->addTextHeader(HEADER::NAME, $header->jsonString());
try {
$response = $mailer->send($message);
print_r($response);
} catch(\Swift_TransportException $e) {
print_r('Bad username / password');
}
我正在尝试使用 php 和 sendgrid 在 outlook 上发送日历邀请。所以我需要创建一个 ics 文件,这不是问题。问题是我需要设置 headers。 Gmail 将 ics 文件识别为日历邀请,但 outlook 不识别。这是我想出的完整代码,但我无处可去。请帮忙。我搜索了每个博客,以了解如何在 sendgrid 中添加 headers,例如 content-type 和 content-disposition,但无济于事。
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
include("/Users/aaa/Downloads/sendgrid-php/sendgrid-php.php");
include('/Users/aaa/Downloads//smtpapi-php/smtpapi-php.php');
$sendgrid = new SendGrid("uname", "pass");
$email = new SendGrid\Email();
$ical = "
Content-Type: text/calendar;method=request
MIME-Version: 1.0
BEGIN:VCALENDAR
METHOD:REQUEST
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@time.co
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20150429T170000Z
DTEND:20150429T035959Z
SUMMARY:New event has been added
END:VEVENT
END:VCALENDAR";
$filename = "invite.ics";
$file = fopen($filename, 'w');
fwrite($file, $ical);
fclose($file);
$email->addTo("aaa@outlook.com")
->setFrom("aaa@example.com")
->setSubject("Subject")
->setAttachment($filename)
->addHeader('Content-Type', 'multipart/alternative')
->addHeader('Content-Disposition', 'inline');
$sendgrid->send($email);
var_dump($sendgrid);
try {
$sendgrid->send($email);
} catch(\SendGrid\Exception $e) {
echo $e->getCode();
foreach($e->getErrors() as $er) {
echo $er;
}
}
?>
</body>
</html>
不幸的是,这是当前网络端点的限制。对于此用例,您需要通过 SMTP 而不是 HTTP 发送。如果您正在使用 smtpapi-php
库来构建您的 X-SMTPAPI headers。然后使用您选择的库构建您的 SMTP 消息,添加您的自定义 headers(如果需要,包括 X-SMTPAPI),然后发送它。
Example using Swift Mailer as the SMTP transport
use Smtpapi\Header;
$transport = \Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
$transport->setUsername('sendgrid_username');
$transport->setPassword('sendgrid_password');
$mailer = \Swift_Mailer::newInstance($transport);
$message = new \Swift_Message();
$message->setTos(array('bar@blurdybloop.com'));
$message->setFrom('foo@blurdybloop.com');
$message->setSubject('Hello');
$message->setBody('%how% are you doing?');
$header = new Header();
$header->addSubstitution('%how%', array('Owl'));
$message_headers = $message->getHeaders();
$message_headers->addTextHeader(HEADER::NAME, $header->jsonString());
try {
$response = $mailer->send($message);
print_r($response);
} catch(\Swift_TransportException $e) {
print_r('Bad username / password');
}