Back

Sending .ics event as email attachment


Osvalds posted 3 years ago

I'm trying to send an .ics file as an email attachment using Laravel's MAIL class. When email is received, Gmail doesn't ask me to add event to calendar. However I can easily add the event to the calendar by uploading the received file. (Format is valid.) And there is a small calendar icon as well, yet there is no fancy formatting or any formatting at all. Here's the code I'm using:

$filename = "invite.ics";
$data[0]  = "BEGIN:VCALENDAR";
$data[1] = "PRODID:-//Google Inc//Google Calendar 70.9054//EN";
$data[2] = "VERSION:2.0";
$data[3] = "CALSCALE:GREGORIAN";
$data[4] = "METHOD:REQUEST";
$data[8] = "BEGIN:VEVENT";
$data[9] = "DTSTART:20140312T080000Z";
$data[10] = "DTEND:20140312T103000Z";
$data[11] = "DTSTAMP:20140312T072230Z";
$data[12] = "UID:[email protected]";
$data[13] = "CREATED:20140312T072126Z";
$data[14] = "DESCRIPTION:Hair cut\, 2\,5h\, 300Eur";
$data[15] = "LAST-MODIFIED:20140312T072206Z";
$data[16] = "LOCATION:";
$data[17] = "SEQUENCE:0";
$data[18] = "STATUS:CONFIRMED";
$data[19] = "SUMMARY:Matu griešana";
$data[20] = "TRANSP:OPAQUE";
$data[21] = "END:VEVENT";
$data[22] = "END:VCALENDAR";


$data = implode("\r\n", $data);
header("text/calendar");
file_put_contents($filename, "\xEF\xBB\xBF".  $data);

Mail::send('emails.temp', array(), function($message) use($filename)
{
    $message->from('[email protected]', 'Jon Doe');
    $message->to('[email protected]')->subject('Registration information');

    $message->attach($filename, array('mime' => "text/calendar"));
});
davidtoledo replied 2 years ago

Do it this way, bro:

	\Mail::send('yourMail/file', $data, function($message) use($data)
	{
		$filename = "invite.ics";
		$meeting_duration = (3600 * 2); // 2 hours
		$meetingstamp = strtotime( $data['start_date'] . " UTC");
		$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().'@yourdomain.com';
		$description = strip_tags($data['texto']);
		$location = "Telefone ou vídeo conferência";
		$titulo_invite = "Your meeting title";
		$organizer = "CN=Organizer name:[email protected]";
		
		// ICS
		$mail[0]  = "BEGIN:VCALENDAR";
		$mail[1] = "PRODID:-//Google Inc//Google Calendar 70.9054//EN";
		$mail[2] = "VERSION:2.0";
		$mail[3] = "CALSCALE:GREGORIAN";
		$mail[4] = "METHOD:REQUEST";
		$mail[5] = "BEGIN:VEVENT";
		$mail[6] = "DTSTART;TZID=America/Sao_Paulo:" . $dtstart;
		$mail[7] = "DTEND;TZID=America/Sao_Paulo:" . $dtend;
		$mail[8] = "DTSTAMP;TZID=America/Sao_Paulo:" . $todaystamp;
		$mail[9] = "UID:" . $uid;
		$mail[10] = "ORGANIZER;" . $organizer;
		$mail[11] = "CREATED:" . $todaystamp;
		$mail[12] = "DESCRIPTION:" . $description;
		$mail[13] = "LAST-MODIFIED:" . $todaystamp;
		$mail[14] = "LOCATION:" . $location;
		$mail[15] = "SEQUENCE:0";
		$mail[16] = "STATUS:CONFIRMED";
		$mail[17] = "SUMMARY:" . $titulo_invite;
		$mail[18] = "TRANSP:OPAQUE";
		$mail[19] = "END:VEVENT";
		$mail[20] = "END:VCALENDAR";
		
		$mail = implode("\r\n", $mail);
		header("text/calendar");
		file_put_contents($filename, $mail);
		
		$message->subject($data['titulo']);
		$message->to($data['destinatario']);
	    $message->attach($filename, array('mime' => "text/calendar"));
	});

Regards,

David

vrajroham replied 8 months ago

@davidtoledo

Thank you for your snippet.

muralibobby35 replied 6 months ago

@davidtoledo

Thanks for your snippet. It's working in gmail but not working in outlook. I am getting 'The .ICS attachment can't be viewed because the format is not supported.' Please let me know where the problem is?

Danyal replied 5 months ago

@muralibobby35 and @davidtoledo

That'e exactly what I am doing through "t's working in gmail but not working in outlook", did you find the fix of this issue?

marcoacm replied 4 months ago

Did you guys find a way to make this work on outlook? I still get the .ics as an attachment instead of the buttons to accept or decline.. Please help


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us