1

Ive been working on create a file upload form using PHPmailer to send as attachments.

Ive finally got it to send the email, but its not sending the attachment. Here's my HTML form:

<input type="file" class="fileupload" name="images[]" size="80" />

And here's my php processor code:

<?php
require("css/class.phpmailer.php");
//Variables Declaration
$name = "the Submitter";
$email_subject = "Images Attachment";
$Email_msg ="A visitor submitted the following :\n";
$Email_to = "jonahkatz@yahoo.com"; // the one that recieves the email
$email_from = "someone@someone.net";
$attachments = array();
//
//
//------Check TYPE------\\
uploadFile();
//
//==============upload File Function============\\
//
function uploadFile() {
global $attachments;
foreach($_FILES['images']['name'] as $key => $value)
{
//
if(!empty($value))
{
$filename = $value;
//the Array will be used later to attach the files and then remove them from ser
ver ! array_push($attachments, $filename);
$dir = "uploads/$filename";
$success = copy($_FILES['images']['tmp_name'][$key], $dir);
}
//
}
$dir ="uploads/$filename";


if ($success) {
echo " Files Uploaded Successfully<BR>";
SendIt();
//
}else {
exit("Sorry the server was unable to upload the files...");
}
//
}
//
//==== PHP Mailer With Attachment Func ====\\
//
function SendIt() {
//
global $attachments,$name,$Email_to,$Email_msg,$email_subject,$email_from;
//
$mail = new PHPMailer();
$mail->IsQmail();// send via SMTP
$mail->From = $email_from;
$mail->FromName = $name;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;// set word wrap
//now Attach all files submitted
foreach($attachments as $key => $value) { //loop the Attachments to be added ...
$mail->AddAttachment("uploads"."/".$value);
}
$mail->Body = $Email_msg."Name : ".$name"\n";
//
$mail->IsHTML(false);// send as HTML
$mail->Subject = $email_subject;
if(!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
//
echo "Message has been sent";
// after mail is sent with attachments , delete the images on server ...
foreach($attachments as $key => $value) {//remove the uploaded files ..
unlink("uploads"."/".$value);
}
//
}
//
?>

Ive checked, and the file IS being saved in the directory "uploads". Here are the errors im receiving:

Files Uploaded Successfully
Message was not sent


Notice: Undefined property: phpmailer::$ErrorInfo in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 69
Mailer Error: 

If anyone can spot the errors or provide some input how to this that would be so helpful! Thanks in advanced!

Jonah


Ive replaced

foreach($attachments as $key => $value) { //loop the Attachments to be added ...
$mail->AddAttachment("uploads"."/".$value);

With

foreach(array_keys($_FILES['files']['name']) as $key) {
   $source = $_FILES['files']['tmp_name'][$key]; // location of PHP's temporary file for this.
   $filename = $_FILES['files']['name'][$key]; // original filename from the client

   $mail->AddAttachment($source, $filename);
}

And now here are my new errors:

Notice: Undefined index: files in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 58

Warning: array_keys() expects parameter 1 to be array, null given in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 58

Warning: Invalid argument supplied for foreach() in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 58

Strict Standards: Creating default object from empty value in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 68

Fatal error: Call to undefined method stdClass::IsHTML() in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 70
4

1 回答 1

3

正如我在您的另一个问题中所说,第一个警告是由于您$filename在脚本的第 10 行中使用,而没有先为其分配值:

$dir ="uploads/$filename"; // $filename has NOT been defined at this point.

同样,对于您的附件,为什么不简单地执行以下操作:

foreach(array_keys($_FILES['files']['name']) as $key) {
   $source = $_FILES['files']['tmp_name'][$key]; // location of PHP's temporary file for this.
   $filename = $_FILES['files']['name'][$key]; // original filename from the client

   $mail->AddAttachment($source, $filename);
}

无需进行所有文件复制、构建自己的路径等...只需直接附加 PHP 为您创建的临时文件,并使用原始文件名对其进行命名。

您的脚本远比它需要的复杂。

于 2011-07-14T17:16:47.597 回答