我正在尝试发送一封包含多个附件的电子邮件...第一次尝试此操作并卡住,它会成功发送带有附件的电子邮件,但作为单个文件...
对于Ex,如果我有两个文件1.jpg
100kb
和2.jpg
100kb
,则会将包含附件的电子邮件作为200kb
的单个文件发送
这是我的代码
<html>
<head>
<title>Sending attachment using PHP</title>
</head>
<body>
<?php
$to = "abc@ex.com";
$subject = "This is subject";
$message = "This is test message.";
# Open a file
$file = "http://bestwallpaperhd.com/wp-content/uploads/2013/03/hot-girl-wallpaper.jpg";
$content = file_get_contents( $file);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
$content1 = file_get_contents('http://www.albnews.al/wp-content/uploads/2013/08/99163-hot-girl_original.jpg');
$encoded_content1 = chunk_split( base64_encode($content1));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From:admin@hotesttube.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "--$num--";
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"hotgirl.php\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"hotgirl.jpg\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
$header .= "--$num--";
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"hotgirl123.php\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"hotgirl123.jpg\"\r\n\n";
$header .= "$encoded_content1\r\n";
$header .= "--$num--";
# Send email now
$retval = mail ( $to, $subject, "", $header );
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
答案 0 :(得分:0)
以下脚本应该执行您想要的操作。指定要在$files
阵列中发送的文件并填写电子邮件字段。
<?php
// array with filenames to be sent as attachment
$files = array("file1.ext","file1.ext","file1.ext", ...);
// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>