Send ical meeting invite using SMTP PHPMailer

Send ical meeting invite using SMTP PHPMailer

PHPMailer is an open source PHP library to send emails safely and easily using SMTP credentials from a web server. You can easily send text mail, invite, meetings, calendar with attachment using PHPmailer. PHPMailer support Gmail, Outlook, Office356 and other mail servers.

First you need to download PHPMailer library from Github or you can also download via compose. Composer is the recommended way to install PHPMailer. Just add this line to your composer.json file:

"phpmailer/phpmailer": "^6.5"

or run

composer require phpmailer/phpmailer

After download copy the contents of the PHPMailer folder and paste into your working directories and load each class file manually:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

Send meeting invite in smtp mail using php

Send meeting with ical attachment code:

<?php

require 'vendor/autoload.php';

$mail = new PHPMailer;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'mail.host.com';                       // Specify main and backup server
$mail->SetFrom('jhon@theknowledgeadda.com', 'Theknowledgedda');
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'username';                   // SMTP username
$mail->Password = 'passwordd';               // SMTP password
//$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 25;                                    //Set the SMTP port number - 587 for authenticated TLS
$mail->addAddress('foo@mail.com');  // Add a recipient
//$mail->addCC($manlv4);
//$mail->addBCC('bcc@mail.com');
$mail->isHTML(true);                                  // Set email format to HTML

$location           = "Alaska";
    $date               = '20211216';
    $startTime          = '0800';
    $endTime            = '0900';
    $subject            = 'Millennium Meeting';
    $desc               = 'This email is a reminder for you. Please accept!';

$organizer          = 'Darth Vader';

    $organizer_email    = 'hxxr@xx.com';   
    $participant_name_1 = 'Doe';
    $participant_email_1= 'xx@xx.com';

    $text = "BEGIN:VCALENDAR\r\n
    VERSION:2.0\r\n
    PRODID:-//Deathstar-mailer//theforce/NONSGML v1.0//EN\r\n
    METHOD:REQUEST\r\n
    BEGIN:VEVENT\r\n
    UID:" . md5(uniqid(mt_rand(), true)) . "example.com\r\n
    DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z\r\n
    DTSTART:".$date."T".$startTime."00Z\r\n
    DTEND:".$date."T".$endTime."00Z\r\n
    SUMMARY:".$subject."\r\n
    ORGANIZER;CN=".$organizer.":mailto:".$organizer_email."\r\n
    LOCATION:".$location."\r\n
    DESCRIPTION:".$desc."\r\n
    ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN".$participant_name_1.";X-NUM-GUESTS=0:MAILTO:".$participant_email_1."\r\n
    END:VEVENT\r\n
    END:VCALENDAR\r\n";

$headers = "From: Sender\n";
$headers .= 'Content-Type:text/calendar; Content-Disposition: inline; charset=utf-8;\r\n';
$headers .= "Content-Type: text/plain;charset=\"utf-8\"\r\n"; #EDIT: TYPO
$mail->Subject = $subject;
$mail->Body = $desc; 
$mail->AltBody = $text; // in your case once more the $text string
$mail->Ical = $text; // ical format, in your case $text string

$mail->addAttachment('invite.ics');         // Add attachments


if($mail->send()) {
   echo 'Message has been sent';
  
}else{
   echo 'Message sent fail.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
  
}


?>

One Comment on “Send ical meeting invite using SMTP PHPMailer”

Leave a Reply

Your email address will not be published. Required fields are marked *