Php Mail()函数:打印了标题

时间:2014-03-28 12:38:11

标签: php email-headers

我尝试使用mail()函数在PHP中发送邮件。电子邮件已成功发送,并且存在于收件箱中。问题是电子邮件标题中打印的标题。我已经尝试了标题的各种代码。 1.第一个是这样的:

$headers = "From: My Example Email".'\r\n'.
"MIME-Version: 1.0".'\r\n'.
"Content-Type: text/html; charset=ISO-8859-1".'\r\n'.
'X-Mailer: PHP/' . phpversion();

结果:My Example EmailrnMIME-Version

2.Second header code:

$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();

结果:电子邮件未发送

3.第三个标题代码:

$headers = "From: My Example Email".'"\r\n"'.
"MIME-Version: 1.0".'"\r\n"'.
"Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
'X-Mailer: PHP/' . phpversion();

结果:My Example Emailrn

我使用PHP 5.4.19。任何答案都会有所帮助。

更新

这是我的全部代码:

class User{
 function callname(){
     $user = $_SESSION['id'];
     $query = ("SELECT * FROM user WHERE user.id='$user'");
     while ($result=mysql_fetch_array($query)){
          echo ($result['username']);}}}

$user = new User;

if($_SERVER["REQUEST_METHOD"] == "POST"){
   $username = mysql_real_escape_string(trim($_POST['username']));
   $check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
 if ($check==TRUE){
   $name = $user->callname();
   $to = "myemail@domain.com";
   $subject = "Example Subject";
   $headers = "From: My Example Email".'"\r\n"'.
   "MIME-Version: 1.0".'"\r\n"'.
   "Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
   'X-Mailer: PHP/' . phpversion();
   $message = "Hai $name, this is the new message.";

        mail($to, $subject, $message, $headers);
} else {
?>
    <script type="text/javascript">
        alert("Sorry, username not exist !");
    </script>
    <?php }}

新增更新:

经过长时间的试用并在这里帮助大家,终于找到了解决方案。但也许这很不寻常。

$headers = 'From: My Example Email'.'""'.
'MIME-Version: 1.0'.'""'.
'Content-Type: text/html; charset=ISO-8859-1'.'""'.
'X-Mailer: PHP/' . phpversion();

但我还不明白。一些文献说每个部分都应该用“\ r \ n”来粘合,但这在我的代码中不起作用。 谢谢你的帮助。非常感谢。这一切都非常有用。

4 个答案:

答案 0 :(得分:2)

您可以尝试我的PHP邮件功能。此功能将为您的电子邮件生成RFC兼容的正文和标题部分。

function buildMime($msg){
$num = md5(time());
$num = "_001_".$num."_";

$headers = "From: SenderName<senderEmail@adress.com>\n";
$headers .= "Return-Path: <senderEmail@adress.com>\n";
$headers .= "Reply-To: <senderEmail@adress.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= "   boundary=\"".$num."\"\n";
$headers .= "X-Mailer: PHP v".phpversion()."\n";

$body = "This is a multi-part message in MIME format.\n\n";

$body1 = "--".$num."\n";
$body1 .= "Content-Type: text/plain; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";

$body1 .= trim(strip_tags($msg))."\n";
$body1 .= "\n";

$body1 .= "--".$num."\n";
$body1 .= "Content-Type: text/html; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";

$body1 .= $msg;
$body1 .= "\n";



$bodyx = "--".$num."--\n";
return array('body' => $body.$body1.$bodyx, 'headers' => $headers);
}
$mime = buildMime("<h1>Hello</h1><p>this is my firs test message</p>");
mail('whereToSend@email.com', 'Your subject', $mime[body], $mime[headers]);
?>

答案 1 :(得分:0)

您没有正确封装引号。

正确的做法...

$headers = 'From: My Example Email'."\r\n".
    'MIME-Version: 1.0'."\r\n".
    'Content-Type: text/html; charset=ISO-8859-1'."\r\n".
    'X-Mailer: PHP/' . phpversion();

<强> EDITED CODE

<?php
class User{
    function callname(){
        $user = $_SESSION['id'];
        $query = ("SELECT * FROM user WHERE user.id='$user'");
        while ($result=mysql_fetch_array($query)){
            return $result['username'];}}}

$user = new User;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysql_real_escape_string(trim($_POST['username']));
    $check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
    if ($check==TRUE){
        $name = $user->callname();
        $to = "myemail@domain.com";
        $subject = "Example Subject";
        $headers = "From: My Example Email"."\r\n".
            "MIME-Version: 1.0"."\r\n".
            "Content-Type: text/html; charset=ISO-8859-1"."\r\n".
            "X-Mailer: PHP/" . phpversion();
        $message = "Hai $name, this is the new message.";

        mail($to, $subject, $message, $headers);
    } else {
        ?>
        <script type="text/javascript">
            alert("Sorry, username not exist !");
        </script>
    <?php }}

答案 2 :(得分:0)

$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();

试试这个

答案 3 :(得分:0)

$ headers =“MIME-Version:1.0”。为 “\ r \ n” 个;
$ headers。=“Content-type:text / html; charset = UTF-8”。为 “\ r \ n” 个;


$ headers。='从:'。为 “\ r \ n” 个;
$ headers。='抄送:myboss@example.com'。为 “\ r \ n” 个;

邮件($到,$主题,$消息,$头);