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:
while on outlook it seems to be an attachment:
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"));
});
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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With