通过mail()发送带有多个附件的邮件的问题.只发送文本文件,没有pdf



我是php的新手,我正在尝试通过邮寄方法从html表单发送带有多个附件的邮件。

问题是文本文件可以发送给我,但其他文件,例如pdf文件不能发送给我。我认为问题是在"n" "r""rn"表达式在标题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>rn";
$headers .= "MIME-Version: 1.0rn";
$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: base64n";
        $headers .= "".$file.""n";
        $headers .= "--".$num."n";
    }

}else { //FILES NO EXISTS
// HTML HEADERS
$headers = "From: $name <$email>rn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=UTF-8rn";
$headers .= "Content-Transfer-Encoding: 8bitrn";
}
// 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>";
}
}
?>

别白费力气了。尝试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';
}

最新更新