多个附件

问题描述:

我想用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

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

这是多个脚本的组合和一个位的读取。我没有添加任何表单处理等,但它允许使用该选项通过一个输入按钮附加多个文件。希望对某人有帮助。我敢肯定它打破了各种标准。我知道它适用于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(); 
?>