Could Not Instantiate Mail Function - PHPMailer - With Attachments - Only Google Chrome

133 views Asked by At

I have an odd problem, I am using PHPMailer to send an email with a few attachements. The attachments are uploaded to a form using jQuery which works fine and the files get uploaded. When I use IE and Firefox the emails is sent without a problem however when I use Google Chrome I get the error message "Could Not Instantiate Mail Funcition", which I thought was odd.

However when I remove the attachment part of the script the email sends fine in all browsers, therefore I would presume that the problem occurs when PHPMailer tries to attach the files to the form therefore I ran some tests to make sure that the files are definitely uploaded and do exist and those tests came back fine.

<?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('[email protected]');
$address = "[email protected]";
$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

There are 1 answers

1
Synchro On

You're not handling your file uploads correctly. Read the PHP file uploading docs, and follow the send file upload example provided with PHPMailer.

Make sure that your form has an enctype="multipart/form-data" attribute and that you include a <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> input tag.

Then use move_uploaded_file and don't try to use the contents of $_FILES directly.