【问题标题】:Sending Calendar invite via AWS SES - Error: Duplicate header 'Content-Type'通过 AWS SES 发送日历邀请 - 错误:重复标头“内容类型”
【发布时间】:2020-11-06 18:52:01
【问题描述】:

我正在尝试通过 AWS 简单电子邮件服务 (SES) 自动生成日历邀请并通过 SMTP 发送它们。发送是通过 PHPMailer 完成的,似乎可以与其他 SMTP 服务器(未经验证)一起使用,但不适用于 AWS。

似乎在通过 SES 发送时,它会触发亚马逊调整内容标题。不知道如何继续,感谢任何帮助,因为我必须使用亚马逊作为发送地址。

谢谢

错误:

SMTP ERROR: DATA END command failed: 554 Transaction failed: Duplicate header 'Content-Type'.

代码:

// PHPMailer Parameters
$cal_mail = new PHPMailer(true);
//$mail->isSMTP();                                                    // Send using SMTP
$cal_mail->SMTPDebug  = 1;                                              // Enable verbose debug output
$cal_mail->SMTPAuth   = $smtp_auth;                                     // Enable SMTP authentication
$cal_mail->SMTPSecure = 'tls';
$cal_mail->Host       = $smtp_host;                                     // Set the SMTP server to send through
$cal_mail->Port       = $smtp_port;
$cal_mail->SMTPKeepAlive = true;
$cal_mail->Mailer = "smtp";
$cal_mail->Username   = $smtp_username;                                 // SMTP username
$cal_mail->Password   = $smtp_password;                                 // SMTP password
$cal_mail->AddAddress($to_email, $name);                                // Add a recipient
$cal_mail->SetFrom($from_email, $from_name);
$cal_mail->AddReplyTo($to_email, $to_name);


$cal_mail->isHTML(false);                                               // calendar must be FALSE
$cal_mail->ContentType = 'text/calendar';
$cal_mail->Subject = "Outlook Event";
$cal_mail->addCustomHeader('MIME-version', "1.0");
$cal_mail->addCustomHeader('Content-type', "text/calendar; method=REQUEST; charset=UTF-8");
$cal_mail->addCustomHeader('Content-Transfer-Encoding', "7bit");
$cal_mail->addCustomHeader('X-Mailer', "Microsoft Office Outlook 12.0");
$cal_mail->addCustomHeader("Content-class: urn:content-classes:calendarmessage");

// Create Calendar Body
$ical = "BEGIN:VCALENDAR\r\n";
$ical .= "VERSION:2.0\r\n";
$ical .= "PRODID:-//BPNSolutions//SalesDept//EN\r\n";
$ical .= "METHOD:REQUEST\r\n";
$ical .= "BEGIN:VEVENT\r\n";
$ical .= "ORGANIZER;SENT-BY=\"MAILTO:noreply@mail.com\":MAILTO:noreply@mail.com\r\n";
$ical .= "ATTENDEE;CN=" . $to_email . ";ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE:MAILTO:" . $to_email . "\r\n";
$ical .= "UID:" . strtoupper(md5($event_id)) . "-bpn-solutions.com\r\n";
$ical .= "SEQUENCE:" . $sequence . "\r\n";
$ical .= "STATUS:" . $status . "\r\n";
$ical .= "DTSTAMP;TZID=Etc/UTC:" . date('Ymd') . 'T' . date('His') . "\r\n";
$ical .= "DTSTART:" . $start . "T" . $start_time . "\r\n";
$ical .= "DTEND:" . $end . "T" . $end_time . "\r\n";
$ical .= "LOCATION:" . $venue . "\r\n";
$ical .= "SUMMARY:" . $summary . "\r\n";
$ical .= "DESCRIPTION:" . $event['description'] . "\r\n";
$ical .= "BEGIN:VALARM\r\n";
$ical .= "TRIGGER:-PT15M\r\n";
$ical .= "ACTION:DISPLAY\r\n";
$ical .= "DESCRIPTION:Reminder\r\n";
$ical .= "END:VALARM\r\n";
$ical .= "END:VEVENT\r\n";
$ical .= "END:VCALENDAR\r\n";

编辑 1: 下面的代码成功生成了一封带有.ics 附件的电子邮件,但桌面版 MS Outlook 无法打开/阅读附件,在预览电子邮件时分别不会触发任何接受/拒绝按钮。不过,同样的附件在 Outlook / Apple Mail for iOS 上也能正常工作。

// Create Calendar Body
$ical = "BEGIN:VCALENDAR\r\n";
$ical .= "VERSION:2.0\r\n";
$ical .= "PRODID:-//Company//SalesDept//EN\r\n";
$ical .= "METHOD:REQUEST\r\n";
$ical .= "NAME: Test1\r\n";
$ical .= "X-WR-CALNAME:Test Calendar\r\n";
$ical .= "BEGIN:VEVENT\r\n";
$ical .= "ORGANIZER;CN=\"Company Name:mailto:noreply@domain.com\r\n";
$ical .= "ATTENDEE;CN=" . $to_email . ";ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE:MAILTO:" . $to_email . "\r\n";
$ical .= "UID:" . md5(uniqid(mt_rand(), true)) . "domain.com\r\n";
$ical .= "SEQUENCE:" . $sequence . "\r\n";
$ical .= "STATUS:" . $status . "\r\n";
$ical .= "DTSTART:" . $start . "T" . $start_time . "Z\r\n";
$ical .= "DTEND:" . $end . "T" . $end_time . "Z\r\n";
$ical .= "DTSTAMP:" . date('Ymd') . 'T' . date('His') . "Z\r\n";
$ical .= "LOCATION:" . $venue . "\r\n";
$ical .= "SUMMARY:" . $summary . "\r\n";
$ical .= "DESCRIPTION:Some descriptive text here.\r\n";
$ical .= "BEGIN:VALARM\r\n";
$ical .= "TRIGGER:-PT15M\r\n";
$ical .= "ACTION:DISPLAY\r\n";
$ical .= "DESCRIPTION:Reminder\r\n";
$ical .= "END:VALARM\r\n";
$ical .= "END:VEVENT\r\n";
$ical .= "END:VCALENDAR\r\n";

