2016-05-23 170 views
0

礦山腳本不會發送附件(這是爲更多的人,但單一給出同樣的問題)。PHP - 郵件不會發送附件,只有沒有它附件

有代碼:

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分鐘......對不起,糟糕的代碼,我'不是專家:\ 它發送電子郵件沒有依附,我好睏惑,問題在哪裏?

回答

0

如果您想發送帶有附件的郵件,您可能需要查看一些外部庫,例如PHPMailer。在那裏你可以添加一個簡單的附件$ mail-> addAtachment('path/To/File.extension')。

使用mail()函數發送電子郵件時,郵件最終會出現在垃圾郵件文件夾中的可能性很大。 使用PHPmailer,您可以使用SMTP驗證和可信的SMTP服務器,這將減少您的郵件被視爲垃圾郵件的機率。