使用PHP在电子邮件中发送多个附件 [英] Sending multiple attachment in an email using PHP
本文介绍了使用PHP在电子邮件中发送多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
发送带有多个附件的电子邮件时遇到问题。这是代码:
<?php
if(isset($ _ POST ['sendEmail ']))
{
foreach($ _ FILES ['uploadEmail'] ['error'] as $ key => $ value){
if(!$ _ FILES ['uploadEmail'] ['error'] [$ key]){
$ target_path =;
$ target_path = $ target_path。 basename($ _FILES ['uploadEmail'] ['name'] [$ key]);
if(move_uploaded_file($ _ FILES ['uploadEmail'] ['tmp_name'] [$ key],$ target_path)){
$ files [] = $ _FILES ['uploadEmail'] ['name' ] [$关键]
}
}
}
$ toEmails = explode(,,$ _ POST ['toEmail']);
$ count = count($ toEmails);
$ i = 0; $ j = 1; $ k = 100;
$ bcc ='';
while($ i< $ count){
$ bcc。= $ toEmails [$ i]。,;
if($ j == $ k || $ i == $ count-1){
$ j = 1;
// echo $ bcc。'< br />'.$sub.'<br />'.$message.'<br />< br />';
$ from ='test@gmail.com';
$ sub = $ _POST ['subject'];
$ message = $ _POST ['message'];
///////////////////////
$ headers ='From:'。 $从。 \r\\\
;
$ headers。=Bcc:。 $ bcc。 \r\\\
;
//边界
$ semi_rand = md5(time());
$ mime_boundary === Multipart_Boundary_x {$ semi_rand} x;
//附件标题
$ headers。=\\\
MIME-Version:1.0\\\
。 Content-Type:multipart / mixed; \\\
。 boundary = \{$ mime_boundary} \;
// multipart boundary
$ message =这是MIME格式的多部分消息.\\\
\\\
。 - {$ mime_boundary} \\\
。 Content-Type:text / html; charset = \iso-8859-1\\\\
。 Content-Transfer-Encoding:7bit\\\
\\\
。 $消息。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;
//准备附件
($ x = 0; $ x< count($ files); $ x ++){
$ file = fopen($ files [$ x ], RB);
// echo< br>。filesize($ files [$ x]);
$ data = fread($ file,filesize($ files [$ x]));
fclose($ file);
$ data = chunk_split(base64_encode($ data));
$ message。=Content-Type:{\application / octet-stream\}; \\\
。 name = \$ files [$ x] \\\\
。
Content-Disposition:attachment; \\\
。 filename = \$ files [$ x] \\\\
。
Content-Transfer-Encoding:base64\\\
\\\
。 $数据。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;
}
//发送
///////////////////////
mail('',$ sub,$ message,$ headers);
$ bcc ='';
} else {
$ j ++;
}
$ i ++;
}
}
?>
< html>
< head>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js>< / script>
< script src =http://js.nicedit.com/nicEdit-latest.jstype =text / javascript>< / script>
< script type =text / javascript> bkLib.onDomLoaded(function(){
new nicEditor()。panelInstance('message');
// new nicEditor fullPanel:true})。panelInstance('area2');
});< / script>
< / head>
< body>
< form method =POSTenctype =multipart / form-data>
< table>
< tr>< td>< label for =toEmail>发送到:< / label>< / td>< td>< textarea id =toEmailname = toEmailcols =100rows =10>< / textarea>< / td>< / tr>
< tr>< td>< label for =subject>主题:< / label>< / td>< td>< input type =textname = id =subjectsize =98>< / td>< / tr>
< tr>< td>< label for =toEmail>消息:< / label>< / td>< td>< textarea id =messagename =message cols =100rows =10>< / textarea>< / td>< / tr>
< tr>< td>< label for =upload []>附件:< / label>< / td>< td>< / td>< / tr>
< tr>< td>< label> 1< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 2< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 3< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 4< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 5< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 6< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 7< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 8< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 9< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td>< label> 10< / label>< / td>< td>< input type =filename =uploadEmail []>< / TD>< / TR>
< tr>< td colspan =2align =center>< input type =submitvalue =发送电子邮件name =sendEmailid =sendEmail>< ; / TD>< / TR>
< / table>
< / form>
< body>
< / html>
我收到邮件但找不到任何附件。
有没有人知道可能是什么问题?
这是一封电子邮件正文,邮件内容如下:
MIME-Version:1.0 Content-Type:multipart / mixed;
boundary === Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x
这是MIME格式的多部分消息。
- == Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x Content-Type:text / html; charset =iso-8859-1
内容转移编码:7bit
ds fsdfsdfsdfsdfsdfsdfsf sffdfsdfsdfs fsdfdf sdf s
- == Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x内容类型:{application / octet-stream}; name =/ tmp / phpHFTvAw
Content-Disposition:attachment; filename =Lighthouse.jpg
Content-Transfer-Encoding:base64
- == Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x Content-Type:{application /八位字节流}; name =/ tmp / phpyX67HR
Content-Disposition:attachment; filename =Penguins.jpg
内容转移编码:base64
- == Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x
$我建议使用 PHPMailer 发送带有附件的邮件:<?php
require 'PHPMailerAutoload.php'; //如果此文件不位于同一目录中,请使用__DIR__。 /path/to/PHPMailerAutoload.php
$ mail = new PHPMailer;
$ mail-> From ='from@example.com';
$ mail-> FromName ='Mailer';
$ mail-> addAddress('joe@example.net','Joe User'); //添加收件人
$ mail-> addAddress('ellen@example.com'); // Name is optional
$ mail-> addReplyTo('info@example.com','Information');
$ mail-> addCC('cc@example.com');
$ mail-> addBCC('bcc@example.com');
$ mail-> addAttachment('/ var / tmp / file.tar.gz'); //添加附件
$ mail-> addAttachment('/ tmp / image.jpg','new.jpg'); //可选名称
$ mail-> isHTML(true); //将电子邮件格式设置为HTML
$ mail-> Subject ='这里是主题';
$ mail-> Body ='这是黑体中的HTML消息正文< b>< / b>';
$ mail-> AltBody ='这是非HTML邮件客户端的纯文本的主体;
如果(!$ mail-> send()){
echo'无法发送消息。
echo'Mailer Error:'。 $ MAIL-> ERRORINFO;
} else {
echo'消息已发送';
}
?>
下载和文档: here 。
I have problem sending an email with multiple attachments. Here is the code:
<?php if(isset($_POST['sendEmail'])) { foreach($_FILES['uploadEmail']['error'] as $key=>$value){ if(!$_FILES['uploadEmail']['error'][$key]){ $target_path = ""; $target_path = $target_path . basename( $_FILES['uploadEmail']['name'][$key]); if(move_uploaded_file($_FILES['uploadEmail']['tmp_name'][$key], $target_path)){ $files[] = $_FILES['uploadEmail']['name'][$key]; } } } $toEmails = explode(",",$_POST['toEmail']); $count = count($toEmails); $i = 0; $j = 1; $k = 100; $bcc = ''; while($i<$count){ $bcc .= $toEmails[$i].","; if($j==$k || $i==$count-1){ $j=1; //echo $bcc.'<br />'.$sub.'<br />'.$message.'<br /><br />'; $from = 'test@gmail.com'; $sub = $_POST['subject']; $message = $_POST['message']; ///////////////////////// $headers = 'From:'. $from . "\r\n"; $headers .= "Bcc:". $bcc . "\r\n"; // 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/html; 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"); //echo "<br>".filesize($files[$x]); $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 ///////////////////////// mail('',$sub,$message,$headers); $bcc = ''; }else{ $j++; } $i++; } } ?> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script> <script type="text/javascript">bkLib.onDomLoaded(function() { new nicEditor().panelInstance('message'); // new nicEditor({fullPanel : true}).panelInstance('area2'); });</script> </head> <body> <form method="POST" enctype="multipart/form-data"> <table> <tr><td><label for="toEmail">Send To : </label></td><td><textarea id="toEmail" name="toEmail" cols="100" rows="10"></textarea></td></tr> <tr><td><label for="subject">Subject : </label></td><td><input type="text" name="subject" id="subject" size="98"></td></tr> <tr><td><label for="toEmail">Message : </label></td><td><textarea id="message" name="message" cols="100" rows="10"></textarea></td></tr> <tr><td><label for="upload[]">Attachments:</label></td><td></td></tr> <tr><td><label>1</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>2</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>3</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>4</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>5</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>6</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>7</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>8</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>9</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td><label>10</label></td><td><input type="file" name="uploadEmail[]"></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="Send Email" name="sendEmail" id="sendEmail"></td></tr> </table> </form> <body> </html>
I received mail but could not find any attachments with it. Does anyone know what could be wrong?
Here is an email body which I got in mail:
MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x" This is a multi-part message in MIME format. --==Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit ds fsdfsdfsdfsdfsdfsdfsf sffdfsdfsdfs fsdfdf sdf s --==Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x Content-Type: {"application/octet-stream"}; name="/tmp/phpHFTvAw" Content-Disposition: attachment; filename="Lighthouse.jpg" Content-Transfer-Encoding: base64 --==Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x Content-Type: {"application/octet-stream"}; name="/tmp/phpyX67HR" Content-Disposition: attachment; filename="Penguins.jpg" Content-Transfer-Encoding: base64 --==Multipart_Boundary_x2d454346f03d2c19cfefc838ce4d8623x
解决方案I propose to use PHPMailer for sending mails with attachements:
<?php require 'PHPMailerAutoload.php'; // If this file is not located in the same directory, use __DIR__ . "/path/to/PHPMailerAutoload.php" $mail = new PHPMailer; $mail->From = 'from@example.com'; $mail->FromName = 'Mailer'; $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient $mail->addAddress('ellen@example.com'); // Name is optional $mail->addReplyTo('info@example.com', 'Information'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?>
Download and documentation: here.
这篇关于使用PHP在电子邮件中发送多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文