$cal_mail->ContentType = 'text/calendar';
$cal_mail->Subject = $summary;
$cal_mail->Body = "This is the body text which is meant to be shown in the email body";
$cal_mail->AltBody = $ical;
$cal_mail->Ical = $ical;

【问题讨论】:

    标签: php phpmailer amazon-ses


    【解决方案1】:

    停在那里。你试图破坏 PHPMailer 为你做的所有事情。如果您打算尝试从头开始构建自己的消息,则不需要 PHPMailer。 PHPMailer 具有对发送 iCal 附件和消息部分的内置支持,因此请删除所有这些 addCustomHeader 调用。发送仅包含 ical 部分的消息不太可能普遍适用(有很多 discussion related to the problems in various ical clients - 简短版本:gmail 和 Outlook 都非常糟糕)所以设置一个消息正文,然后添加你的 iCal创建 $ical 字符串后的部分:

    $cal_mail->Body = 'Here is your calendar invitation';
    $cal_mail->Ical = $ical;
    

    仅此而已。所有的标头和编码都会为您处理好。

    要调试,你真的很想拥有SMTPDebug = 2; 1 用处不大。也不要直接设置Mailer;请致电isSMTP()

    【讨论】:

    • 谢谢。现在会生成邀请和 .ics 附件,但 Outlook 无法打开邀请。谷歌引用了兼容性问题,对此有什么建议吗?谢谢
    • Outlook 对 iCal 格式非常挑剔。我建议在尝试自己制作之前使用由 Outlook 生成的 iCal 内容进行测试。
    • 上述$ical 不会触发桌面版 Outlook 中的接受/拒绝按钮,但在 iOS 上可以正常工作。但是在查看 Outlook cal 邀请的来源时,我根本看不到日历详细信息...?
    • 所以你明白我的意思是挑剔!我基本上已经放弃了它,因为我认为它不可能同时在所有 Outlook、gmail 和 iOS 中工作。只要 ics 附件是可见的,它就可以在任何地方使用。
    • 看看我在下面发布的答案,它现在适用于 Mac 和 PC 上的 Outlook 桌面,以及 iOS 和 Android 上的移动设备。
    【解决方案2】:

    经过大量试验和错误并让它在桌面或移动设备上运行后,我终于找到了$ical 元素的正确组合,让接受/拒绝按钮出现在 MS Outlook for Desktop 和 Outlook for iOS 和苹果邮件。请注意,以下不适用于 Gmail

    // Create Calendar Body
    $ical = "BEGIN:VCALENDAR\r\n";
    $ical .= "VERSION:2.0\r\n";
    $ical .= "PRODID:-//CompanyName//SalesDept//EN\r\n";
    $ical .= "METHOD:REQUEST\r\n";
    $ical .= "NAME: Test1\r\n";
    $ical .= "X-WR-CALNAME:Test Cal\r\n";
    $ical .= "BEGIN:VEVENT\r\n";
    $ical .= "ORGANIZER;CN=" . $from_name . ":mailto:" . $from_email . "\r\n";
    $ical .= "ATTENDEE;CN=" . $to_email . ";ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE:MAILTO:" . $to_email . "\r\n";
    $ical .= "UID:" . md5(uniqid(mt_rand(), true)) . "mydomain.com\r\n";
    $ical .= "SEQUENCE:" . $sequence . "\r\n";
    $ical .= "STATUS:" . $status . "\r\n";
    $ical .= "DTSTART:" . $start . "T" . $start_time . "Z\r\n";
    $ical .= "DTEND:" . $end . "T" . $end_time . "Z\r\n";
    $ical .= "DTSTAMP:" . date('Ymd') . 'T' . date('His') . "Z\r\n";
    $ical .= "LOCATION:" . $venue . "\r\n";
    $ical .= "SUMMARY:" . $summary . "\r\n";
    $ical .= "DESCRIPTION:Some descriptive text here.\r\n";
    $ical .= "BEGIN:VALARM\r\n";
    $ical .= "TRIGGER:-PT15M\r\n";
    $ical .= "ACTION:DISPLAY\r\n";
    $ical .= "DESCRIPTION:Reminder\r\n";
    $ical .= "END:VALARM\r\n";
    $ical .= "END:VEVENT\r\n";
    $ical .= "END:VCALENDAR\r\n";
    
    $cal_mail->ContentType = 'text/calendar';
    $cal_mail->Subject = $summary;
    $cal_mail->Body = "This is the body text";
    $cal_mail->AltBody = $ical;
    $cal_mail->AddStringAttachment("$ical", "attachment.ics", "base64", "text/calendar; charset=utf-8; method=REQUEST");
    $cal_mail->Ical = $ical;
    

    值得指出的是,MS Outlook for Desktop 没有正确显示日历邀请,直到我添加了以下代码,将.ics 附件附加到邮件中(即使 PHPMailer 应该自动执行此操作)。

    感谢Frank Van Der Oort 指出这一点here

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 2017-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 2021-12-08
      • 2021-07-21
      相关资源
      最近更新 更多