Send .ics calendar invite after form entry is updated or created

Est. Reading: 3 minutes
By: veronicalobanov
Created: 04/26/2018
Difficulty: Intermediate

Send .ics calendar invite after form entry is updated or created

×Warning: This tutorial was created 2185 days ago. Some of the information may be out of date with more recent versions of Formidable. Please proceed with caution and always perform a backup before adding custom code.

I am using frm_after_create_entry & frm_after_update_entry hooks to send an email with calendar invite. I have tested it with google calendar and outlook and it works for me. You can add more conditional logic to only get it sent if certain fields have certain values

add_action('frm_after_create_entry', 'sendcalendarinvite', 99, 2);
add_action('frm_after_update_entry', 'sendcalendarinvite', 99, 2);
function sendcalendarinvite($entry_id, $form_id){
if($form_id == 50){ //replace 5 with the id of the form

$seq_id = $_POST['item_meta'][810]; //this is where I stored Event ID
$venue = $_POST['item_meta'][811]; //field of my location info
$event_name = $_POST['item_meta'][811];

//my invitees were stored in repeating section here is how I got those values into a string
$repeating_values = array();
foreach( $_POST['item_meta'][815] as $k => $r ) {// Change 815 to the ID of the repeating section in Form
if ( $k === 'row_ids' ) {
$repeating_values[ $k ] = $r;
} else if ( is_array( $r ) ) {
foreach ( $r as $i => $v ) {
if ( $i == 817 ) {// Change 817 to the ID of a field inside of the repeating section
$repeating_values[ $k ] = $v;// they are now all stored in array

$to_address = implode(",",$repeating_values); //creating a string from array
$approver_address = do_shortcode( '[display-frm-data id=342 filter=limited]' );//here I have some additional people that I need to send emails to. //These addresses are stored in a different form. I have created a view which lists all emails from that form separated by ",". So here I am using a //shortcode to display them. Since I am working out of functions.php I am using do_shortcode(); to get them into $approver_address variable.

$summary = 'This is a summary XXX.';
$headers .= "Content-type: text/calendar; method=REQUEST; charset=UTF-8n";
$headers .= "Content-Disposition: inline; filename=calinvite.icsn";
$headers .= "Content-class: urn:content-classes:calendarmessagen";
$headers .= "BCC: [email protected]"; //CC or BCC can be added to headers
$sequence = $seq_id;
$event_id = $seq_id;
$status = 'TENTATIVE';
$timestamp = $_POST['item_meta'][806]." ". date("H:i", strtotime($_POST['item_meta'][807])); // 806 - is date field and 807 is start time
$timestamp2 = $_POST['item_meta'][806]." ". date("H:i", strtotime($_POST['item_meta'][808])); //808 - end time
$timezone = new DateTimeZone($_POST['item_meta'][809]); //809 is time zone. I used official time zone names like America/New_York etc (user //selects it from dropdown field
$organizer = $_POST['item_meta'][812];
// The UTC timezone
$utc = new DateTimeZone('UTC'); //setting $utc variable to UTC timezone
// Create a DateTime object from your timestamp using your local timezone
$datetime = new DateTime( $timestamp, $timezone ); //$datetime now contains date object with timezone that they selected from dropdown
$datetime2 = new DateTime( $timestamp2, $timezone );
// Set the timezone on the object to UTC. We are setting timezone to UTC so that people that are in different timezones who receive this invite receive it //in correct time for their timezone

$desc = "Invite Details";
$ical = "BEGIN:VCALENDARrn";
$ical .= "PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//ENrn";
$ical .= "VERSION:2.0rn";
$ical .= "METHOD:REQUESTrn";
$ical .= "BEGIN:VEVENTrn";
$ical .= "ORGANIZER;CN='John Smith':MAILTO:[email protected]";//email can be hardcoded
$ical .= "ATTENDEE;CN='John Smith';ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:".$organizer."rn"; //email address is a variable
$ical .= "UID:".strtoupper(md5($event_id))."rn";
$ical .= "SEQUENCE:".$sequence."rn";
$ical .= "STATUS:".$status."rn";
$ical .= "DTSTART:".$datetime->format('Ymd')."T".$datetime->format('His')."Zrn";
$ical .= "DTEND:".$datetime2->format('Ymd')."T".$datetime2->format('His')."Zrn";
$ical .= "LOCATION:".$venue."rn";
$ical .= "SUMMARY:".$summary."rn";
$ical .= "DESCRIPTION: This is an event reminder for ".$event_name." event.rn";
$ical .= "BEGIN:VALARMrn";
$ical .= "TRIGGER:-PT15Mrn";
$ical .= "ACTION:DISPLAYrn";
$ical .= "DESCRIPTION:Reminderrn";
$ical .= "END:VALARMrn";
$ical .= "END:VEVENTrn";
$ical .= "END:VCALENDARrn";

$to = $to_address . $approver_address;
$subject = 'Invite for ' . $event_name;
$message = $ical;
add_filter ('wp_mail_content_type', 'set_html_content_type' );
wp_mail( $to, $subject, $message, $headers, $attachments );
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type(){
return 'text/calendar';

Leave a Reply

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community