PortyR
PortyR

Reputation: 385

When sending an iCalendar event, how do I get it to appear in the body of the email?

I'm trying to send an iCalendar event through email and have it appear in the body of the email in Outlook. I'm using PHP and Postmark and Spatie iCalendar generator.

This is the code so far:

public function testCreateIcsFile()
    {
        $eventName = 'Testing Create Event';
        $organizerName = 'John Smith';
        $organizerEmail = '[email protected]';
        $address = '123 Main St, Springfield, IL 62701';
        $addressName = 'My House';
        $uniqueIdentifier = 'test-1234';
        $description = 'This is a test event description';
        $createdAt = new \DateTime('2024-07-08 14:30:00', new \DateTimeZone('America/New_York'));
        $startsAt = new \DateTime('2024-07-08 15:00:00', new \DateTimeZone('America/New_York'));
        $endsAt = new \DateTime('2024-07-08 17:00:00', new \DateTimeZone('America/New_York'));
        $attendeeEmail = '[email protected]';
        $attendeeName = 'Joe Blow';
        $participationStatus = ParticipationStatus::needs_action();
        $requiresResponse = true;
        $timezone = Timezone::create('America/New_York');
        $event = Event::create()
            ->name($eventName)
            ->organizer($organizerEmail, $organizerName)
            ->address($address)
            ->addressName($addressName)
            ->uniqueIdentifier($uniqueIdentifier)
            ->description($description)
            ->createdAt($createdAt)
            ->startsAt($startsAt)
            ->endsAt($endsAt)
            ->attendee($attendeeEmail, $attendeeName, $participationStatus, $requiresResponse);

        $cal = Calendar::create('Testing Calendar Library')
            ->timezone($timezone)
            ->event($event);

        $ics = $cal->get();
        $ec = new Email();
        $html = $ec->generateEmailHtml([
            'template' => 'calendar_invite',
            'recipientName' => $attendeeName,
            'eventName' => $eventName,
            'organizerName' => $organizerName,
            'organizerEmail' => $organizerEmail,
            'address' => $address,
            'addressName' => $addressName,
            'description' => $description,
            'startsAt' => $startsAt->format('Y-m-d H:i:s'),
            'endsAt' => $endsAt->format('Y-m-d H:i:s'),
            'requiresResponse' => $requiresResponse,
        ]);

        $res = $ec->sendEmail([
            'from' => '[email protected]',
            'to' => '[email protected]',
            'subject' => 'Another ICS test',
            'html' => $html,
            'attachments' => [
                [
                    'filename' => 'test.ics',
                    'name' => 'test.ics',
                    'content' => $ics,
                    'contenttype' => 'text/calendar; charset=utf-8; method=REQUEST',
                    'mimetype' => 'text/calendar; charset=utf-8; method=REQUEST',
                    'Disposition' => 'inline',
                    'content-disposition' => 'attachment; filename="invite.ics"',
                    'content-transfer-encoding' => 'base64'
                ]
            ]
        ]);

        print_r($res);
    }

What comes through is this (the body text appears because I added data to a template): Email preview

How can I get the calendar event to appear in the body of the email and fix the "not supported calendar message" of the attachment?

TIA.

Upvotes: 1

Views: 58

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66306

For Outlook to recognize the message as a meeting invitation, ICS file must not be sent as an attachment. The message must be a single part MIME message with the content type of text/calendar.

Upvotes: 1

Related Questions