Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not instantiate mail function. Why this error occurring

Tags:

When I'm trying to send mail through PHPMailer, i'm getting this error message. My code is below:

<?
require("phpmailer/class.phpmailer.php"); // First we require the PHPMailer libary in our script
$mail = new PHPMailer(); // Next we create a new object of the PHPMailer called $mail
$mail->From = "[email protected]";
$mail->FromName = "Rajasekar";
$mail->AddAddress("[email protected]"); // This is the adress to witch the email has to be send.
$mail->Subject = "First PHP Email message"; // This is the subject  of the email message.
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHP."; // This is the actual email message
if(!$mail->Send()) // Now we send the email and check if it was send or not.
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Message has been sent.';
}
?>
like image 558
Rajasekar Avatar asked Dec 22 '09 07:12

Rajasekar


People also ask

What does Could not instantiate mail function mean?

This message means that your mail server (the mailing part of your host) failed to send an email.

Why is my PHP mail function not working?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

How can I get the error message for the mail function?

There is no error message associated with the mail() function. There is only a true or false returned on whether the email was accepted for delivery. Not whether it ultimately gets delivered, but basically whether the domain exists and the address is a validly formatted email address.


2 Answers

In Ubuntu (at least 12.04) it seems sendmail is not installed by default. You will have to install it using the command sudo apt-get install sendmail-bin

You may also need to configure the proper permissions for it as mentioned above.

like image 84
Michael Yagudaev Avatar answered Oct 16 '22 11:10

Michael Yagudaev


I used this line of code

if($phpMailer->Send()){

    echo 'Sent.<br/>';

}else{

    echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';

}

to find out what the problem was. Turns out, I was running in safe-mode, and in line 770 or something, a fifth argument, $params, was given to mail(), which is not supported when running in safe-mode. I simply commented it out, and voilá, it worked:

$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header/*, $params*/);

It's within the MailSend-function of PHPMailer.

like image 42
arik Avatar answered Oct 16 '22 11:10

arik