【问题标题】:Remove headers from Email Body从电子邮件正文中删除标题
【发布时间】:2012-10-10 13:51:15
【问题描述】:

我在使用电子邮件同步应用程序时遇到问题,我正在为我的项目开发一个对话模块,所以我从电子邮件帐户中提取了电子邮件正文和附件,但问题是我在电子邮件正文中也有一些标题文本,例如

"--PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: text/plain --PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 "

我想删除它

实际输出是

--PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: text/plain --PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: multipart/related; boundary="PHP-related-9ce0b24f8d52c4d37de61cb315107795d140f8b7" --PHP-related-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: text/html Dear Milind

Thank you for choosing XXXXX for your limo hire.

Please accept this e-mail as confirmation the following booking has been
confirmed and requires you to print, sign and return the attached
contract with us, either by e-mail, fax or post, within 3 days.
To view contract for booking please click here
We would like to draw your attention to our agreed Terms and
Conditions concerning the booking and ask that you ensure you comply,
which can be found on our website www.xxxxx.co.uk

If this has been received in error, please contact our Office
immediately on XXXXXXX or XXXXXX.

Regards
Company name
TEL:

我想要类似的东西

Dear Milind

    Thank you for choosing XXXXX for your limo hire.

    Please accept this e-mail as confirmation the following booking has been
    confirmed and requires you to print, sign and return the attached
    contract with us, either by e-mail, fax or post, within 3 days.
    To view contract for booking please click here
    We would like to draw your attention to our agreed Terms and
    Conditions concerning the booking and ask that you ensure you comply,
    which can be found on our website www.xxxxx.co.uk

    If this has been received in error, please contact our Office
    immediately on XXXXXXX or XXXXXX.

    Regards
    Company name
    TEL:

我尝试过substr(),但它没有那么有用 任何正则表达式可能是? 这是发送电子邮件的代码 让我们假设正文字符串 = $email_message 所有电子邮件格式都写在“字符串”中 PS:请注意,有些邮件包含附件

$subject = 'A sample email - Dual Format plus attachment plus inline';

// Create a boundary string.  It needs to be unique (not in the text) so ...
// We are going to use the sha1 algorithm to generate a 40 character string:
$sep = sha1(date('r', time()));

// Define the headers we want passed.
$headers = "From: StarLimosine <sender@gmail.com> \r\n X-Mailer: Custom PHP Script";
//$headers.="\r\nCc:sender@gmail.com";
$headers.="\r\nBcc:sender@gmail.com , sender@gmail.in";
// Add in our primary content boundary, and mime type specification:
$headers .= 
    "\r\nContent-Type: text/html; boundary=\"PHP-mixed-{$sep}\"";

// Prepare our attachment file - Read it in, encode it and split it
//$attached = chunk_split(base64_encode(file_get_contents('attachment.zip')));

// Also now prepare our inline image - Also read, encode, split:
$inline = chunk_split(base64_encode(file_get_contents('images/Logo.png')));

// Now the body of the message.
$body =<<<EOBODY
--PHP-mixed-{$sep}
Content-Type: multipart/alternative; boundary="PHP-alt-{$sep}"

--PHP-alt-{$sep}
Content-Type: text/plain


--PHP-alt-{$sep}
Content-Type: multipart/related; boundary="PHP-related-{$sep}"

--PHP-related-{$sep}
Content-Type: text/html

$email_message
<strong>
Star Limousines<br/>
TEL: 0800 9 556 556 <br/>
or 01435 813494</strong><br/>
<span style="font-size:10px">Registerd Office Pick Hill Farm, Horam East Sussex TN21 0JR</span>

--PHP-mixed-{$sep}--
EOBODY;
$body=$email_message;

mail($to,$subject,$body);

【问题讨论】:

  • 问题在于您的电子邮件提取方法。使用适当的 MIME 库,而不是事后尝试修复它。
  • 你能不能多描述一些或者给我网址什么的?
  • 您不应该自己生成 mime 电子邮件。 PHPMailerSwiftmailer 用更少的代码为你做到这一点。
  • 感谢大家的帮助,PHPMailer 为我工作!

标签: php email header content-type


【解决方案1】:

在任何时候,标题都在前两行,对吗?如果是这种情况,您可以这样做:

substr($email, strpos($email, PHP_EOL, strpos($email, PHP_EOL) + 1));

【讨论】:

  • 感谢 Praveen 的帮助,它解决了我的一些问题,但不是全部
【解决方案2】:

仅当您使用 GET 发布数据时才添加此内容。

$headers .= 
    "\r\nContent-Type: text/html; boundary=\"PHP-mixed-{$sep}\"";

你会得到没有标题的响应

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2013-09-04
    • 2014-05-21
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多