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

标签 php javascript css

php如何将多个文件附加到email并动态发送到多个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 

 $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"; 
?>

关于php - 如何将多个文件附加到电子邮件并将其动态发送到多个emailid php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17480846/

相关文章:

JavaScript RegEx 获取字符串中特定模式的一次或多次出现

html - 悬停第 n 个元素时如何将 css 应用于前 n 个元素

html - 浏览器之间的差异

javascript - 我如何将总标签添加到垫分页器范围 - Angular Material

javascript - 修复可滚动 div 内的元素

php - 搜索表单 url 结构

javascript - JSON从php到JS文件

php - ZF2 动态菜单 Zend\Navigation\Navigation

php - 为什么我的 WordPress functions.php 无法加载样式或脚本?

javascript - 相当复杂的正则表达式