PHP中的多个邮件附件 [英] Multiple Mail Attachments in PHP
问题描述
我想知道我是否可以让你为某些事情动脑筋.
I was wondering if I could pick your brain about something.
我的网站上有一个邮件脚本,可以用来发送带有单个附件的电子邮件.发送单个附件就可以了.
I have a mail script on my site that I can use to send emails with a single attachment. Sending a single attachment works just fine.
我希望能够发送带有多个附件的电子邮件,但这样做遇到了麻烦.
I want to be able to send an email with multiple attachments and I am running into problems doing so.
这是我采用以下格式的文件选择框:
This is the file selection box I have in the form:
<input type="file" name="attachment[]" id="attachment[]" multiple onfocus="this.style.backgroundColor='#e7e7e7'" onblur="this.style.backgroundColor='#ffffff'"/>
然后,当文件被选中,这是在PHP处理它:
Then, when the files are selected this is the php that handles it:
if (!empty($_FILES['attachment']['name']))
{
if(count($_FILES['attachment']['name'])>0)
{
$count = 0;
foreach ($_FILES['attachment']['name'] as $file)
{
$name_of_file = $_FILES['attachment']['name'][$count];
$file_name = $guid." - ".$name_of_file;
$temp_name = $_FILES['attachment']['tmp_name'][$count];
$file_type = $_FILES['attachment']['type'][$count];
$file_size = $_FILES['attachment']['size'][$count];
$count++;
if ($file_size > 2048000)
{
header ("refresh: 5; url=attach.php");
include ("header.php");
echo "File size is to big. Size must be no bigger than 2Mb. Please go <a href='attach.php'>back</a>";
include ("footer.php");
exit;
}
else
{
$pics = array(".bmp", ".gif", ".jpg", "jpeg", ".png"); //5
$docs = array(".doc", "docx", ".odt", ".pdf", ".ppt", "pptx", ".rtf", ".txt", ".xls", "xlsx"); //10
$misc = array(".csv", ".htm", "html", ".php", ".pkt", ".rar", ".sql", ".xpi", ".zip"); //9
$base = basename($file_name);
$extension = substr($base, strlen($base)-4, strlen($base));
$extension = strtolower($extension);
if (in_array($extension,$pics))
{
$target = "".FILES."/".FUP_PICS."/";
}
if (in_array($extension,$docs))
{
$target = "".FILES."/".FUP_DOCS."/";
}
if (in_array($extension,$misc))
{
$target = "".FILES."/".FUP_MISC."/";
}
$target = $target.$base;
$allowed_extensions = array(".bmp", ".csv", ".doc", "docx", ".gif", ".htm", "html",
".jpg", ".JPG", "jpeg", "JPEG", ".odt", ".pdf", ".php", ".pkt", ".png", ".ppt", "pptx",
".rtf", ".sql", ".txt", ".xls", "xlsx", ".zip"
);
if(in_array($extension,$allowed_extensions))
{
$from = mysql_real_escape_string($_POST['from']);
$emailto = mysql_real_escape_string($_POST['emailto']);
$bcc = mysql_real_escape_string($_POST['bcc']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message'], ENT_NOQUOTES);
$message1 = htmlspecialchars_decode($_POST['message'], ENT_QUOTES);
if ($sendhash == 'Y')
{
$message1 .= "\n\nThe following is your unique message ID: ";
$message1 .= $guid;
$message1 .= "\n\nAttachment has been scanned for viruses and is virus free.";
$message1 .= "\n\nPlease make sure the first part of the file name matches the unique message ID. If it does not, please DO NOT open the file";
$message1 .= "\n\nTo verify the validity of the message, click the link below or copy and paste it into your browser:";
$message1 .= "\n\n ";
$message1 .= HTTP_PATH;
$message1 .= "verify.php?uid=";
$message1 .= urlencode($guid);
$message1 .= "\n\nPlease note, verification link expires on ";
$message1 .= $expirydate;
}
else
{
}
$message1 .= "\n\n--\nSent from Chris' Address Book";
foreach ($file as $files)
{
$files = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$from."\r\n";
$header .= "Bcc: ".$bcc."\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-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message1."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n";
}
if ($_POST['emailto'] == '')
{
$sqlp=mysql_query("SELECT * FROM ".PERSON." JOIN contact on contact.personID = person.adbkid WHERE email1 = '$bcc' OR email2 = '$bcc'");
$num=mysql_numrows($sqlp);
}
else
{
$sqlp=mysql_query("SELECT * FROM ".PERSON." JOIN contact on contact.personID = person.adbkid WHERE email1 = '$emailto' OR email2 = '$emailto'");
$num=mysql_numrows($sqlp);
}
while ($row = mysql_fetch_array($sqlp))
{
$adbkid = $row['adbkid'];
$id = $row['id'];
}
if ($num == 0)
{
$selfid = "EMAILSA";
}
else
{
$selfid = $adbkid;
}
if (mail($emailto, $subject, "", $header))
{
if ($sendhash == 'Y')
{
$sql=mysql_query("INSERT INTO ".EMAILS." (emailfrom, emailto, bcc, subject, message, getthedate, gettime, randhash, fileatt, fileext, showinsearch, expireit, showinverify, wasviewed, personID) VALUES ('$from', '$emailto', '$bcc', '$subject', '$message', '$getthedate', '$gettime', '$guid', '$file_name', '$extension', '$showinsearch', '$expireit', '$siv', '$wv', '$selfid')");
$lastid=mysql_insert_id();
$sqlr=mysql_query("INSERT INTO ".HASH." (randhash) VALUES ('$guid')");
}
else
{
$sql=mysql_query("INSERT INTO ".EMAILS." (emailfrom, emailto, bcc, subject, message, getthedate, gettime, fileatt, fileext, showinsearch, expireit, showinverify, wasviewed, personID) VALUES ('$from', '$emailto', '$bcc', '$subject', '$message', '$getthedate', '$gettime', '$file_name', '$extension', '$showinsearch', '$expireit', '$siv', '$wv', '$selfid')");
$lastid=mysql_insert_id();
}
$sqlone = "INSERT INTO ".SENTFILES." (filename, filetype, fileext, filesize, filetempname, dateadded, timeadded, fileguid, sentmailid) VALUES ('$file_name', '$file_type', '$extension', '$file_size', '$temp_name', '$getthedate', '$gettime', '$guid', '$lastid')";
$expire = mysql_query("UPDATE ".EMAILS." SET showinverify = 0 WHERE expireit < CURDATE()");
if (!mysql_query($sqlone,$conn))
{
die("Error: " . mysql_error().".");
}
header ("refresh: 5; url=$url");
include ("header.php");
$ful = (move_uploaded_file($temp_name, $target)) ? "".$file_name." was uploaded to ".$target."" : "".$file_name.", was not uploaded. Please try a manual upload.";
echo "<title>Success sending email</title>";
echo "Your message has been successfully sent. Message details have been added to the database.<p>$ful";
include ("footer.php");
}
else
{
header ("refresh: 5; url=$url");
include ("header.php");
echo "<title>Error sending email</title>";
echo "There seems to be an error sending your email.";
include ("footer.php");
}
exit;
}
else
{
header ("refresh: 5; url=attach.php");
include ("header.php");
echo "File type is not allowed. Please go <a href='attach.php'>back</a>";
include ("footer.php");
exit;
}
}
}
}
我遇到的问题是,当我尝试通过这种方式发送电子邮件时,出现以下错误:
The problem I run in to is when I try to send an email this way, I get the following error:
Warning: Invalid argument supplied for foreach() in /home/flattley/public_html/chriswilcox/cab/attach.php on line 150
第150行是这样:
foreach ($file as $files)
我只是不知道我要去哪里错了.如果我没有foreach循环,则该循环不会发送,或者只会发送第一个文件.
I just don't have any idea where I am going wrong. If I don't have the foreach loop there it doesn't send or it will only send the first file.
我知道我可以使用类似phpmailer的工具,但是我希望自己能够做到这一点.我只是看不到我要去哪里错了.
I know I could use something like phpmailer, but I want to be able to do this on my own. I just can't see where I am going wrong.
我在家里没有邮件的情况下在本地apache上尝试了该脚本,它可以上载和移动多个文件,就像在主机上一样.当我添加邮寄选项时,一切都变成了梨形.
I have tried the script on my local apache at home without mailing it and it uploads and moves multiple files just fine - as it does on my host. It's when I add the mailing option that it all goes pear shaped.
我们将不胜感激
推荐答案
如果您回头看代码:
foreach ($_FILES['attachment']['name'] as $file)
$file
已经是一个文件名.它不是数组.如前所述,我的 php文档.
$file
is already a single file name. It is not an array. As mentioned my php documentation.
我看到在您的代码中,您尝试获取上载文件的内容.为此,您可以执行以下操作:
I see that in your code, you try to get the content of uploaded files. To do that, you can do something like this:
foreach($_FILES['attachment']['tmp_name'] as $eachFile){
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$from."\r\n";
$header .= "Bcc: ".$bcc."\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-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message1."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n";
}
这篇关于PHP中的多个邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!