使用PHP Mail()发送附件?

249
我需要通过邮件发送一个PDF文件,这可行吗?
$to = "xxx";
$subject = "Subject" ;
$message = 'Example message with <b>html</b>';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxx <xxx>' . "\r\n";
mail($to,$subject,$message,$headers);

我漏掉了什么?


17
使用 mail() 函数发送附件比你想象的要困难得多,为了节省时间,请尝试使用 PHPMailer - Mihai Iorga
1
或者你可以直接链接它? - user849137
2
@ChristianNikkanen 这只是一个设置良好的脚本,它还具有许多难以实现的功能。为什么要重新发明轮子呢?它不使用任何额外的插件。 - Mihai Iorga
我不知道如何设置那个脚本,而且由于我只是自动发送相同内容的邮件,所以我更喜欢更简单的选项... - user1537415
@ChristianNikkanen 你所说的“空间”是指字符空间还是实际磁盘空间?如果只是字符限制,可以使用URL修剪器的API。但是在这里设置磁盘空间限制不会有问题。 - user849137
显示剩余4条评论
16个回答

341
我同意MihaiIorga在评论中的观点——使用PHPMailer脚本。你似乎是因为想要更简单的选项而拒绝它。相信我,与尝试使用PHP内置的mail()函数自己完成任务相比,PHPMailer是更容易的选择,两者之间的差距非常大。PHP的mail()函数真的不是很好用。
要使用PHPMailer:
- 从这里下载PHPMailer脚本:http://github.com/PHPMailer/PHPMailer - 解压缩存档并将脚本文件夹复制到项目中的一个方便的位置。 - 包含主要脚本文件——require_once('path/to/file/class.phpmailer.php'); 现在,带有附件的电子邮件发送变得异常困难变得非常容易:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

就只有这一行代码$email->AddAttachment();--再也没有更简单的了。

如果你用PHP的mail()函数来实现,你就得写大量的代码,而且还可能会遇到很难发现的bug。


7
我一开始也是这样想的 - 想要使用mail()函数,只是因为已经在我的代码中有它了。但是PHPMAILER让我不到5分钟就成功地发送了附件! - James Wilson
9
PHPMAILER 对很多人来说可能是一个不错的简单方法。但是使用它需要信任另外一个东西(即 PHPMailer 中没有 bug/等等),这似乎是不必要的。如果不盲目地相信,就需要查看至少3155行sloc (115.456 kb)的代码。与此相比,使用纯粹的 mail() 是更好的选择,因为它的替代方案只需要不到100行代码。不喜欢回答“I want A”的问题时却回答“不,用 B 更好”。其他答案则会告诉你,“A 是这样做的”。 - humanityANDpeace
133
我在寻找如何使用 mail() 函数添加附件的答案时发现了这个问题。但是这个答案并没有帮助我实现这个目标。 - Cypher
23
这不是问题的答案。如何使用PHPMailer发送附件并非被问到的如何使用PHP的mail()函数发送附件的方法。请注意区分。 - Toby
6
这个答案也忽略了项目使用的许可证。使用PHPMailer时,你必须确保将其从你的源代码中排除,以避免与它的LGPL许可证产生问题。 - Axle
显示剩余14条评论

225

您可以尝试使用以下代码:

    $filename = 'myfile';
    $path = 'your path goes here';
    $file = $path . "/" . $filename;

    $mailto = 'mail@mail.com';
    $subject = 'Subject';
    $message = 'My message';

    $content = file_get_contents($file);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (RFC)
    $eol = "\r\n";

    // main header (multipart mandatory)
    $headers = "From: name <test@test.com>" . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--" . $separator . $eol;
    $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol;
    $body .= $content . $eol;
    $body .= "--" . $separator . "--";

    //SEND Mail
    if (mail($mailto, $subject, $body, $headers)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
        print_r( error_get_last() );
    }

编辑于 2018年6月14日

为了在某些邮件提供程序中实现更好的可读性 请使用

$body .= $eol . $message . $eol . $eol;$body .= $eol . $content . $eol . $eol;


2
从PHPMailer的文档中...“正确格式化电子邮件非常困难。有无数重叠的RFC,需要紧密遵守可怕的复杂格式和编码规则 - 在网上找到的绝大多数直接使用mail()函数的代码都是错误的!” ...这是真的!我用了类似这样的答案来发送带附件的邮件,它可以工作!只是几天后发现,虽然Gmail可以正常显示附件,但其他提供商会在邮件中直接内联显示base64内容。 - Aaron Cicali
这个答案中发布的代码适用于部分情况,但它省略了许多涉及编码、行折叠、字符集等方面的要求,这可能会给发件人和收件人带来问题。电子邮件格式化很困难。 - Synchro
$headers .= "Content-Transfer-Encoding: 7bit" . $eol; was breaking my emails until I changed it to $headers .= "Content-Transfer-Encoding: 8bit" . $eol; - Blue Eyed Behemoth
1
抱歉,我无法翻译附件中的内容。请提供文本正文以便我进行翻译。谢谢! - Zaheer
1
在我将这一行"$body .= "Content-Disposition: attachment" . $eol;"替换为$body .= 'Content-Disposition: attachment; name=\"". $filename.";'.$eol.$eol;之前,这个脚本发送的是一个空文件。 - rilent
显示剩余9条评论

