2011-07-14 60 views
1

我一直在使用PHPmailer创建一个文件上传表单作为附件发送。为什么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 = "[email protected]"; // the one that recieves the email 
$email_from = "[email protected]"; 
$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); 
} 
// 
} 
// 
?> 

伊夫检查,该文件被保存在目录“上传”。这里是我收到的错误:

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

相关:http://stackoverflow.com/questions/6695756/why-wont-my-phpmailer-script-work-to-get-save-files –

回答

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为您创建,并将其命名为与任何原始文件名是。

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

+0

看上面 - 当使用你的更好的代码时,我添加了新的错误。再次感谢。 –

+0

做一个'var_dump($ _ FILES)',它会告诉你数组的确切结构。 –

+0

好的。这里: 'array(1){[“images”] => array(5){[“name”] => array(1){[0] => string(8)“logo.png”} [ “type”] => array(1){[0] => string(9)“image/png”} [“tmp_name”] => array(1){[0] => string(18)“/ var/tmp/phpozhSdq“} [”error“] => array(1){[0] => int(0)} [”size“] => array(1){[0] => int(56306)} }} –