2011-07-14 26 views
1

私は添付ファイルとして送るためにPHPmailerを使ってファイルアップロードフォームを作成しています。PHPmailerが添付ファイルを送信しないのはなぜですか?

最終的にIveはメールを送信するようにしましたが、添付ファイルを送信しませんでした。ここに私の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(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); 
} 

そして今、ここで

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

を交換し、私の新しいエラーです:

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

ここで: {["images"] => array(5){["name"] => array(1){[0] => string(8) "logo.png"} [ "[tmp_name"] =>配列(1){[0] =>文字列(18) "/ var" (1){[0] => int(56306)}/tmp/phpozhSdq "} ["エラー "] =>配列(1){[0] => int(0) }} ' –

関連する問題