How to send ical attachment via PHPMailer

|
| By Webner

We can send ical attachment in email using attach ical file (filename with .ics file extension).

For example, following is the object containing the information about ical like event start date and time, location, event summary etc:

$ical_content = “BEGIN:VCALENDAR
VERSION:2.0
PRODID://Drupal iCal API//EN
BEGIN:VEVENT
UID:http://www.icalmaker.com/event/d8fefcc9-a576-4432-8b20-40e90889affd
DTSTAMP:20170203T045941Z
DTSTART:20170214T060000Z
DTEND:20170214T100000Z
SUMMARY:Party in Daawat
LOCATION:Hotel Daawat, Ground Floor, Phase 5, Sector 59, Near Post Office, Mohali 160059.
DESCRIPTION:Dinner
END:VEVENT
END:VCALENDAR”;

Normally, We can send ical in email along with other parameters, if we are using curl-post request to send emails, like following:

$params = array(
	'api_user'  => $user,
	'api_key'   => $pass,
	'to'        => $to,
	'from'      => $from,
	'fromname' => $fromName,
	'subject'   => $subject,
	'html'      => $body,
	//'headers'   => json_encode($headers),
	'files[ical.ics]' => $ical_content
);

But this method is not supported with some SMTP servers like outlook mail, yahoo etc.

So, to support ical in emails for such smtp servers, we can use following method to attach ical:

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host =$host;
$mail->Port =$port;
$mail->Username = $username;
$mail->Password = $password;
$mail->SMTPSecure = $smtpSecure;
$mail->SMTPAuth = true;
$mail->CharSet = 'UTF-8';
$message =$body;
$subject =$subject;
$to_id=$to;
$mail->From =$from;
$mail->FromName= $emailFromName;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->msgHTML($message);
//For sending ical
if(!empty($ical)){
$mail->addStringAttachment($ical_content,'ical.ics','base64','text/calendar');
}

Here $ical_content contains the data of ical, ‘ical.ics’ is name given to attachment file and ‘text/calendar’ declare the type of attachment. This function (addStringAttachment) will attach the ical in the email. Then, we can send email like this:

$mail->send();

The ical attachment will be shown in the email just like in the following screenshot:
1

4 comments

  1. Thanks, but not working.
    ical.ics is attached to email but Gmail dont showing RSVP buttons (like on your screenshot)

  2. For this you need to pass 2 additional statements in ical file:

    METHOD:REQUEST
    ATTENDEE;RSVP=TRUE;CN=Yourname;X-NUM-GUESTS=0:MAILTO:target-email-id

    Here’s the complete text of iCal file for example:

    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//blog-webners-005//
    METHOD:REQUEST
    X-WR-TIMEZONE:UTC
    BEGIN:VEVENT
    UID:blog-webners-005
    DTSTAMP:20180124T100152Z
    ATTENDEE;RSVP=TRUE;CN=Webner;X-NUM-GUESTS=0:MAILTO:test@test.com
    DESCRIPTION:Location Name: Webner Solution\nWebsite: https://www.web
    nersolutions.com\nInstructor Name: Webner Solution \nInstructor Email: abc@webners.com\nInstructor Bio: Webner Solution\nWebner Solution
    DTSTART:20180124T173000Z
    DTEND:20180125T183000Z
    LOCATION:
    ORGANIZER;CN=webnersolutions ;SENT-BY=”MAILTO:abc@webners.com”:MAILTO:
    abc@webners.com
    SEQUENCE:1504692112
    STATUS:CONFIRMED
    SUMMARY:RSVP button
    TRANSP:OPAQUE
    END:VEVENT
    END:VCALENDAR

Leave a Reply

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