2015-04-01 89 views
0

我一直在尝试一段时间来让我的头在这附近,我不知道。我试图写一个简单的表单来发送带有上传文件的电子邮件(它最终会扩展成实际有用的东西),而且根本不起作用。PHPMailer拒绝发送附件

电子邮件是通过适当的机构,但没有附件被包括在内。我已经用文件上传的形式尝试过了,AddAttachments链接到服务器上的一个文件,AddAttachments指向imgur上的图像,它们都不起作用;附件永远不会通过。我现在对我的耐心已经结束了,有没有人知道我在做什么错误或者没有phpmailer的方法?

HTML表单

<form action="xxxx.php" id="upload" method="post" name="upload"> 
<input id="fileToUpload" name="fileToUpload" type="file" /> 
<input type="submit" /> 
</form> 

PHP代码

require("../../../classes/class.phpmailer.php"); 
$mail = new PHPMailer(); 

$mail->From  = "[email protected]"; 
$mail->FromName = "Uploader"; 
$mail->AddAddress("[email protected]"); 

$mail->Subject = "First PHPMailer Message"; 
$mail->Body  = "Hi! \n\n This is my first e-mail sent through PHPMailer."; 
$mail->WordWrap = 50; 
$mail->AddAttachment($_FILES['fileToUpload']['tmp_name'], $_FILES['fileToUpload']['name']); 

if(!$mail->Send()) { 
    echo 'Message was not sent.'; 
    echo 'Mailer error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent.'; 
+0

这可能对你有用:http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data。我认为你需要在表单上使用'enctype =“multipart/form-data”'。 – potame 2015-04-01 13:50:25

回答

0

看你的表格你没有ENCTYPE = “的multipart/form-data的” 在你的表单标签设置。

此外,您需要在发送电子邮件之前进行文件附件检查以确保其实际连接。举一个例子,

if (isset($_FILES['uploaded_file']) && 
    $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) { 
    $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'], 
         $_FILES['fileToUpload']['name']); 
} 
+0

我已经将enctype添加到表单中,并且该附件检查到php并且它仍然不起作用。我仍然收到没有附件的电子邮件。 – AlexRS 2015-04-01 13:59:11

0

你正在使用一些旧例,和旧版本的PHPMailer的,所以我建议你update to the latest。您还需要知道how to handle file uploads,这是你所缺少的。下面是the example bundled with PHPMailer

<?php 
/** 
* PHPMailer simple file upload and send example 
*/ 
$msg = ''; 
if (array_key_exists('userfile', $_FILES)) { 
    // First handle the upload 
    // Don't trust provided filename - same goes for MIME types 
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation 
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'])); 
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
     // Upload handled successfully 
     // Now create a message 
     // This should be somewhere in your include_path 
     require 'PHPMailerAutoload.php'; 
     $mail = new PHPMailer; 
     $mail->setFrom('[email protected]', 'First Last'); 
     $mail->addAddress('[email protected]', 'John Doe'); 
     $mail->Subject = 'PHPMailer file sender'; 
     $mail->msgHTML("My message body"); 
     // Attach the uploaded file 
     $mail->addAttachment($uploadfile, 'My uploaded file'); 
     if (!$mail->send()) { 
      $msg = "Mailer Error: " . $mail->ErrorInfo; 
     } else { 
      $msg = "Message sent!"; 
     } 
    } else { 
     $msg = 'Failed to move file to ' . $uploadfile; 
    } 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"/> 
    <title>PHPMailer Upload</title> 
</head> 
<body> 
<?php if (empty($msg)) { ?> 
    <form method="post" enctype="multipart/form-data"> 
     <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file"> 
     <input type="submit" value="Send File"> 
    </form> 
<?php } else { 
    echo $msg; 
} ?> 
</body> 
</html> 
+0

我正在努力在现有网站上做一个小小的增加,这个网站已经设置了相当数量并且时间有限,所以将所有内容更改为不同版本的phpmailer听起来都不太实际。 – AlexRS 2015-04-01 14:39:37

+0

我试过运行那个例子,我得到的响应 “无法将文件移动到/ tmp/88d542d578b1fc7dae265b22296701a217343830LIhRZ0” – AlexRS 2015-04-01 14:39:50

+0

因此,您的系统的临时文件夹已损坏,或者您的Web服务器无法对其进行写入访问 - 修正它们它会工作。更新是将旧版本更换为新的并更改一行代码的问题。 – Synchro 2015-04-01 14:43:14