Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lprice's avatar
Level 21

Spatie calendar-links ICS invite as attachment to email

Anyone has maybe some example code or pointers on how to attach an ICS file to an email so that email clients recognize it. I've tried, but neither outlook nor Gmail recognizes the email as an invite.

I'm making use of https://github.com/spatie/calendar-links

I have an action class "EventICSAction.php" which creates the ICS file:

$link = Link::create($event->name, $event->start_at, $event->end_at, $allDay = false)
    ->description(($event->body) ?? __('No Description'))
    ->address($event->location->name);

return $link->ics();

And then on the email notification, I attach it to the email. I've tried different mime types etc. all without success.:

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject(__('Your participation was registered'))
            ->markdown('notifications.participant.created', ['user' => $this->user, 'booking' => $this->booking])
            ->attachData((new EventICSAction)->execute($this->booking->event), 'invite.ics', [
                'Content-Type' => 'text/calendar; charset="utf-8"; method=REQUEST',
//                'mime' => 'text/calendar; charset="utf-8"; method=REQUEST',
                'Content-Disposition' => 'attachment; filename="invite.ics"',
            ]);
    }

Any ideas would be appreciated!

Thanks!

0 likes
6 replies
kikebeltran's avatar

HI @lprice try it in your Mail class.

return $this->view('emails.bla-bla')
            ->attach($link->ics(),  ['as' => 'event.ics', 'mime' => 'text/calendar'])
            ->subject("Email subject");

I hope this proves useful

cdterry87's avatar

In case anyone is attempting this in the new Laravel 10 Mailable format which contains an attachments() method, this is how I'm accomplishing the same thing as above in my App\Mail\SendEventInviteMail.php:

// The rest of the Mailable class is defined above
...

/**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
		// Generate your calendar link data using the Spatie Calendar Links package. (The variables below are placeholders so replace them with your dynamic data)
        $calendarLink = Link::create($invitationTitle, $fromDate, $toDate)
				->description($invitationDescription)
				->address($invitationAddress);

		// The attachments method must return an array of Attachment objects. Generate an attachment using fromPath, passing the ics string from the calendar link data generated above. Then give it a title and add the 'text/calendar' mime type.
        return [
            Attachment::fromPath($calendarLink->ics())
                ->as('invitation.ics')
                ->withMime('text/calendar')
        ];
    }
1 like
cdterry87's avatar

@laracoft No, this just adds an attachment of the ics file which can be opened and imported into the user’s calendar.

OstojicI's avatar

@cdterry87 thank you! I thought fromPath() is asking for file path, not sure how this works but it works

Please or to participate in this conversation.