Could not instantiate mail function. Why this error occurring
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 PH开发者_如何学GoPMailer libary in our script
$mail = new PHPMailer(); // Next we create a new object of the PHPMailer called $mail
$mail->From = "rajasekar.kcet@gmail.com";
$mail->FromName = "Rajasekar";
$mail->AddAddress("rajasekar.kcet@gmail.com"); // 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.';
}
?>
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.
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.
I just had this problem and found in my apache error log that sendmail was'nt installed, after installation it was all working as it should!
root@web1:~$ tail /var/log/apache2/error.log
sh: 1: /usr/sbin/sendmail: not found
Try using SMTP to send email:-
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Had same problem. Just did a quick look up apache2 error.log
file and it said exactly what was the problem:
> sh: /usr/sbin/sendmail: Permission denied
So, the solution was to give proper permissions for /usr/sbin/sendmail
file (it wasn't accessible from php).
Command to do this would be:
> chmod 777 /usr/sbin/sendmail
be sure that it even exists!
Make sure that you also include smtp class which comes with phpmailer:
// for mailing
require("phpmailer/class.phpmailer.php");
require("phpmailer/class.smtp.php");
To revisit an old thread, my issue was that one of the "AddressTo" email addresses was not valid. Removing that email address removed the error.
Just revisiting the old thread you can debug PHPMailer in depth by adding:
print_r(error_get_last());
this will print out the exact error for you which is causing the default php mail() to break.
Hope it helps somebody.
Try with an address that is not gmail. They do not allow (as far as i know) smpt access to send mail from. I was doing a simple mail program last week and they also dont use default ports to send from and require that you transport across https
Check with your host to see if they have any hourly limits to emails being sent.
This a system error.
Check error of the system with:
tail /var/log/httpd/error_log
It can be any reason.
As noted here, "This means that your PHP installation is not configured to call the mail() function correctly (e.g. sendmail_path is not set correctly in your php.ini), or you have no local mail server installed and configured."
In my case I had to allow the mail() function ("activate the mail() queue") in the settings of my webhost.
"Could not instantiate mail function" is PHPMailer's way of reporting that the call to mail() (in the Mail extension) failed. (So you're using the 'mail' mailer.)
You could try removing the @s before the calls to mail() in PHPMailer::MailSend and seeing what, if any, errors are being silently discarded.
i have resolved my problem (for wamp)
$mail->IsSMTP();
$mail->Host='hote_smtp';
of corse change hote_smtp by the right value
An old thread, but it may help someone like me. I resolved the issue by setting up SMTP server value to a legitimate value in PHP.ini
I had the same error. The Reply-to was causing the problem. I removed it.
$email->AddReplyTo( $admin_email, $admin_name );
In CentOS this might be caused by the SELinux policy. Execute the following code to see if it is enabled.
getsebool httpd_can_sendmail
You can enable it by calling the command below. The -P parameter makes it permanent.
setsebool -P httpd_can_sendmail 1
精彩评论