2015-01-17 52 views
-1

爲什麼我的腳本不起作用?
我嘗試使用xampp和在線主機。
我試過$headers = "From: $from \r\n";
我得到了「發送成功!」但我沒有收到任何郵件。
$to = "********"; $from = $_POST['email']; $nume = $_POST['nume']; $prenume = $_POST['prenume']; $phone = $_POST['telefon']; $oras = $_POST['oras']; $adresa = $_POST['adresa']; $facultate = $_POST['facultate']; $titlu1 = $_POST['titlu1']; $desc1 = $_POST['desc1']; $titlu2 = $_POST['titlu2']; $desc2 = $_POST['desc2']; $titlu3 = $_POST['titlu3']; $desc3 = $_POST['desc3']; $subject = "Luminile Iernii - Inscriere: $nume $prenume"; $message = " Nume si Prenume: $nume $prenume \n Email: $from \n Nr. Telefon: $phone \n Oras: $oras \n Adresa: $adresa \n Institutia de invatamant: $facultate \n Titlu Fotografie 1: $titlu1 \n Descriere Fotografie 1: $desc1 \n Titlu Fotografie 2: $titlu2 \n Descriere Fotografie 2: $desc2 \n Titlu Fotografie 3: $titlu3 \n Descriere Fotografie 3: $desc3 \n ";帶發送文件的PHP郵件腳本不起作用

// Temporary paths of selected files 
    $file1 = $_FILES['file1']['tmp_name']; 
    $file2 = $_FILES['file2']['tmp_name']; 
    $file3 = $_FILES['file3']['tmp_name']; 

    // File names of selected files 
    $filename1 = "Fotografie 1"; 
    $filename2 = "Fotografie 2"; 
    $filename3 = "Fotografie 3"; 

    // array of filenames to be as attachments 
    $files = array($file1, $file2, $file3); 
    $filenames = array($filename1, $filename2, $filename3); 

    // include the from email in the headers 
    $headers = "From: $from"; 

    // boundary 
    $time = md5(time()); 
    $boundary = "==Multipart_Boundary_x{$time}x"; 

</code> 

Is boundary necessary with attachment?

<code> 
    // headers used for send attachment with email 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\""; 

    // multipart boundary 
    $message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$boundary}\n"; 

    // attach the attachments to the message 
    foreach($files as $key => $value) 
    { 
     if(empty($value)) 
     { 
      unset($files[$key]); 
      unset($filenames[$key]); 
     } 
    } 

    for($x = 0; $x < count($files); $x++) { 
     $file = fopen($files[$x], "r"); 
     $content = fread($file,filesize($files[$x])); 
     fclose($file); 
     $content = chunk_split(base64_encode($content)); 
     $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n"; 
     $message .= "--{$boundary}\n"; 
    } 

    // sending mail 
    $sendmail = mail($to, $subject, $message, $headers); 

    // verify if mail is sent or not 
    if ($sendmail) { 
     echo "Sent successfully!"; 
    } else { 
     echo "Error occurred. Try again!"; 
    } 

</code> 

Someone can exaplin to me?
I don't get it... what I did wrong?

+0

指定錯誤,並確切地告訴它是什麼,不工作.. – displayname

+0

所以你想發送一個電子郵件與附件的PHP? –

+0

我得到了「發送成功!」但我沒有收到任何郵件。 –

回答

2

Try using http://github.com/PHPMailer/PHPMailer

This is a PHP plugin that handles all the stressful tasks of sending mail without writing all the code.

Download the PHPMailer Script from http://github.com/PHPMailer/PHPMailer and upload it to your server.

Include that file like so: <?php require_once('/path/to/class.phpmailer.php'); ?>

你最終的結果會是這個樣子:

require_once('/path/to/class.phpmailer.php'); 

$email = new PHPMailer(); 
$email->From  = '[email protected]'; 
$email->FromName = 'Your Name'; 
$email->Subject = 'Message Subject'; 
$email->Body  = $bodytext; 
$email->AddAddress('[email protected]'); 

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE'; 

$email->AddAttachment($file_to_attach , 'NameOfFile.pdf'); 

$email->Send(); 

如果你嘗試和發送附件沒有這個類文件,你會寫,只是不需要的代碼堆棧。這個插件使發送電子郵件變得容易得多,並且最終還可以更容易地發送帶有附件的電子郵件。

我希望這會有所幫助。