多个附件

时间:2012-02-16 14:36:49

标签: php phpmailer

我想使用php邮件程序在邮件中发送多个附件。 但问题是,如何使用它。在哪里下载,如何安装,。我已经搜索了3天,但是很困惑,我使用了两三个教程,不起作用,让我更加困惑。 我想要一个文件标签,上传多个附件,并通过电子邮件发送。 我已成功完成了使用一个附件的电子邮件发送..

请指导我。请提供真正有用的链接。

2 个答案:

答案 0 :(得分: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('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', '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!
}
?>

答案 1 :(得分: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();
?>