2012-02-16 75 views
0

我想用php郵件發送郵件中的多個附件。 但問題是,如何使用它。在哪裏下載它,如何安裝它。我搜索了3天,但有困惑,我使用了兩三個教程,不起作用,讓我更加困惑。 我想要一個文件標籤,上傳多個附件,並通過電子郵件發送。 我已完成電子郵件發送與一個附件成功..多個附件

請指導我。並請給那些真正爲此目的工作的鏈接。

+0

改爲使用[SwiftMailer](http://swiftmailer.org/)。 – 2012-02-16 14:39:52

+0

okk。任何真正有效的教程,指導我 – 2012-02-16 14:44:33

+0

請參閱文檔。 http://swiftmailer.org/docs/introduction.html – 2012-02-16 14:45:26

回答

1

PHPMailer可以是downloadedits SourceForge page

我們的代碼,其中大部分從ZIPball提供的實施例採取:

<?php 
require_once 'class.phpmailer.php'; 

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 

try { 
    $mail->AddReplyTo('[email protected]', 'First Last'); 
    $mail->AddAddress('[email protected]', 'John Doe'); 
    $mail->SetFrom('[email protected]', 'First Last'); 
    $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; 
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically 
    $mail->MsgHTML(file_get_contents('contents.html')); 
    $mail->AddAttachment('images/phpmailer.gif');  // attachment 
    $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment 
    $mail->Send(); 
    echo "Message Sent OK</p>\n"; 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 
?> 
+0

PHPMailer有點混亂 - 至少有兩個不同的版本,具有不兼容的變量名稱和函數。這就是爲什麼我傾向於推薦Swiftmailer。 – 2012-02-16 15:10:08

+0

上面的sourceforge URL與我7年前開始使用它的時候並沒有改變。我注意到,自從我上次看起來worxware正試圖移動到谷歌代碼 - 多麼混亂! (就像你說的)我也喜歡SwiftMailer並廣泛使用它,但他要求PHPMailer。 – Treffynnon 2012-02-16 15:13:58

0

這是多個腳本的組合和一個位的讀取。我沒有添加任何表單處理等,但它允許使用該選項通過一個輸入按鈕附加多個文件。希望對某人有幫助。我敢肯定它打破了各種標準。我知道它適用於Chrome 31和IE10。

編輯:使用這個小腳本,我添加了HTML格式的消息和謝謝消息替換。

<?php 

if(isset($_POST['Submit'])) { 

    $email_to = ""; 
    $email_subject = ""; 
    $thankyou = "thanks.html"; 

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

    function died($error) { 
     echo "Sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    $requester_name = $_POST['requester_name']; // required 
    $requester_email = $_POST['requester_email']; // required 




    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message = "<html><body> \r\n"; 
    $email_message .= "<table style=\"border: 1px #777 solid; font-family: Arial; font-size: 13px;\" cellpadding=\"7\"> \r\n"; 
    $email_message .= "<tr><td style=\"background: #444; color:#fff;\"><strong>New Hire Form</strong></td><td style=\"background: #444; color:#fff;\">Requirements</td></tr>" . "\n"; 

    $email_message .= "<tr><td style=\"background: #ccc;\"><strong>Requester Name: </strong></td><td style=\"background: #ddd;\">" .clean_string($requester_name). "</td></tr>" . "\n"; 
    $email_message .= "<tr><td style=\"background: #ccc;\"><strong>Requester Email: </strong></td><td style=\"background: #ddd;\">".clean_string($requester_email). "</td></tr>" . "\n"; 



    $email_message .= "</table> \r\n"; 
    $email_message .= "</body></html>"; 


    // multipart boundary 
$email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; 



for($i=0;$i<count($_FILES['attachfile']['name']);$i++) 
{ 

    if($_FILES['attachfile']['name'][$i] != "") 
    { 
    //here you will get all files selected by user. 




     $name = ($_FILES['attachfile']['name'][$i]); 
     $tmp_name = ($_FILES['attachfile']['tmp_name'][$i]); 
     $type = ($_FILES['attachfile']['type'][$i]); 
     $size = ($_FILES['attachfile']['size'][$i]); 

    echo count($_Files['attachfile']) ; 
    echo $_FILES['attachfile']['name'][$i] ; 
    echo $_FILES['attachfile']['tmp_name'][$i] ; 
    echo $_FILES['attachfile']['type'][$i] ; 


      // Read the file content into a variable 
      $file = fopen($tmp_name,'rb'); 
      $data = fread($file,filesize($tmp_name)); 
      // Close the file 
      fclose($file); 


      $data = chunk_split(base64_encode($data)); 


     $email_message .= "--{$mime_boundary}\n" . 
      "Content-Type: {$type};\n" . 
      " name=\"{$name}\"\n" . 
      "Content-Disposition: attachment;\n" . 
      " filename=\"{$name}\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . 
     $data . "\n\n"; 
    } 
} 

    $headers .= 'From: '.$email_sender."\r\n". // Mail will be sent from your Admin ID 
    'Reply-To: '.$Email."\r\n" .    // Reply to Sender Email 
    'X-Mailer: PHP/' . phpversion(); 
// headers for attachment 
    $headers .= "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; 
    @mail($email_to, $email_subject, $email_message, $headers); 


?> 

<script>location.replace('<?php echo $thankyou;?>')</script> 
<?php 
} 
die(); 
?>