One-click export of events from email to Google Calendar - pitfalls

    image

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

    Based on the iCalendar format - this format is simple and is supported by many applications .

    There were no problems with the format itself. They simply added an attachment to the letter and the user of Outlook or Evolution (tested in these applications) can export data to his calendar with 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 (few people would do that).

    And here the fun began. Google displays a calendar icon for writing with an ics file attached, but the attachment can only be downloaded. But Gmail has the option “Insert invitation” and in this case the recipient will be sent an email with an attachment that can be imported into your Google calendar with one click. This is what we wanted to achieve. A search in Google itself did not give anything, I had to find on my own the reason that our attachment was not processed in the same way as an invitation letter created directly from Gmail.

    The code for attaching the file to the email 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 got 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 there is no Content-Disposition header, i.e. The attachment is added to the letter as inline. We set the Content-disposition: inline header in the email generated by our service, changed the Content-type of the attachment to indicate the file type, added method = REQUEST so that Outlook accepts the email as an invitation and “a miracle happened”: the link “Add to calendar ”, and Outlook began to display the letter as an invitation to an event.

    Solution for ZendFramework:
    $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 types of content / type are used in message parts.

    This is how it looks in Evolution:
    image

    And this is how it is in Outlook:
    image

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

    Also popular now: