How to send iCal attachment in the email

|
| By Webner

Suppose you are sending an email in PHP and want to send iCal attachment. In PHP this can be achieved as below:

1. Create iCal file content and put in a variable (like $iCal here):

$iCal = "BEGIN:VCALENDAR\r\n
VERSION:2.0\r\n
METHOD:PUBLISH\r\n
X-WR-CALNAME: DASN events | Dallas AfterSchool Network\r\n
PRODID:-//Drupal iCal API//EN\r\n
BEGIN:VEVENT\r\n
UID:calendar:120:field_event_datetime:0:0\r\n
SUMMARY:$classnames\r\n
ORGANIZER;CN=$name:mailto:$email\r\n
LOCATION:$where\r\n
DESCRIPTION:$description\r\n
DTSTART:$startDate\r\n
URL;VALUE=URI:http://dasn.org/calendar/120\r\n
END:VEVENT\r\n
END:VCALENDAR\r\n";

2.Now $iCal can be appended to the email body and it will show up like an iCal event in the email as shown in this image:

3. If you are using sendgrid, you can send $iCal in parameters array like given below:

$params = array(
'api_user'  => $user,
'api_key'   => $pass,
'to'        => $to,
'from'      => $from,
'subject'   => $subject,
'html'      => $body,
'files[iCal.ics]' => $iCal
);

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need any software development assistance please contact us at dev@webners.com

Leave a Reply

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