Html in ics file description with php

I am working on a script that includes access to the mail server, receiving email, analyzing information from email, creating an event (ics file) from the analyzed information, and then sending an invitation to the event along with the original letter as DESCRIPTION ics file. I am doing all of the above in php. I was stuck in the last step, i.e. sent the original email body (html) as a DESCRIPTION.

I realized that the ics file field DESCRIPTION does not suit line breaks (I must use \ n to send multi-line descriptions). So after extracting the body of the email, I delete all the "\ r \ n" and I get the html code in the line without line breaks.

The study found that Outlook uses something like X-ALT-DESC.So to analyze html in the ics file, I save both DESCRIPTION and X-ALT-DESC as a line with html code. But sending email causes Outlook to ignore most of the html, and end the tags on it at the end, and I get only part of the original message.

To find the problem, I sent the same mail to gmail and then downloaded the ics file. While checking, I noticed that gmail does not change DESC in any way, but some line breaks are added automatically. And I noticed that outlook ignores html after the first line break and adds its own tags to complete the html along with native Microsoft Exchange stuff at the beginning.

I have no clue if the problem is in php (to add line breaks) or if there is a restriction on the ics file regarding the length of the html code (i.e. after the line number with automatic line break is added to X-ALT-DESC or something else). Thank you for any help, and I want to disclose that I had no experience in php before starting with this script.

CODE FOR EXTRACTION OF THE MESSAGE (HTML) AND CONFIGURING THIS IN A LINE WITHOUT CHECKING:

$infoxo = quoted_printable_decode(imap_fetchbody($imap,$email_id,"1")); emailyy = str_replace(array("\r\n"),"\n",$infoxo); $lines = explode("\n",$emailyy); $new_lines =array(); foreach($lines as $i => $line) { if(!empty($line)) $new_lines[]=trim($line); $emailzz = implode($new_lines); } $desc = $emailzz; 

IN OTHER FUNCTIONS (CODE FOR ICS FILE):

 $headers = 'Content-Type:text/calendar; Content-Disposition: inline; charset=utf-8;\r\n'; $headers .= "Content-Type: /text/plain;charset=\"utf-8\"\r\n"; $message = "BEGIN:VCALENDAR VERSION:2.0 PRODID:-//BLAH/BLAH METHOD:REQUEST BEGIN:VEVENT UID:" . md5(uniqid(mt_rand(), true)) . "example.com DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z DTSTART:".$start1."00Z DTEND:".$end1."00Z DESCRIPTION:".$desc." SUMMARY:".$subject." ORGANIZER;CN=".$organizer.":mailto:".$organizer_email." LOCATION:".$location." X-ALT-DESC;FMTTYPE=text/html:".$desc." ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS- ACTION;RSVP=TRUE;CN".$participant_name_1.";X-NUM-GUESTS=0:MAILTO:".$participant_email_1." END:VEVENT END:VCALENDAR"; $headers .= $message; 

Then I use phpamiler () to send email

+5
source share
3 answers

Thanks arnaudq for guiding me in the right direction. This is the code that works to bend a line:

 function abc($htmlMsg) { $temp = str_replace(array("\r\n"),"\n",$htmlMsg); $lines = explode("\n",$temp); $new_lines =array(); foreach($lines as $i => $line) { if(!empty($line)) $new_lines[]=trim($line); } $desc = implode("\r\n ",$new_lines); return $desc; } 

In the icalender file, I use:

 DESCRIPTION:".$desc." X-ALT-DESC;FMTTYPE=text/html:".$desc." 

The rest of the options in the icalender file are the same as in my question.

Please note that the above is checked only in Outlook. It displays as a clean event and the correct HTML formatting in the description.

+2
source

Your icalendar stream should not contain lines with more than 75 characters. If your description contains such lines, you should use the folding method described in https://tools.ietf.org/html/rfc5545#section-3.1

Then, looking at your code, your DESCRIPTION field should contain a text version of the description (without any HTML tag).

+1
source

I think the problem is that the lines in the email have the maximum length, so either phpmailer or the mail transfer program breaks long lines. To avoid this, you can use base64 encoding: in this way, base64 code will be split into short lines, which will not affect the encoded line.

As I see it, phpmailer does not allow you to set the encoding of the message body, but you can send ICAL as an attachment.

 $infoxo = quoted_printable_decode(imap_fetchbody($imap,$email_id,"1")); $emailyy = str_replace(array("\r\n"),"\n",$infoxo); $lines = explode("\n",$emailyy); $new_lines = array(); foreach($lines as $i => $line) { $line = trim($line); if(!empty($line)) $new_lines[]=$line; } // it is enough to implode after foreach, when $new_lines is complete $desc = implode($new_lines); $message = "BEGIN:VCALENDAR DESCRIPTION:".$desc." END:VCALENDAR"; // Prepending headers to $message is useless, since phpmailer sets the headers itself. $mail = new PHPMailer; $mail->From = ' tzunghaor@example.com '; $mail->addAddress(' joe@example.com ', 'Joe User'); $mail->isHTML(false); $mail->Subject = 'ical'; $mail->Body = 'ical'; $mail->addStringAttachment( $message, //attachment data 'thing.ical', // attachment file name 'base64', // encoding 'text/calendar;charset=utf-8', // content type 'inline' // content disposition ); $mail->send(); 
0
source

All Articles