简体   繁体   English

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

[英]Problems sending mail with multiple attachments via mail() php. send only text file, NO pdf

I'm new with php and I'm trying send mail with multiple attachments from html form by post method. 我是php新手,正尝试通过post方法从html表单发送带有多个附件的邮件。

The problem is text file send me ok but other file, for example, pdf file not send me good. 问题是文本文件可以很好地发送给我,但其他文件(例如pdf文件)不能很好地发送给我。 I think the problem is in "\\n" "\\r" and "\\r\\n" expresions in headers mail() . 我认为问题出在标头mail()中的"\\n" "\\r""\\r\\n"表达式中。 But i don´t know how to configure this correctly. 但是我不知道如何正确配置它。

This is my code: 这是我的代码:

<?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>";
}

}
?>

Don't reinvent the wheel. 不要重新发明轮子。 Try PHPMailer . 试试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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM