为什么PHPmailer不发送附件?

时间:2011-07-14 17:02:27

标签: php phpmailer email-attachments

我一直致力于使用PHPmailer创建一个文件上传表单作为附件发送。

我终于得到它发送电子邮件,但它没有发送附件。这是我的HTML表单:

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

这是我的php处理器代码:

<?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);
}
//
}
//
?>

我已经检查过,文件IS保存在“uploads”目录中。以下是我收到的错误:

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: 

如果有人能够发现错误或提供一些输入,那将是如此有用!提前致谢!

约拿


我已经取代了

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

使用

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);
}

现在这是我的新错误:

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

1 个答案:

答案 0 :(得分:3)

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

$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为您创建的临时文件,并使用原始文件名命名。

你的脚本远比它需要的复杂得多。

相关问题