2015-10-27 97 views
0

我与php新,我试图发送邮件与多个附件从html形式通过post方法。通过邮件发送带有多个附件的邮件问题()php。只发送文本文件,否pdf

问题是文本文件送我好,但其他文件,例如,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 = "[email protected]"; 
$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

不要推倒重来。尝试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 = '[email protected]';     // 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('[email protected]', 'Mailer'); 
$mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
$mail->addAddress('[email protected]');    // Name is optional 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
$mail->addBCC('[email protected]'); 

$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'; 
}