The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

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

Labels

This Discussion is public

Notifications

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
$datetime->setTimezone($utc);
$datetime2->setTimezone($utc);

$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';
}

Note that when posting backslashes got removed from $ical variable construction. At the end of each line, please make sure to use backslash r backslash n to create a new line in ics file.

Veronica,

I'm working on a calendar that needs this functionality, how can I connect with you to get a better understanding of how you did this?

I am willing to pay for your time.

Hi Peter,

Yes, sure, please contact me at [email protected] I will help you out, no payment needed, as I can just point you to the right direction :) Please let me know your form set up.

Discussion closed.