dongquan6030 2015-04-28 11:54
浏览 89

Outlook接受/拒绝按钮不会显示给Icalendar

I want to send calendar invitations from my Laravel application. I want these invitations to show the accept / decline / propose new time / maybe buttons inside Outlook, but for some reason I can't get them to show up

I need this because I want to achieve this:

Calendar invitation

My code:

$icalendar = IcalendarUtil::genEvent(
    array('Me', 'me@example.com'),
    new DateTime(),
    null,
    array(
        'example@example.com' => 'John Doe'
    ),
    'Test subject',
    'Test location',
    'Test Description',
    true,
    true
);

Mail::send('nada', array(), function($message) use($icalendar)
{
    $message->from('example@example.com', 'John Doe');
    $message->to('me@example.com')->subject('Test Calendar Event');

    $attachment = Swift_Attachment::newInstance($icalendar, 'event.ics', 'text/calendar');

    $attachment->getHeaders()->addTextHeader('Content-Type', 'text/calendar');
    $attachment->getHeaders()->addTextHeader('Content-Transfer-Encoding', '7bit');
    $attachment->getHeaders()->addTextHeader('X-Mailer', 'Microsoft Office Outlook 12.0');
    $attachment->getHeaders()->addTextHeader('method', 'REQUEST');
    $attachment->getHeaders()->addTextHeader('charset', 'iso-8859-1');

    $message->attachAttachment($attachment);
});

$message->attachAttachment($attachment); is a function I added to Illuminate\Mail\Message.php to add a Swift_Attachment created with newInstance. Code:

public function attachAttachment($attachment)
{
    $this->prepAttachment($attachment);
}

The IcalendarUtil class was provided to me by Arun Poudel:

<?php

/**
 * iCalendar util
 */
class IcalendarUtil
{

/**
 * Generates a iCalendar event.
 *
 * @param array $organizer
 * @param DateTime $from_date
 * @param DateTime $to_date
 * @param array $attendees
 * @param string $subject
 * @param string $location
 * @param string $description
 * @param bool $all_day_event
 * @param bool $rsvp
 * @return string
 * @static
 * @throws cbmException
 */
static public function genEvent(array $organizer, DateTime $from_date, DateTime $to_date = null, array $attendees = null, $subject = null, $location = null, $description = null, $all_day_event = false, $rsvp = true)
{
    if (!$all_day_event && $to_date === null)
    {
        throw new Exception('to_date is required when the event is not an all day event');
    }
    $organizer_name = $organizer[0];
    $ical = "BEGIN:VCALENDAR
";
    $ical .= "VERSION:2.0
";
    $ical .= "PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN
";
    $ical .= "METHOD:REQUEST
";
    $ical .= "X-MS-OLK-FORCEINSPECTOROPEN:TRUE
";
    $ical .= "BEGIN:VTIMEZONE
";
    $ical .= sprintf("TZID:%s
", date('T'));
    $ical .= "BEGIN:STANDARD
";
    $ical .= "DTSTART:16010101T000000
";
    $ical .= "TZOFFSETFROM:+0545
";
    $ical .= "TZOFFSETTO:+0545
";
    $ical .= "END:STANDARD
";
    $ical .= "END:VTIMEZONE
";
    $ical .= "BEGIN:VEVENT
";
    $ical .= sprintf("UID:%s
", rand());

    $ical .= sprintf("ORGANIZER;CN=\"%s\":MAILTO:%s
", $organizer_name, $organizer[1]);
    foreach ($attendees as $email => $name)
    {
        $ical .= sprintf("ATTENDEE;");
        if ($name !== null)
        {
            $ical .= sprintf("CN=\"%s\";", $name);
        }
        if($rsvp)
        {
            $ical .= sprintf("RSVP=TRUE:");
        }
        $ical .= sprintf("mailto:%s;
", $email);
    }
    $ical .= sprintf("LOCATION:%s
", $location);
    $ical .= sprintf("DTSTAMP:%s
", self::getDateTimeInUTCFormat());
    $ical .= "CLASS:PUBLIC
";
    $ical .= sprintf("DTSTART:%s
", self::getDateTimeInUTCFormat($from_date));
    if (!$all_day_event)
    {
        $ical .= sprintf("DTEND:%s
", self::getDateTimeInUTCFormat($to_date));
    }
    $ical .= sprintf("SUMMARY:%s
", $subject);
    $ical .= "TRANSP:OPAQUE
";
    $ical .= "X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
";
    $ical .= "X-MICROSOFT-CDO-IMPORTANCE:1
";
    $ical .= "X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY
";
    $ical .= "X-MICROSOFT-DISALLOW-COUNTER:FALSE
";
    $ical .= "X-MS-OLK-APPTLASTSEQUENCE:1
";
    $ical .= "X-MS-OLK-AUTOSTARTCHECK:FALSE
";
    $ical .= "X-MS-OLK-CONFTYPE:0
";
    $ical .= sprintf("X-MS-OLK-SENDER;CN=\"%s\":MAILTO:%s
", $organizer_name, $organizer[1]);
    $ical .= sprintf("X-ALT-DESC;FMTTYPE=text/html:%s
", "<p>" . preg_replace('/\R/', "</p><p>", $description) . "</p>");
    $ical .= sprintf("X-MS-OLK-CONFTYPE:0
");
    $ical .= "BEGIN:VALARM
";
    $ical .= "TRIGGER:-PT15M
";
    $ical .= "ACTION:DISPLAY
";
    $ical .= "DESCRIPTION:Reminder
";
    $ical .= "END:VALARM
";
    $ical .= "END:VEVENT
";
    $ical .= "END:VCALENDAR";
    return $ical;
}

static protected function getDateTimeInUTCFormat(DateTime $date = null)
{
    if ($date === null)
    {
        $date = new DateTime();
    }
    $date->setTimezone(new DateTimeZone('UTC'));
    return $date->format('Ymd\THis\Z');
}

}

The email shows up simply as an email with a .ics attachment. What am I doing wrong? Why are the RSVP buttons not showing up?

  • 写回答

2条回答 默认 最新

  • duanheyi7147 2015-04-28 12:01
    关注

    You wouldn't want to set the type of the whole email as text/calendar. You might need to add email body as well. So, my suggestion will be to attach the calendar and then set the mine type:

    $message->attach('something.ics', array('mime' => 'text/calendar'))
    

    EDIT:

    Use this class to generate your iCal

    <?php
    
    /**
     * iCalendar util
     */
    class IcalendarUtil
    {
    
        /**
         * Generates a iCalendar event.
         *
         * @param array $organizer
         * @param DateTime $from_date
         * @param DateTime $to_date
         * @param array $attendees
         * @param string $subject
         * @param string $location
         * @param string $description
         * @param bool $all_day_event
         * @param bool $rsvp
         * @return string
         * @static
         * @throws cbmException
         */
        static public function genEvent(array $organizer, DateTime $from_date, DateTime $to_date = null, array $attendees = null, $subject = null, $location = null, $description = null, $all_day_event = false, $rsvp = true)
        {
            if (!$all_day_event && $to_date === null)
            {
                throw new Exception('to_date is required when the event is not an all day event');
            }
            $organizer_name = $organizer[0];
            $ical = "BEGIN:VCALENDAR
    ";
            $ical .= "VERSION:2.0
    ";
            $ical .= "PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN
    ";
            $ical .= "METHOD:REQUEST
    ";
            $ical .= "X-MS-OLK-FORCEINSPECTOROPEN:TRUE
    ";
            $ical .= "BEGIN:VTIMEZONE
    ";
            $ical .= sprintf("TZID:%s
    ", date('T'));
            $ical .= "BEGIN:STANDARD
    ";
            $ical .= "DTSTART:16010101T000000
    ";
            $ical .= "TZOFFSETFROM:+0545
    ";
            $ical .= "TZOFFSETTO:+0545
    ";
            $ical .= "END:STANDARD
    ";
            $ical .= "END:VTIMEZONE
    ";
            $ical .= "BEGIN:VEVENT
    ";
            $ical .= sprintf("UID:%s
    " . rand());
    
            $ical .= sprintf("ORGANIZER;CN=\"%s\":MAILTO:%s
    ", $organizer_name, $organizer[1]);
            foreach ($attendees as $email => $name)
            {
                $ical .= sprintf("ATTENDEE;");
                if ($name !== null)
                {
                    $ical .= sprintf("CN=\"%s\";", $name);
                }
                if($rsvp)
                {
                    $ical .= sprintf("RSVP=TRUE:");
                }
                $ical .= sprintf("mailto:%s;
    ", $email);
            }
            $ical .= sprintf("LOCATION:%s
    ", $location);
            $ical .= sprintf("DTSTAMP:%s
    ", self::getDateTimeInUTCFormat());
            $ical .= "CLASS:PUBLIC
    ";
            $ical .= sprintf("DTSTART:%s
    ", self::getDateTimeInUTCFormat($from_date));
            if (!$all_day_event)
            {
                $ical .= sprintf("DTEND:%s
    ", self::getDateTimeInUTCFormat($to_date));
            }
            $ical .= sprintf("SUMMARY:%s
    ", $subject);
            $ical .= "TRANSP:OPAQUE
    ";
            $ical .= "X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
    ";
            $ical .= "X-MICROSOFT-CDO-IMPORTANCE:1
    ";
            $ical .= "X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY
    ";
            $ical .= "X-MICROSOFT-DISALLOW-COUNTER:FALSE
    ";
            $ical .= "X-MS-OLK-APPTLASTSEQUENCE:1
    ";
            $ical .= "X-MS-OLK-AUTOSTARTCHECK:FALSE
    ";
            $ical .= "X-MS-OLK-CONFTYPE:0
    ";
            $ical .= sprintf("X-MS-OLK-SENDER;CN=\"%s\":MAILTO:%s
    ", $organizer_name, $organizer[1]);
            $ical .= sprintf("X-ALT-DESC;FMTTYPE=text/html:%s
    ", "<p>" . preg_replace('/\R/', "</p><p>", $description) . "</p>");
            $ical .= sprintf("X-MS-OLK-CONFTYPE:0
    ");
            $ical .= "BEGIN:VALARM
    ";
            $ical .= "TRIGGER:-PT15M
    ";
            $ical .= "ACTION:DISPLAY
    ";
            $ical .= "DESCRIPTION:Reminder
    ";
            $ical .= "END:VALARM
    ";
            $ical .= "END:VEVENT
    ";
            $ical .= "END:VCALENDAR";
            return $ical;
        }
    
        static protected function getDateTimeInUTCFormat(DateTime $date = null)
        {
            if ($date === null)
            {
                $date = new DateTime();
            }
            $date->setTimezone(new DateTimeZone('UTC'));
            return $date->format('Ymd\THis\Z');
        }
    
    }
    

    Also, you might need to set some custom headers in the email.

    encoding = '7bit'
    header = 'Microsoft Office Outlook 12.0' // Not sure if this is needed, but we might need to fool microsoft
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码