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:
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?