Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar invite is received as ICS file in outlook - Laravel

I am sending calendar invite using Laravel's mail api.

The calendar looks good on gmail but shows an attachment on outlook instead of proper calendar invitation.

Gmail's output:

enter image description here

while on outlook it seems to be an attachment:

enter image description here

I am creating a file with name invite.ics and I put the content inside the invite.ics file, I attach the file while sending the email.

$to = $row->to;
$subject = $row->subject;
$attachments = $row->attachment;
$cc = $row->cc;
$body = $row->body;
$calendar_invitation = $row->calendar_invitation;

\Mail::send(
'emailTemplates.dummy', 
['emailBody'=>$row->body],  
function(Message $message) use ($to,$subject,$attachments,$cc, $body, $calendar_invitation, $companyEmail)
{
    $message->from($companyEmail, '');
    $message->replyTo($companyEmail, 'Email Agent Evmeetings');
    $message->to($to, '')->subject($subject);
    $file = fopen("invite.ics","w");
    echo fwrite($file,$calendar_invitation);
    fclose($file);
    $message->attach('invite.ics', array('mime' => "text/calendar"));


});
like image 468
Danyal Sandeelo Avatar asked Jul 12 '17 10:07

Danyal Sandeelo


People also ask

How do I accept an ICS calendar invite?

To respond to an invite Open the message that contains the meeting request in either the Inbox or right-click the appointment in the Calendar. To respond to the meeting invitation, click Accept, Tentative, or Decline. A reply is automatically sent if the creator requested one.


1 Answers

That's how I made it work

$message->from($companyEmail, '');
$message->replyTo($companyEmail, 'Email Agent Evmeetings');
$message->to($to, '')->subject($subject);
$message->setBody($calendar_invitation, 'text/calendar; charset="utf-8"; method=REQUEST');
$message->addPart($body, "text/html");

Added the calendar in body and changed the mime type to 'text/calendar; charset="utf-8"; method=REQUEST'

and used addPart($body, "text/html"); method to add html body in the email.

Full code:

        \Mail::send('emailTemplates.dummy', ['emailBody'=>$row->body],  function(Message $message) use ($to,$subject,$attachments,$cc, $body, $calendar_invitation, $companyEmail,$replyTo)
        {
            $message->from($companyEmail, trim(env("email_agent_name")));
            $message->replyTo($replyTo, trim(env("email_agent_email")));
            $message->to($to, '')->subject($subject);
            $message->setBody($calendar_invitation, 'text/calendar; charset="utf-8"; method=REQUEST');
            $message->addPart($body, "text/html");

            $attachments = unserialize($attachments);
            foreach($attachments as $attachment){
                if(file_exists(public_path()."/".$attachment['location'])){

                    $message->attach(public_path()."/".$attachment['location'], array('as'=>$attachment['name'].".".pathinfo(parse_url($attachment['location'])['path'], PATHINFO_EXTENSION),
                        'mime' => mime_content_type ( public_path()."/".$attachment['location']) ));
                }
            }
            $cc = unserialize($cc);
            foreach($cc as $anotherEmail){
                $message->cc($anotherEmail);
            }
        });
like image 198
Danyal Sandeelo Avatar answered Nov 09 '22 05:11

Danyal Sandeelo