duanhan4763 2016-05-23 17:16
浏览 43

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

Mine script won't send attachment (this is for more people, but the single give the same problem).

There is the code:

    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" . "
";
    $headers .= 'From: <'.$email.'>' . "
";
    $headers .= "Content-Type: multipart/mixed;
";
 // 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;
";
  $headers .= " boundary=\"{$mime_boundary}\"";

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

";

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

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

";
  $body .= $message . "

";

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

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

";
  $body .= $data . "

";

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


    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" . "
";
    $headers .= "Content-type:text/html;charset=UTF-8" . "
";


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

Worked for 5/6 minutes... Sorry for the bad code, i'am not an expert :\ It send email without attachment, i'm so confused, where is the problem?

  • 写回答

1条回答 默认 最新

  • dongtong7990 2016-05-23 18:11
    关注

    If you want to send mail with attachments you might want to take a look on some external libraries like PHPMailer for example. There you can add an attachment simply $mail->addAtachment('path/To/File.extension').

    When sending email with mail() function there are big chance that mail will end up in spam folder. With PHPmailer you can use SMTP authentication and trusted SMTP server and this will reduce chances that your mail will be considered as spam.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么