如何将多个文件附加到电子邮件并动态发送到多个emailid php
我有一个表单,我想在点击按钮时将多个文件动态发送到多个电子邮件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";
?>