如何让 .ics 为 Outlook(Laravel 邮件程序)工作?

How to get .ics to work for Outlook (Laravel mailer)?

我正在尝试让 Outlook 将我电子邮件的 .ics 文件识别为正确的邀请。 .ics 日历邀请在 Gmail 中工作得很好,只是在 Outlook 中不行。通过阅读其他 Whosebug 帖子,我的猜测是我没有正确设置“MIME”等,但我不知道该怎么做(我对电子邮件有点新手)。

这是我的基本代码片段。

Mail::send($data['view'], $data['data'], function($message) use ($data)  {
    $message->from($from_email,$f_n)->to($to_email)->subject($data['subject']);
    $message->attachData($data['ical'], 'invite.ics');
});

我试过这样的代码:

$message->setContentType("text/calendar;method=REQUEST;name=\"invite.ics\"");
$disp->addParameterizedHeader(
    'Content-Disposition', 'x'
);
$disp = $message->getHeaders()->get('Content-Disposition');
$disp->setValue('attachment');
$disp->setParameter('filename', 'invite.ics');

$message->setBody($data['ical'], 'text/calendar; charset="utf-8"; method=REQUEST');

但到目前为止运气不好。 我正在使用 Laravel 5.5.

为了支持问题与 .ics 文件本身无关的观点,我尝试使用标准 Google 日历生成的邀请 .ics,当通过来自 Google 日历的自动电子邮件,但当我通过我的 Mail::send.

发送时却没有

提前感谢您的任何建议!

我最终为 Laravel 4 应用程序所做的是扩展邮件程序,然后将 MailServiceProvider 换成我自己的,添加一个额外的方法invite:

public function invite($view, array $data, $callback)
{
    // First we need to parse the view, which could either be a string or an array
    // containing both an HTML and plain text versions of the view which should
    // be used when sending an e-mail. We will extract both of them out here.
    list($view, $plain) = $this->parseView($view);

    $data['message'] = $message = $this->createMessage();

    $ts = \Carbon\Carbon::now();

    $filename = "invite.ics";
    $meeting_duration = (60 * 60); // 5 minutes
    $meetingstamp = $ts->getTimeStamp();
    $dtstart = gmdate('Ymd\THis\Z', $meetingstamp);
    $dtend =  gmdate('Ymd\THis\Z', $meetingstamp + $meeting_duration);
    $todaystamp = gmdate('Ymd\THis\Z');
    $uid = date('Ymd').'T'.date('His').'-'.rand();
    $description = $data['event']['description'];
    $location = $data['event']['location'];

    $organizer = "Organizer name:" . $data['event']['organizername'];
    $organizerEmail = 'noreply@email.com';

    //Including additional headers
    $typemime = $message->getHeaders()->get("MIME-version");
    $typemime->setValue("1.0");
    $type = $message->getHeaders()->get("Content-Type");
    $type->setValue("text/calendar");
    $type->setParameters(array(
        "name" => "calendar.ics",
        "method" => "REQUEST",
        "charset" => "iso-8859-1"
    ));

    $typetrans = $message->getHeaders()->get("Content-Transfer-Encoding");
    $typetrans->setValue("7bit");
    $message->getHeaders()->addTextHeader("X-Mailer", "Microsoft Office Outlook 12.0");

    //vCalendar parameters
    $vcal = "BEGIN:VCALENDAR\r\n";
    $vcal .= "VERSION:2.0\r\n";
    $vcal .= "PRODID:-//yourdomain.com//OrgCalendarWebTool//EN\r\n";
    $vcal .= "METHOD:REQUEST\r\n";
    $vcal .= "BEGIN:VEVENT\r\n";
    $vcal .= "ORGANIZER;CN=\"$organizer"."\":mailto:$organizerEmail\r\n";
    $vcal .= "UID:".$uid;
    $vcal .= "DTSTAMP:".date('Ymd').'T'.date('His')."\r\n";
    $vcal .= "DTSTART:$dtstart\r\n";
    $vcal .= "DTEND:$dtend\r\n"; 
    $vcal .= "LOCATION:$location\r\n";
    $vcal .= "SUMMARY:$description\r\n";
    $vcal .= "DESCRIPTION:$description \r\n";
    $vcal .= "BEGIN:VALARM\r\n";
    $vcal .= "TRIGGER:-PT15M\r\n";
    $vcal .= "ACTION:DISPLAY\r\n";
    $vcal .= "DESCRIPTION:Reminder\r\n";
    $vcal .= "END:VALARM\r\n";
    $vcal .= "END:VEVENT\r\n";
    $vcal .= "END:VCALENDAR\r\n";

    //Adding the parameters (This part Im not sure how to do it)
    $message->addPart($vcal, 'text/calendar; method=REQUEST', 'iso-8859-1');

    $this->callMessageBuilder($callback, $message);

    // Once we have retrieved the view content for the e-mail we will set the body
    // of this message using the HTML type, which will provide a simple wrapper
    // to creating view based emails that are able to receive arrays of data.
    $this->addContent($message, $view, $plain, $data);

    $message = $message->getSwiftMessage();

    return $this->sendSwiftMessage($message);
}

这样使用就简单多了:

// send a calendar invite
Mail::invite(...
// send a regular email
Mail::send(...

Laravel 5 添加了一些新功能,例如邮件,因此我建议您查看源代码以确定您可能需要什么。

邮件来源:https://github.com/laravel/framework/blob/5.5/src/Illuminate/Mail/Mailer.php

我不记得在哪里找到 vCalendar 参数的语法。如果我找到它,我会把它添加到这里。

我刚刚发现您可以将 mime 参数添加到 attachData 函数(根据 https://laravel.com/docs/5.7/mail#attachments)- 这适用于 Outlook 和 Gmail。耶:)

$message->attachData($data['ical'], 'invite.ics', [
                    'mime' => 'text/calendar;charset=UTF-8;method=REQUEST',
                ]);