为什么PHPmailer不发送附件? [英] Why is PHPmailer not sending the attachment?

查看:56
本文介绍了为什么PHPmailer不发送附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用PHPmailer创建文件上传表单以作为附件发送.

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

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

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" />

这是我的php处理器代码:

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!

约拿

我已替换

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

推荐答案

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

As I said in your other question, the first warning is due to you using $filename in line 10 of your script, without having assigned a value to it first:

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

同样,对于您的附件,为什么不简单地做:

As well, for your attachments, why not simply do:

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为您创建的临时文件,然后使用原始文件名命名即可.

There's no need to do all the file copying, building your own paths, etc... Just directly attach the temporary file PHP creates for you, and name it with whatever the original filename was.

您的脚本要复杂得多.

这篇关于为什么PHPmailer不发送附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