Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html in ics file description with php

I'm working on a script that involves reaching mail server, fetching email,parsing information from email, creating an event(ics file) from parsed info and then sending the event invite along with original email as DESCRIPTION of ics file. I'm doing the whole of the above in php. I'm stuck in the last step,i.e sending original email body(html) as DESCRIPTION.

I have come to understand that ics file field DESCRIPTION is not comfortable with line breaks(to send multi line descriptions I have to use \n).So after extracting email body, I remove all the "\r\n" and I get the html code in a string with no line breaks.

Upon researching I found out that to parse html in an ics file, Outlook uses something like X-ALT-DESC.So,I keep both the DESCRIPTION as well as X-ALT-DESC as the string with html code. But sending the email then results in Outlook ignoring most of the html, and ends the tags on it's own towards the end and I only receive a fraction of the original email.

To find the problem, I sent the same mail to gmail and then downloaded the ics file.Upon inspection I observed that gmail does not alter the DESC in any way but certain line breaks are added automatically. And I have observed the outlook ignores html after the first line break and adds its own tags to complete the html along with it's own Microsoft Exchange stuff at the beginning.

I dont have a clue if the problem is in php (for adding line breaks) or if there is a limitation to ics file regarding length of html code(i.e after n amount of code an automatic line break is added or something) in X-ALT-DESC. Thank you for any help and I wish to disclose that I did not have and experience in php before starting out with this script.

CODE FOR EXTRACTING MESSAGE(HTML) AND PUTTING IT INTO A STRING WITHOUT LINE BREAKS:

$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 ANOTHER FUNCTION(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;

I then use phpamiler() to send the email

like image 723
akshay dhuri Avatar asked Aug 13 '15 08:08

akshay dhuri


1 Answers

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

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 parameters in the icalender file are same as those in my question.

Please note that the above is only tested in Outlook. It shows up as a clean event and proper HTML formatting in description.

like image 158
akshay dhuri Avatar answered Oct 11 '22 13:10

akshay dhuri