tags:

views:

745

answers:

6

Hi there,

I have the following code with PHPmailer:

$tomailn[0] = '[email protected]';
$tomailn[1] = '[email protected]';
foreach($tomailn as $value)
{
$mail->AddAddress($value, '');
}

But I am getting the error 'Could not instantiate mail function'.

If I remove an item from the array it works fine, but gives an error on when trying to add 2 or more addresses. Any ideas why this is happening? Is there a different way to add multiple e-mail addresses?

Cheers, Gazler.

A: 

did you try just $mail->AddAddress($value); ?

jellyfishtree
Yes, makes no difference, thanks for the suggestion though.
Gazler
A: 

Most of the times this error is caused when from header is not set or is invalid. Try setting this variable:

$mail->From = '[email protected]';

If it still doesn´t work, try one of the following:

  • check if the mail is enabled in the server (and the php.ini settings);
  • the openssl module is enabled (execute a phpinfo() and search for OpenSSL)
Luiz Damim
All three of these conditions are fine, I am sure the problem lies with the loop as an array with a single element works fine.
Gazler
A: 

I just looked through the source code of PHPMailer, the message "Could not instantiate mail function" indicates that mail() returns false.

Can you try the same function, but with two different email-addresses that you know usually accepts emails?

chelmertz
+2  A: 

Dig into the source code. Edit PHPMailer.php and find "function MailSend". (In 5.0.2, it's around line 564.)

In said function, remove the @ error suppressor from each call to mail(). Make sure error_reporting is set to something reasonable for debugging. When developing, choose something like this:

error_reporting(E_ALL | E_STRICT);
ini_set('log_errors', 'On');
ini_set('display_errors', 'On');

See if PHP shows any errors. PHPMailer only throws the instantiate exception when the last call to mail() returns something falsey, or if $rt never gets set, which would mean that if ($this->Sender != '' && strlen(ini_get('safe_mode'))< 1) evaluates to true.

Are you using safe mode? What do PHP Mailer $mailer->Sender and ini_get('safe_mode') say? (My guess: if you are not running in safe mode, but have it set to something like Off, this code would return true.)

janmoesen
<b>Warning</b>: mail() [<a href='function.mail'>function.mail</a>]: SAFE MODE Restriction in effect. The fifth parameter is disabled in SAFE MODE in <b>/home/gazler/public_html/ahem/includes/php/class.phpmailer-lite.php</b> on line <b>590</b><br />Could not instantiate mail function.Mailer Error: Could not instantiate mail function.
Gazler
As a result of the error, I removed the firth parameter and it mailed fine, the only issue is that it doesn't show all the recipients, although all do receive the e-mail.
Gazler
Are you able to use PHPMailer's own SMTP transport? (I am not familiar enough with PHPMailer and safe_mode, sorry.)
janmoesen
A: 

Try downloading latest version of PHPMailer if you are not using one, it has bug fixes. Chances are that your mailer class has been messed with.

Sarfraz
A: 

'Could not instantiate mail function'. Just check your mailer function working or not. many time this error cause due to limitation from hosting provider .many time Hosting provider block your mailing feature then usually you get this error

vinayrks