Skip to content

Instantly share code, notes, and snippets.

@vivekwaah
Last active May 22, 2021 14:37
Show Gist options
  • Save vivekwaah/3c6b134eeed633fef209b138d1ecb09c to your computer and use it in GitHub Desktop.
Save vivekwaah/3c6b134eeed633fef209b138d1ecb09c to your computer and use it in GitHub Desktop.
Outlook invitation from Laravel
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Traits\Monday\Monday;
use Illuminate\Support\Facades\Mail;
class InviteMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels, Monday;
public $subject;
private $toAddress;
private $message;
private $headers;
private $fromName;
private $fromAddress;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct() {}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$fromName = "From Kumar";
$fromAddress = "fromKumar@email.com";
$toName = "To Kumar";
$toAddress = "toKumar@email.com";
$subject = "Add your subject here";
$description = "Add your description here";
$location = "Vivek's House";
// Add message
$mimeBoundary = "----Meeting Booking----" . MD5(TIME());
$message = "--$mimeBoundary\r\n\n";
$message .= $description . "\r\n\n";
$message .= "--$mimeBoundary";
$this->message = $message;
$vcal = $this->createEvent($fromName, $fromAddress, $toName, $toAddress, $location, $description);
$this->sendInvite($vcal, $toAddress, $subject);
}
private function createEvent(
string $fromName,
string $fromAddress,
string $toName,
string $toAddress,
string $location,
string $description
): string {
$timeStamp = \Carbon\Carbon::now();
$meetingDuration = (60 * 60);
$meetingstamp = $timeStamp->getTimeStamp();
$dtstart = gmdate('Ymd\THis\Z', $meetingstamp);
$dtend = gmdate('Ymd\THis\Z', $meetingstamp + $meetingDuration);
$uid = date('Ymd') . 'T' . date('His') . '-' . rand();
$organizer = "Organizer name:" . $fromName;
$organizerEmail = $fromAddress;
$vcal = "BEGIN:VCALENDAR\r\n";
$vcal .= "VERSION:2.0\r\n";
$vcal .= 'PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN' . "\r\n";
$vcal .= "METHOD:REQUEST\r\n";
$vcal .= "BEGIN:VEVENT\r\n";
$vcal .= "ORGANIZER;CN=\"$organizer" . "\":mailto:$organizerEmail\r\n";
$vcal .= 'ATTENDEE;CN="' . $toName . '";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:' . $toAddress . "\r\n";
$vcal .= "UID:" . $uid;
$vcal .= "DTSTAMP:" . date('Ymd') . 'T' . date('His') . "\r\n";
$vcal .= "DTSTART:$dtstart\r\n";
$vcal .= "DTEND:$dtend\r\n";
$vcal .= "LOCATION:$location\r\n";
$vcal .= "SUMMARY:$description\r\n";
$vcal .= "DESCRIPTION:$description \r\n";
$vcal .= "BEGIN:VALARM\r\n";
$vcal .= "TRIGGER:-PT15M\r\n";
$vcal .= "ACTION:DISPLAY\r\n";
$vcal .= "DESCRIPTION:Reminder\r\n";
$vcal .= "END:VALARM\r\n";
$vcal .= "END:VEVENT\r\n";
$vcal .= "END:VCALENDAR\r\n";
return $vcal;
}
private function sendInvite(string $vcal, string $toAddress, string $subject): void
{
Mail::raw($this->message, function ($mail) use ($vcal, $toAddress, $subject) {
$swiftObj = $mail
->to($toAddress)
->subject($subject)
->getSwiftMessage();
$swiftObj->addPart($vcal, 'text/calendar; method=REQUEST', 'iso-8859-1');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment