📜 ⬆️ ⬇️

Export events from email to Google Calendar in one click - pitfalls

image

As one of the capabilities of the client pre-registration service, zabroniruy.com implemented the idea that the client received all the information about upcoming events in a format that can be easily exported (preferably with a single click) to existing task schedulers. In the course of the work, we faced the fact that not all planners are equally “friendly” to the developers.

The iCalendar format was taken as a basis - this format is simple and is supported by many applications .

There were no problems with the format itself. Just added an attachment to the email and the Outlook or Evolution user (tested in these applications) can export the data to your calendar in one click. But we wanted Gmail users to be able to just as easily add events to their Google calendar, instead of downloading the file and then exporting it to the calendar (very few people will do this).
')
And here the most interesting began. Google displays a calendar icon for the letter with an ics file attached, but the attachment can only be downloaded. But in Gmail there is an option “Insert Invitation” and in this case, the recipient will receive an email with an attachment that can be imported into your Google calendar in one click. This is what we wanted to achieve. Google’s search didn’t give anything, we had to search on our own for the reason that our attachment isn’t processed in the same way as the invitation letter created directly from Gmail.

The code for attaching a file to a letter at this moment looked like this:
$mailer->createAttachment( $data, Zend_Mime::TYPE_OCTETSTREAM, Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, 'icalendar-file.ics' ); 


Before we received the first workable version, we tried different approaches, but the solution, as often happens, turned out to be very simple. After examining the invitation letter from Gmail, we noticed that the Content-Disposition title was missing, i.e. attachment added to the letter as inline. We set the Content-disposition: inline header in the letter generated by our service, change the Content-type of the attachment to indicate the file type, add method = REQUEST so that Outlook accepts the letter as an invitation and “a miracle happened”: the link “Add to the calendar ”, and Outlook began to display the letter as an invitation to an event.

ZendFramework Solution:
 $mailer->createAttachment( $data, 'text/calendar; charset=UTF-8; method=REQUEST', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, 'icalendar-file.ics' ); $mailer->setType(Zend_Mime::MULTIPART_ALTERNATIVE); 

multipart / alternative - indicates that different content / type types are used in parts of the message.

This is what Evolution looks like:
image

And so in Outlook:
image

The author of the solution, our programmer Dmitry, unfortunately, does not have an account in Habré, so I will answer the questions.

Source: https://habr.com/ru/post/115276/


All Articles