[SOLVED] Could not instantiate mail function in PHPMailer

(This article has an advanced technical level, written for people with PhpMailer experience.)

PHPMailer 5.1 in PrestaShop 1.4

Trying to integrate PHPMailer in Prestashop and use it instead of SWIFT Mailer 3 (which is the default mailer script included with Prestashop 1.4), I spent hours to no avail and was getting the Could not instantiate mail function error.

mail() function

The mail() function by itself ¬†was working correctly before and after loading Prestashop and every passed argument seemed to be correct. I did get the “Bad parameters to mail() function, mail not sent” error for mail() too.

Debugging and Abracadabra!

I’ve done lots of debugging and found in my case that the following functions were causing the error, when passing the $Subject parameter to mail():

SecureHeader();
EncodeHeader();

To solve the “Could not instantiate mail function” error in PHPMailer, I removed the above 2 functions from the mail() function arguments.

Just be careful to remove these functions correctly from all places in your class.phpmailer.php.

I recommend using the PHP function

mb_encode_mimeheader()

http://php.net/manual/en/function.mb-encode-mimeheader.php

because it encodes your stuff correctly for the headers in emails.

To be more clear,

1- open the PHPMAILER class file (class.phpmailer.php)

2- Locate:

protected function MailSend($header, $body)

function and in its body,

3- In every call to mail(),

replace this:

@mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);

by this:

@mail($val, mb_encode_mimeheader($this->Subject,$this->CharSet, 'B', ''), $body, $header, $params);

 

This way you are sure to correctly encode your subjet.

Remember to set the correct Charset for your PHPMAILER instance. That is, when calling PHPMailer, just after:

$mail=new PHPMailer(true);

add:

$mail->CharSet = 'UTF-8';

(or any other charset you need).

This solved my “Could not instantiate mail() function in PHPMailer issue. I hope it helps you out if you got this issue too.

Evenmore:

I’ll write more about PHPMailer and PrestaShop later,

In my next post, learn how to correctly include inline images in PHPMailer email to display in GMAIL, YAHOO, Mac Mail app and other email clients correctly as inline images.

5 Replies to “[SOLVED] Could not instantiate mail function in PHPMailer”

  1. Gopal, just want to clarify where could i find the File for the line of code below?

    $mail=new PHPMailer(true);

    so that i could add this line as you mention

    $mail->CharSet = ‘UTF-8’;

    I’m using joomla 2.5.8

    Thank you in advance and more power..

  2. I discovered that if I had % character in the message the mail wouldn’t send using mail or phpmailer $mail->Send(). I would get the same error message “could not instantiate….mail..”. When I removed the % via $content = str_replace(“%”,””,$content); the message sent without a problem. I did try all the suggested remedies, nothing worked until I removed the ‘%’ character. Very strange obviously the mail() function must be interpreting the % character and it was causing it problems. The % character was surrounded by whitespace e.g. “100 % certain” . I’m am going to investigate further and see if i can resolve it. I think it must be a operating system issue but i’m not certain about this.

Comments are closed.