155

针对 PHP 5.5.27 的安全更新

$file = $path.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$file_name = basename($file);

// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";

if (mail($mailto, $subject, $nmessage, $header)) {
    return true; // Or do something here
} else {
  return false;
}

5
@Jon. $filename 是你的文件的实际名称,而 $path 是不包含文件名的实际文件路径。我认为这些变量已经足够描述了,用于声明和使用它们。 - Simon Mokhele
5
对于发送多个附件的人,请使用 $nmessage .= "--".$uid."\r\n"; 分隔 MIME 部分,并在最后一个 MIME 部分之后使用 $nmessage .= "--".$uid."--";(如上所示)。 - rinogo
1
如果 $message 是 HTML 格式,它将不会被解析并且会原样显示,包括 HTML 标签。如何修复? - Sunish Menon
2
这终于成功了,之前一直头疼着怎么让那个愚蠢的phpmailer工作。 - E.Arrowood
2
这是一个非常清晰和干净的答案。它可以在Outlook、Gmail等接收时正常工作。整洁的答案。如果您能更新HTML消息部分,这将变得更加完整。 - Sriram Nadiminti
显示剩余15条评论

22

Swiftmailer 是另一个易于使用的脚本,自动防止电子邮件注入并使附件发送变得轻松。我也强烈不建议使用 PHP 内置的 mail() 函数。

使用方法:

  • 下载 Swiftmailer 并将 lib 文件夹放入您的项目中
  • 使用 require_once 'lib/swift_required.php'; 包含主文件

现在,在您需要发送电子邮件时添加代码:

// Create the message
$message = Swift_Message::newInstance()
    ->setSubject('Your subject')
    ->setFrom(array('webmaster@mysite.com' => 'Web Master'))
    ->setTo(array('receiver@example.com'))
    ->setBody('Here is the message itself')
    ->attach(Swift_Attachment::fromPath('myPDF.pdf'));

//send the message          
$mailer->send($message);

更多信息和选项可以在Swiftmailer文档中找到。


6
因为你提供了下载第三方库的选项,我猜测。 - vladkras
PHPMailer不是第三方的吗?或者是@MatthewJohnson创建或是SwiftMailer维护者之一?无论哪种方式,只要解决方案好且有效,否决都是不合适的... - Xsmael
1
@Xsmael,PHPMailer 明显是第三方的 :) 我不同意这些负评,因为(至少在当时)这个解决方案确实可行。然而,人们可以按照自己的意愿投票,而赞成票远远超过了反对票。 - Matthew Johnson

17

要发送带有附件的电子邮件,我们需要使用multipart/mixed MIME类型,该类型指定了将在电子邮件中包含多种类型。此外,我们还想使用multipart/alternative MIME类型来发送电子邮件的纯文本和HTML版本。看一下这个示例:

<?php 
//define the receiver of the email 
$to = 'youraddress@example.com'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="attachment.zip"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>

如您所见,附带发送邮件非常容易。在前面的例子中,我们使用了multipart/mixed MIME类型,并在其中使用multipart/alternative MIME类型指定了电子邮件的两个版本。为了将附件包含在我们的信息中,我们从指定的文件中读取数据到字符串中,使用base64编码,将其分成较小的块以确保其符合MIME规范,然后将其作为附件包含。

摘自这里


15
抱歉,我无法访问您在评论中添加的链接。请提供其他文本以供翻译。 - Mark
不错的片段,但是我必须在边界字符串后提供一个额外的换行符才能使其工作。我猜这与php文件的行结束有关。我的编辑器默认为LF,但我认为标准还需要回车(CRLF)。 - Ogier Schelvis

14

这对我有效。它也可以轻松地附加多个附件。

<?php

if ($_POST && isset($_FILES['file'])) {
    $recipient_email = "recipient@yourmail.com"; //recepient
    $from_email = "info@your_domain.com"; //from email using site domain.
    $subject = "Attachment email from your website!"; //email subject line

    $sender_name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING); //capture sender name
    $sender_email = filter_var($_POST["s_email"], FILTER_SANITIZE_STRING); //capture sender email
    $sender_message = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING); //capture message
    $attachments = $_FILES['file'];

    //php validation
    if (strlen($sender_name) < 4) {
        die('Name is too short or empty');
    }
    if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
        die('Invalid email');
    }
    if (strlen($sender_message) < 4) {
        die('Too short message! Please enter something');
    }

    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5("specialToken$4332"); // boundary token to be used

    if ($file_count > 0) { //if attachment exists
        //header
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "From:" . $from_email . "\r\n";
        $headers .= "Reply-To: " . $sender_email . "" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

        //message text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= chunk_split(base64_encode($sender_message));

        //attachments
        for ($x = 0; $x < $file_count; $x++) {
            if (!empty($attachments['name'][$x])) {

                if ($attachments['error'][$x] > 0) { //exit script and output error if we encounter any
                    $mymsg = array(
                        1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
                        2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
                        3 => "The uploaded file was only partially uploaded",
                        4 => "No file was uploaded",
                        6 => "Missing a temporary folder");
                    die($mymsg[$attachments['error'][$x]]);
                }

                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];

                //read file 
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

                $body .= "--$boundary\r\n";
                $body .= "Content-Type: $file_type; name=" . $file_name . "\r\n";
                $body .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
                $body .= "Content-Transfer-Encoding: base64\r\n";
                $body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
                $body .= $encoded_content;
            }
        }
    } else { //send plain email otherwise
        $headers = "From:" . $from_email . "\r\n" .
                "Reply-To: " . $sender_email . "\n" .
                "X-Mailer: PHP/" . phpversion();
        $body = $sender_message;
    }

    $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if ($sentMail) { //output success or failure messages
        die('Thank you for your email');
    } else {
        die('Could not send mail! Please check your PHP mail configuration.');
    }
}
?>

由于缺乏验证和适当的上下文转义用户输入,此代码容易受到标头注入攻击的威胁。 - Synchro
@Synchro .. 这是我找到的唯一适用于多个附件的代码..请问您能否建议如何以安全的方式使用它。 - NMathur

7

由于附件格式指定为application/octet-stream,以上回答均不适用于我的情况。对于PDF文件,最好使用application/pdf格式。

<?php

// just edit these 
$to          = "email1@domain.com, email2@domain.com"; // addresses to email pdf to
$from        = "sent_from@domain.com"; // address message is sent from
$subject     = "Your PDF email subject"; // email subject
$body        = "<p>The PDF is attached.</p>"; // email body
$pdfLocation = "./your-pdf.pdf"; // file location
$pdfName     = "pdf-file.pdf"; // pdf file name recipient will get
$filetype    = "application/pdf"; // type

// creates headers and mime boundary
$eol = PHP_EOL;
$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_$semi_rand";
$headers       = "From: $from$eolMIME-Version: 1.0$eol" .
    "Content-Type: multipart/mixed;$eol boundary=\"$mime_boundary\"";

// add html message body
$message = "--$mime_boundary$eol" .
    "Content-Type: text/html; charset=\"iso-8859-1\"$eol" .
    "Content-Transfer-Encoding: 7bit$eol$eol$body$eol";

// fetches pdf
$file = fopen($pdfLocation, 'rb');
$data = fread($file, filesize($pdfLocation));
fclose($file);
$pdf = chunk_split(base64_encode($data));

// attaches pdf to email
$message .= "--$mime_boundary$eol" .
    "Content-Type: $filetype;$eol name=\"$pdfName\"$eol" .
    "Content-Disposition: attachment;$eol filename=\"$pdfName\"$eol" .
    "Content-Transfer-Encoding: base64$eol$eol$pdf$eol--$mime_boundary--";

// Sends the email
if(mail($to, $subject, $message, $headers)) {
    echo "The email was sent.";
}
else {
    echo "There was an error sending the mail.";
}

$headers = "From: $from$eolMIME-Version: 1.0$eol" 在 $eolMIME 上不断抛出未定义变量错误。 - Recoil
"From: $from" . "$eolMIME-Version: 1.0$eol" 替换 "From: $from$eolMIME-Version: 1.0$eol" - omikes
也许在某些版本的PHP中不能像这样添加两个变量,但在我使用的那个版本中可以。很抱歉。实际上有这么多情况,您可能希望将所有的“$eol”实例替换为“ . ”$eol”,以便一次性完成全部替换。 - omikes

7

在处理格式糟糕的附件时我挣扎了一段时间,最终使用了以下代码:

$email = new PHPMailer();
$email->From      = 'from@somedomain.com';
$email->FromName  = 'FromName';
$email->Subject   = 'Subject';
$email->Body      = 'Body';
$email->AddAddress( 'to@somedomain.com' );
$email->AddAttachment( "/path/to/filename.ext" , "filename.ext", 'base64', 'application/octet-stream' );
$email->Send();

6

工作概念:

if (isset($_POST['submit'])) {
    $mailto = $_POST["mailTo"];
    $from_mail = $_POST["fromEmail"];
    $replyto = $_POST["fromEmail"];
    $from_name = $_POST["fromName"];
    $message = $_POST["message"];
    $subject = $_POST["subject"];

    $filename = $_FILES["fileAttach"]["name"];
    $content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));

    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
    $header .= "Reply-To: " . $replyto . "\r\n";

    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--" . $uid . "\r\n";

// You add html "Content-type: text/html; charset=utf-8\n" or for Text "Content-type:text/plain; charset=iso-8859-1\r\n" by I.khan
    $header .= "Content-type:text/html; charset=utf-8\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

// User Message you can add HTML if You Selected HTML content
    $header .= "<div style='color: red'>" . $message . "</div>\r\n\r\n";

    $header .= "--" . $uid . "\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n"; // For Attachment
    $header .= $content . "\r\n\r\n";
    $header .= "--" . $uid . "--";
    if (mail($mailto, $subject, "", $header)) {
        echo "<script>alert('Success');</script>"; // or use booleans here
    } else {
        echo "<script>alert('Failed');</script>";
    }
}

3

HTML 代码:

<form enctype="multipart/form-data" method="POST" action=""> 
    <label>Your Name <input type="text" name="sender_name" /> </label> 
    <label>Your Email <input type="email" name="sender_email" /> </label> 
    <label>Your Contact Number <input type="tel" name="contactnumber" /> </label>
    <label>Subject <input type="text" name="subject" /> </label> 
    <label>Message <textarea name="description"></textarea> </label> 
    <label>Attachment <input type="file" name="attachment" /></label> 
    <label><input type="submit" name="button" value="Submit" /></label> 
</form> 

PHP代码:

<?php
if($_POST['button']){
{
    //Server Variables
    $server_name = "Your Name";
    $server_mail = "your_mail@domain.com";

    //Name Attributes of HTML FORM
    $sender_email = "sender_email";
    $sender_name = "sender_name";
    $contact = "contactnumber";
    $mail_subject = "subject";
    $input_file = "attachment";
    $message = "description";

    //Fetching HTML Values
    $sender_name = $_POST[$sender_name];
    $sender_mail = $_POST[$sender_email];
    $message = $_POST[$message];
    $contact= $_POST[$contact];
    $mail_subject = $_POST[$mail_subject];

    //Checking if File is uploaded
    if(isset($_FILES[$input_file])) 
    { 
        //Main Content
        $main_subject = "Subject seen on server's mail";
        $main_body = "Hello $server_name,<br><br> 
        $sender_name ,contacted you through your website and the details are as below: <br><br> 
        Name : $sender_name <br> 
        Contact Number : $contact <br> 
        Email : $sender_mail <br> 
        Subject : $mail_subject <br> 
        Message : $message.";

        //Reply Content
        $reply_subject = "Subject seen on sender's mail";
        $reply_body = "Hello $sender_name,<br> 
        \t Thank you for filling the contact form. We will revert back to you shortly.<br><br>
        This is an auto generated mail sent from our Mail Server.<br>
        Please do not reply to this mail.<br>
        Regards<br>
        $server_name";

//#############################DO NOT CHANGE ANYTHING BELOW THIS LINE#############################
        $filename= $_FILES[$input_file]['name'];
        $file = chunk_split(base64_encode(file_get_contents($_FILES[$input_file]['tmp_name'])));
        $uid = md5(uniqid(time()));
        //Sending mail to Server
        $retval = mail($server_mail, $main_subject, "--$uid\r\nContent-type:text/html; charset=iso-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n\r\n $main_body \r\n\r\n--$uid\r\nContent-Type: application/octet-stream; name=\"$filename\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"$filename\"\r\n\r\n$file\r\n\r\n--$uid--", "From: $sender_name <$sender_mail>\r\nReply-To: $sender_mail\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$uid\"\r\n\r\n");
        //Sending mail to Sender
        $retval = mail($sender_mail, $reply_subject, $reply_body , "From: $server_name<$server_mail>\r\nMIME-Version: 1.0\r\nContent-type: text/html\r\n");
//#############################DO NOT CHANGE ANYTHING ABOVE THIS LINE#############################

        //Output
        if ($retval == true) {
            echo "Message sent successfully...";
            echo "<script>window.location.replace('index.html');</script>";
        } else {
            echo "Error<br>";
            echo "Message could not be sent...Try again later";
            echo "<script>window.location.replace('index.html');</script>";
        }
    }else{
        echo "Error<br>";
        echo "File Not Found";
    }
}else{
    echo "Error<br>";
    echo "Unauthorised Access";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接