Sending .ics File Using MailGun API

73 Views Asked by At

This is how I wan to display my .ics calendar invite.

I am attempting to send a calendar invite using the MailGun API. The content of my .ics calendar works correctly for Gmail, but it doesn't display properly for Outlook. How can I ensure that my .ics calendar content displays correctly for Outlook users when sent via the MailGun API?

This is my complete code using PHP

This is my Ics file which I am trying to send as an attachment

$ics_content = "BEGIN:VCALENDAR\r\n"
            . "VERSION:2.0\r\n"
            . "PRODID:-//Service Provider Inc//NONSGML kigkonsult.se iCalcreator 2.18//\r\n"
            . "CALSCALE:GREGORIAN\r\n"
            . "METHOD:REQUEST\r\n"
            . "X-WR-TIMEZONE:$timezone\r\n"
            . "BEGIN:VEVENT\r\n"
            . "UID:$uid\r\n"
            . "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=$fname $lname:MAILTO:$email\r\n"
            . "DESCRIPTION:$ical_desc\r\n"
            . "DTSTART:$dateFrom\r\n"
            . "DTEND:$dateTo\r\n"
            . "DTSTAMP:$currentDate\r\n"
            . "LOCATION:$location\r\n"
            . "ORGANIZER;CN=$organizer_name:MAILTO:$replyTo\r\n"
            . "SEQUENCE:0\r\n"
            . "SUMMARY:$summary\r\n"
            . "BEGIN:VALARM\r\n"
            . "ACTION:DISPLAY\r\n"
            . "DESCRIPTION:REMINDER\r\n"
            . "TRIGGER;RELATED=START:-PT00H15M00S\r\n"
            . "END:VALARM\r\n"
            . "END:VEVENT\r\n"
            . "END:VCALENDAR\r\n";
$domain = $data['domain'];
$api_key = $data['api_key'];
$from = $data['from'];
$to = $data['to'];
$subject = $data['subject'];
$body = $data['body'];

$ics_content = isset($data['ics_content']) ? $data['ics_content'] : null;
$attachments = $ics_content;

$request = $this->httpClient->request('POST', "https://api.mailgun.net/v3/{$domain}/messages",[
    'auth' => ['api', $api_key],
    RequestOptions::MULTIPART => [
        [
            'name' => 'from',
            'contents' => $from,
        ],
        [
            'name' => 'to',
            'contents' => $to,
        ],
        [
            'name' => 'subject',
            'contents' => $subject,
        ],
        [
            'name' => 'html',
            'contents' => "<html>{$body}</html>",
        ],
        [
            'name' => 'attachment', // Name of the attachment parameter
            'contents' => base64_decode($attachments), // iCalendar content
            'filename' => 'invite.ics', // Filename for the attachment
        ],
    ],
]);

$statusCode = $request->getStatusCode();
$response_data = json_decode($request->getBody()->getContents(), true);
1

There are 1 best solutions below

7
Dmitry Streblechenko On

Don't send ICS as an attachment. The calendar part must be the one and only MIME part (root) with the right content type (text/Calendar).