通过mail()php发送带有多个附件的邮件时出现问题。只发送文本文件,没有pdf

时间:2015-10-27 00:37:34

标签: php email pdf

我是php的新用户,我正尝试通过邮寄方式从html表单发送包含多个附件的邮件。

问题是文本文件发送给我但是其他文件,例如,pdf文件不能发送给我好。我认为问题出在标题"\n"中的"\r" "\r\n"mail()表达式中。但我不知道如何正确配置。

这是我的代码:

<?php

if ($_POST){ 

$num = md5(time());

$name = $_POST["name"];
$email = $_POST["mail"];
$subject = $_POST["subject"];
$menssage = $_POST["menssage"];
$to = "prueba@gmail.com";
$dummy = "";

//MAIL BODY
$body = "
<html>
<head>
<title>My Web</title>
</head>

$body .= "
<strong style='color:#0090C6;'>Name: </strong>
<span style='color:#767676;'>" . $name . "</span><br>";

$body .= "
<strong style='color:#0090C6;'>Email: </strong>
<span style='color:#767676;'>" . $email . "</span><br>";

$body .= "
<strong style='color:#0090C6;'>Message: </strong>
<span style='color:#767676;'>" . $menssage . "</span><br>";

$body .= "</body></html>";



// MULTI-HEADERS Content-Type: multipart/mixed and Boundary is mandatory.
$headers = "From: $name <$email>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; "; 
$headers .= "boundary=".$num."\n";
$headers .= "--".$num."\n"; 

// HTML HEADERS 
$headers .= "Content-Type: text/html; charset=UTF-8";
$headers .= "Content-Transfer-Encoding: 8bit";
$headers .= "".$body."\n";
$headers .= "--".$num."\n";

if (isset ($_FILES["attach_files"])) {
    $tot = count($_FILES["attach_files"]["name"]);
    for ($i = 0; $i < $tot; $i++){
        $_name=$_FILES["attach_files"]["name"][$i];
        $_type=$_FILES["attach_files"]["type"][$i];
        $_size=$_FILES["attach_files"]["size"][$i];
        $_temp=$_FILES["attach_files"]["tmp_name"][$i];  

        //FILES EXISTS
        if(strcmp($_name, "")){
            $fp = fopen($_temp, "rb");
            $file = fread($fp, $_size);
            $file = chunk_split(base64_encode($file));
        }

        // FILES HEADERS
        $headers .= "Content-Type:application/octet-stream ";
        $headers .= "name=\"".$_name."\"\n";
        $headers .= "Content-Disposition: attachment; ";
        $headers .= "filename=\"".$_name."\"\n";
        $headers .= "Content-Transfer-Encoding: base64\n";
        $headers .= "".$file."\"\n";
        $headers .= "--".$num."\n";
    }


}else { //FILES NO EXISTS

// HTML HEADERS
$headers = "From: $name <$email>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
}

// SEND MAIL
if (mail($to, $subject, $dummy, $headers)){
    echo "<script language='javascript'> alert('Mail send, Thanks.'); window.location.href='index.html';</script>";
} 
else {
     echo "<script language='javascript'> alert('ERROR: mail failed.');</script>";
}

}
?>

1 个答案:

答案 0 :(得分:1)

不要重新发明轮子。试试PHPMailer

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}