【问题标题】:php - create ics file dynamically and email as attachmentphp - 动态创建 ics 文件并将电子邮件作为附件
【发布时间】:2015-04-20 22:51:15
【问题描述】:

我需要动态创建日历邀请并将 ics 文件作为附件通过电子邮件发送。我正在使用 sendgrid 发送电子邮件。

这是我目前的 php 脚本:

include("/Users/path/sendgrid-php.php");
$sendgrid = new SendGrid("uname", "pass");
$email    = new SendGrid\Email();

$ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@test.com
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:New event
END:VEVENT
END:VCALENDAR";

header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=invite.ics');

$email->addTo("test@test.com")
    ->setFrom("test2@test2.com")
    ->setSubject("Test-Subject")
    ->setHtml("Test-Body")
    ->setAttachment($ical);

$sendgrid->send($email);

现在,每当我运行此脚本时,我的浏览器不会将 ics 文件作为附件发送到所提供的电子邮件地址,而是自动下载 ics 文件,关闭页面,并且不执行任何操作(不发送电子邮件) .

你们能告诉我我哪里错了吗?我刚开始使用 PHP,所以很可能我犯了一些愚蠢的错误。

谢谢。

【问题讨论】:

  • 你试过删除header(…)这两条线吗?

标签: php google-calendar-api sendgrid


【解决方案1】:

您看到的行为是由您的header() 调用触发的。

header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=invite.ics');

这两行正在向客户端浏览器发送标头并触发下载。

SendGrid PHP examples 中,我根本看不到他们设置这样的标题。

【讨论】:

    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多