无法实例化邮件功能 - PHPMailer - 附件 - 仅限Google Chrome

时间:2015-06-08 14:43:06

标签: php google-chrome email phpmailer

我有一个奇怪的问题,我正在使用PHPMailer发送一个带有一些附件的电子邮件。附件使用jQuery上传到表单,工作正常,文件上传。当我使用IE和Firefox时,电子邮件发送没有问题,但是当我使用谷歌浏览器时,我收到错误消息"无法实例化邮件功能",我认为这很奇怪。

然而,当我删除脚本的附件部分时,电子邮件在所有浏览器中都发送正常,因此我认为当PHPMailer尝试将文件附加到表单时会出现问题,因此我运行了一些测试以确保文件肯定是上传并存在,这些测试回来了。

<?php
$attachment_location = "files/";
$attachments = $_POST["files"];
require_once('../includes/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer(true); 
try {
$mail ->isMail();
$body = '    
<html>
    <head>
        <title>New Account Application Submitted</title>
        <style type="text/css">
            body {
                font-family: Arial;
            }
        </style>
    </head>
    <body>
        <h1>New Account Application Submitted</h1>
        <p>A user completed the account application form please see the attached documents</p>
        <p>Thank You</p>
    </body>
</html>
        ';
$mail->SetFrom('myemail@email.com');
$address = "myemail@email.com";
$mail->AddAddress($address);
$mail->Subject = "New Account Application Submitted";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
$mail->MsgHTML($body);
foreach($attachments as $attachment){
    if(file_exists($attachment_location . $attachment)){ 
        $mail->AddAttachment($attachment_location . $attachment);  
    }else{
        echo "File does not exist " . $attachment_location . $attachment . "<br>";
    }
}
$mail->Send();
} catch (phpmailerException $e) {
  echo $e->errorMessage();
} catch (Exception $e) {
  echo $e->getMessage();
}
foreach($attachments as $attachment){
    unlink($attachment_location . $attachment);  
}
echo "Your application has been received<br> a member of our team will be in contact with you shortly";
?>

1 个答案:

答案 0 :(得分:0)

您没有正确处理文件上传。阅读PHP file uploading docs,并按照PHPMailer提供的send file upload example进行操作。

确保您的表单具有enctype="multipart/form-data"属性,并且您包含<input type="hidden" name="MAX_FILE_SIZE" value="30000" />输入标记。

然后使用move_uploaded_file,不要尝试直接使用$_FILES的内容。