Php 如何将多个文件附加到电子邮件并将其动态发送到多个emailid

Php 如何将多个文件附加到电子邮件并将其动态发送到多个emailid,php,javascript,css,Php,Javascript,Css,如何将多个文件附加到电子邮件并将其动态发送到多个emailid 我有一个表单,我想在点击按钮时将多个文件动态发送到多个电子邮件id。我尝试过这段代码,但它只接受zip文件。我想把所有的文件单独附上 <?php function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { $file = $path.$filename;

如何将多个文件附加到电子邮件并将其动态发送到多个emailid

我有一个表单,我想在点击按钮时将多个文件动态发送到多个电子邮件id。我尝试过这段代码,但它只接受zip文件。我想把所有的文件单独附上

<?php

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/mixed; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}


$my_file = "1.rar";
$my_path = $_SERVER['DOCUMENT_ROOT']."schedulemgt/upload/";
$my_name = "tech";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,Your report is here";


mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

?>


--PHP混合-
内容类型:多部分/备选;boundary=“PHP alt-”
--PHP alt-
内容类型:文本/纯文本;charset=“iso-8859-1”
内容传输编码:7bit
你好,世界!!!
这是一封简单的短信。
--PHP alt-
内容类型:text/html;charset=“iso-8859-1”
内容传输编码:7bit
你好,世界!
这是HTML格式的东西。

--PHP alt-- --PHP混合- 内容类型:application/zip;name=“attachment.zip” 内容传输编码:base64 内容配置:附件 --PHP混合--
当其他库比我更喜欢的库做得更好时,为什么要自己做这件事?请告诉我这些库的情况好吗?我们提供了链接,请访问这些站点并阅读。请注意,最好不要在
mail()
函数中通过
@
抑制警告。我如何附加多个文件?并将其发送到多个电子邮件id。文件可以是任何类型,而不仅仅是zip
<?php 

 $to = 'youraddress@example.com'; 

$subject = 'Test email with attachment'; 
 //create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
 //define the headers we want passed. Note that they are separated with \r\n 
 $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
 $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
//define the body of the message. 
 ob_start(); //Turn on output buffering 
  ?> 
 --PHP-mixed-<?php echo $random_hash; ?>  
 Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
 Content-Type: text/plain; charset="iso-8859-1" 
 Content-Transfer-Encoding: 7bit

 Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
 Content-Type: text/html; charset="iso-8859-1" 
  Content-Transfer-Encoding: 7bit

 <h2>Hello World!</h2> 
 <p>This is something with <b>HTML</b> formatting.</p> 

 --PHP-alt-<?php echo $random_hash; ?>-- 

   --PHP-mixed-<?php echo $random_hash; ?>  
  Content-Type: application/zip; name="attachment.zip"  
  Content-Transfer-Encoding: base64  
  Content-Disposition: attachment  

  <?php echo $attachment; ?> 
 --PHP-mixed-<?php echo $random_hash; ?>-- 

 <?php 
 //copy current buffer contents into $message variable and delete current output buffer 
 $message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>