PHP - 邮件不会发送附件,只有没有附件

时间:2016-05-23 17:16:20

标签: php email

我的剧本不会发送附件(这是为了更多的人,但是单曲也会出现同样的问题)。

有代码:

    if ($_POST) {

$to = mysql_real_escape_string($_POST['to']);
$subject = mysql_real_escape_string($_POST['subject']);
$message = mysql_real_escape_string($_POST['message']);


if (empty($to) || empty($subject) || empty($message) ) {

    header("location: nation.php?error=empty");
} else {

    $email = $b['email'];

    $my_file = $_FILES['my_file']['tmp_name'];
    $my_file_type = $_FILES['my_file']['type'];
    $my_file_name = $_FILES['my_file']['name'];
    $body = "";
    //upload
if (is_uploaded_file($my_file))
{
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= 'From: <'.$email.'>' . "\r\n";
    $headers .= "Content-Type: multipart/mixed;\n";
 // Apro e leggo il file my_file
  $file = fopen($my_file,'rb');
  $data = fread($file, filesize($my_file));
  fclose($file);

  // Adatto il file al formato MIME base64 usando base64_encode
  $data = chunk_split(base64_encode($data));

  // Genero il "separatore"
  // Serve per dividere, appunto, le varie parti del messaggio.
  // Nel nostro caso separerà la parte testuale dall'my_file
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // Aggiungo le intestazioni necessarie per l'my_file
  $headers .= "Content-Type: multipart/mixed;\n";
  $headers .= " boundary=\"{$mime_boundary}\"";

  // Definisco il tipo di messaggio (MIME/multi-part)
  $body .= "This is a multi-part message in MIME format.\n\n";

  // Metto il separatore
  $body .= "--{$mime_boundary}\n";

  // Questa è la parte "testuale" del messaggio
  $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
  $body .= "Content-Transfer-Encoding: 7bit\n\n";
  $body .= $message . "\n\n";

  // Metto il separatore
  $body .= "--{$mime_boundary}\n";

  // Aggiungo l'my_file al messaggio
  $body .= "Content-Disposition: attachment; filename=\"{$my_file_name}\"\n";
  $body .= "Content-Transfer-Encoding: base64\n\n";
  $body .= $data . "\n\n";

  // chiudo con il separatore
  $body .= "--{$mime_boundary}--\n";


    if ($privilegi == '1') {

    $nap = mysql_query("SELECT * FROM leads WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$body,$headers); 
    }

    } else if ($privilegi == '2') {
    $nap = mysql_query("SELECT * FROM contacts WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$body,$headers); 
    }

    } else if ($privilegi == '3') {

    $nap = mysql_query("SELECT * FROM agent WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$body,$headers); 
    }

    } else {

    $name = 'error';
    }



    } else {

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


    if ($privilegi == '1') {

    $nap = mysql_query("SELECT * FROM leads WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$message,$headers); 
    }

    } else if ($privilegi == '2') {
    $nap = mysql_query("SELECT * FROM contacts WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$message,$headers); 
    }

    } else if ($privilegi == '3') {

    $nap = mysql_query("SELECT * FROM agent WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$message,$headers); 
    }

    } else {

    $name = 'error';
    }

工作了5/6分钟...对不好的代码抱歉,我不是专家:\ 它发送没有附件的电子邮件,我很困惑,问题出在哪里?

1 个答案:

答案 0 :(得分:0)

如果您想发送带附件的邮件,您可能需要查看一些外部库,例如PHPMailer。在那里你可以简单地添加一个附件$ mail-&gt; addAtachment(&#39; path / To / File.extension&#39;)。

使用mail()函数发送电子邮件时,邮件很可能会以垃圾邮件文件夹结尾。 使用PHPmailer,您可以使用SMTP身份验证和受信任的SMTP服务器,这将减少您的邮件被视为垃圾邮件的机会。