Html 在 ics 文件描述中 php

Html in ics file description with php

我正在编写一个脚本,该脚本涉及到达邮件服务器、获取电子邮件、从电子邮件中解析信息、从解析的信息中创建事件(ics 文件),然后将事件邀请连同原始电子邮件作为 ics 的描述发送文件。我正在 php 中完成上述所有操作。我停留在最后一步,即发送原始电子邮件正文 (html) 作为描述。

我了解到 ics 文件字段 DESCRIPTION 不适合换行符(要发送多行描述我必须使用 \n)。所以在提取电子邮件正文后,我删除了所有“\r\n" 并且我在没有换行符的字符串中得到 html 代码。

经过研究,我发现要在 ics 文件中解析 html,Outlook 使用 X-ALT-DESC.So 之类的东西,我保留了 DESCRIPTION 和 X-ALT-DESC作为带有 html 代码的字符串。但是发送电子邮件会导致 Outlook 忽略大部分 html,并在最后自行结束标签,我只收到原始电子邮件的一小部分。

为了找到问题,我将相同的邮件发送到 gmail,然后下载 ics file.Upon 检查我发现 gmail 不会以任何方式更改 DESC,但会自动添加某些换行符。我观察到 outlook 在第一个换行符后忽略了 html,并在开头添加了自己的标签来完成 html 以及它自己的 Microsoft Exchange 内容。

我不知道问题出在 php(用于添加换行符)还是 如果 ics 文件在 X-ALT-DESC 中有关于 html 代码长度的限制(即在 n 个代码量之后添加一个自动换行符或其他东西)。 感谢您的帮助,我想透露一下,在开始使用此脚本之前,我没有 php 方面的经验。

用于提取消息(HTML)并将其放入没有换行符的字符串的代码:

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

在另一个函数中(ICS 文件的代码):

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

然后我使用 phpamiler() 发送电子邮件

我认为问题在于电子邮件中的行有最大长度,因此 phpmailer 或邮件传输程序会中断长行。为避免这种情况,您可以使用 base64 编码:这样 base64 代码将被分成短行,这不会影响编码后的字符串。

据我所知,phpmailer 不允许设置邮件正文编码,但您可以将 ICAL 作为附件发送。

$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();

您的 icalendar 流不应包含超过 75 个字符的行。如果您的描述包含此类行,则应使用 https://www.rfc-editor.org/rfc/rfc5545#section-3.1

中描述的折叠技术

然后查看您的代码,您的 DESCRIPTION 字段应该包含描述的纯文本版本(没有任何 HTML 标记)。

感谢arnaudq对我的正确指导direction.This是用于折叠行的代码:

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

在我使用的 icalender 文件中:

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

icalender文件中的其余参数与我的问题相同。

请注意以上仅在 Outlook 中测试。它在描述中显示为干净的事件和正确的 HTML 格式。