无法在PHP中设置邮件头

时间:2013-06-05 04:34:47

标签: php email header sendmail html-email

内容类型作为消息的一部分处理,如何解决?感谢

From - Wed Jun 05 12:29:59 2013
X-Account-Key: account1
X-UIDL: 50933ddb0000053d
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
Return-Path: <flippingST@DPS_FDBD.localdomain>
Received: from xxxxxxxxx (SMTP1 [xxxxxxx])
    by xxxxxx (8.13.8/8.13.8) with ESMTP id r554TvMv018216
    for <leochan@pop.singtao.com>; Wed, 5 Jun 2013 12:29:57 +0800
Received: from cip.singtaonewscorp.com (cip.singtaonewscorp.com [202.66.86.162] (may be forged))
    by xxxxxxx  with Microsoft SMTPSVC(xxxxxxxxx);
    for <leo.chan@singtaonewscorp.com>; Wed, 5 Jun 2013 12:29:56 +0800
Date: Wed, 5 Jun 2013 12:29:56 +0800
From: flippingST@DPS_FDBD.localdomain
Message-Id: <201306050429.r554TudQ021830@smtp.singtao.com>
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AogdALq9rlEuic+b/2dsb2JhbABagzk0gkEBhw+jHgsBkhIdTBd0giMBFQE7AQo8FQEBVgcNEySIEQiPSYxDjgYBAYVBAZxtgj6BBwMEC50ji02DSw
X-IronPort-AV: E=Sophos;i="4.87,804,1363104000"; 
   d="scan'208,217";a="25969537"
Received: from ec2-46-137-207-155.ap-southeast-1.compute.amazonaws.com (HELO DPS_FDBD.localdomain) ([46.137.207.155])
  by cip.singtaonewscorp.com with ESMTP; 05 Jun 2013 12:30:48 +0800
Received: by DPS_FDBD.localdomain (Postfix, from userid 500)
    id D8FEE4329E; Wed,  5 Jun 2013 00:29:28 -0400 (EDT)
To: leo.chan@singtaonewscorp.com
Subject: =?UTF-8?B?5oql56ug5YaF5a655YiG5Lqr?=
X-PHP-Originating-Script: 500:mail.php
MIME-Version: 1.0
X-EsetId: 40C9373366470C695FCF37616E1C4038

Content-type: text/html; charset=UTF-8

From: leo.chan@singtaonewscorp.com  <leo.chan@singtaonewscorp.com>
Message-Id: <20130605042928.D8FEE4329E@DPS_FDBD.localdomain>
Date: Wed,  5 Jun 2013 00:29:28 -0400 (EDT)

<html><head></head><body><b>讯息 :</b>leo.chan@singtaonewscorp.com</br></br>分享连结 :</b><a href = "http://46.137.207.155/?product=ChangSha&issue=20130524&page=1">按此观看</a></br></br><img src = "https://s3-ap-southeast-1.amazonaws.com/demosource/ChangSha/2013/05/24/0/0/A/Content/1/Pg001.png" /></body></html>

__________ Information from ESET NOD32 Antivirus, version of virus signature database 8412 (20130604) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

以上是邮件源代码,请注意

 Content type ,From: leo.chan@singtaonewscorp.com  <leo.chan@singtaonewscorp.com>
Message-Id: <20130605042928.D8FEE4329E@DPS_FDBD.localdomain>
Date: Wed,  5 Jun 2013 00:29:28 -0400 (EDT)

是邮件消息的一部分

这是php代码,我已将内容类型设置为标题,但它似乎在MIME版本1.0之后被切断了?如何解决这个问题?感谢。

<?php
function encodeMIMEString ($enc, $string)
{
   return "=?$enc?B?".base64_encode($string)."?=";
}

if (isset($_POST["data"])){
    // Get posted data
    $mailStr = $_POST["data"];
    $mailStr = str_replace ('&page','#page',$mailStr);
    $mailStr = str_replace ('&issue','#issue',$mailStr);
    $info = explode("&", $mailStr);

    // To send HTML mail, the Content-type header must be set
    $headers  = "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n";
    $headers .= "From: $info[0] <$info[0]>";

    // Read XML to place the headings in mail content  
    $xml = simplexml_load_file('lang'.DIRECTORY_SEPARATOR.$info[4].'.xml') 
       or die("Error: Cannot create object");

    $subject = (string)$xml->mailTitle;
    $mailMsgDes = (string)$xml->mailMsgDes;
    $mailLink = (string)$xml->mailLink;
    $mailLinkView = (string)$xml->mailLinkView; 

    $message = nl2br(htmlentities(trim($info[2]), ENT_QUOTES, "UTF-8"));

    $url = str_replace ('#page','&page',$info[3]);
    $url = str_replace ('#issue','&issue',$url);

    $mailContent = '<html><head></head><body><b>'.$mailMsgDes.' :</b>'.$message.'</br></br>'.$mailLink.' :</b><a href = "'.$url.'">'.$mailLinkView.'</a></br>';

    if (isset($info[5])){
        $mailContent = $mailContent."</br><img src = \"$info[5]\" />";
    }
    if (isset($info[6])){
        $mailContent = $mailContent."</br><img src = \"$info[6]\" />";
    }

    $mailContent .= '</body></html>';

    if (mail($info[1], encodeMIMEString("UTF-8", $subject), $mailContent, $headers))
        echo 'Mail sent';
}
?>

3 个答案:

答案 0 :(得分:2)

使用\r代替\r\n解决了问题。

可能是由于服务器设置。

答案 1 :(得分:1)

将FROM标头放在MIME-Version和Content-type定义之上。只需交换'em:

// To send HTML mail, the Content-type header must be set
$headers = "From: $info[0] <$info[0]>\r\n";
$headers .= "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n\r\n";

答案 2 :(得分:0)

如果使用PHP_EOL常量,则无需担心\ r或\ n或\ r \ n,并且您的代码在服务器之间的可移植性更高。