20

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 = "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.';
}
?>
Cœur
  • 32,421
  • 21
  • 173
  • 232
Rajasekar
  • 201
  • 1
  • 2
  • 3

17 Answers17

42

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.

Michael Yagudaev
  • 5,629
  • 2
  • 43
  • 50
13

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.

arik
  • 23,480
  • 35
  • 91
  • 147
7

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
Jacta
  • 497
  • 6
  • 17
3

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!

Nayan
  • 3,018
  • 2
  • 14
  • 32
slawkens
  • 99
  • 6
  • 3
    are you sure you want to give the whole world writable access to sendmail? I think this could be a vulnerability. Perhaps try just giving executable access: `chmod +x /usr/sbin/sendmail` – Jeff Puckett Apr 19 '16 at 21:13
2

Make sure that you also include smtp class which comes with phpmailer:

// for mailing
require("phpmailer/class.phpmailer.php");
require("phpmailer/class.smtp.php");
Sarfraz
  • 355,543
  • 70
  • 511
  • 562
2

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.

Greg A
  • 481
  • 4
  • 9
2

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';
Mukesh Chapagain
  • 22,983
  • 12
  • 108
  • 114
1

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

Kieran
  • 15,368
  • 4
  • 41
  • 51
1

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.

Shuja Ahmed
  • 744
  • 5
  • 16
  • This helped me! Here is the full error from the `print_r`: `Array ( [type] => 2 [message] => mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() [file] => ...\vendor\phpmailer\phpmailer\src\PHPMailer.php [line] => 852 ) ` – dmikester1 Feb 29 '20 at 04:22
  • So your SMTP is not configured properly. As the error shows you can configure the SMTP server settings wither by mentioning port,host,username and other credentials in php.ini or specifying them inline through ini_set(). I would recommend reading up SMTP documentation to set it up properly. A little Googling will definitely help! – Shuja Ahmed Feb 29 '20 at 06:17
  • 1
    Thanks I was under the impression that PHPMailer could do the SMTP itself. I am using my gmail smtp credentials and that is working now. – dmikester1 Mar 01 '20 at 03:59
1

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.

Fanky
  • 1,152
  • 12
  • 18
1

This a system error.

Check error of the system with:

tail /var/log/httpd/error_log

It can be any reason.

Jérôme Verstrynge
  • 51,859
  • 84
  • 263
  • 429
Alexey
  • 11
  • 1
1

Check with your host to see if they have any hourly limits to emails being sent.

Sir Lojik
  • 1,341
  • 6
  • 22
  • 44
  • yep check your error_log: sendmail: 550 E-mail not accepted due to policy violation: recipient limit reached, xxx >= xxx per day. – mewiki Dec 14 '16 at 10:57
0

i have resolved my problem (for wamp)

    $mail->IsSMTP(); 

    $mail->Host='hote_smtp'; 

of corse change hote_smtp by the right value

khaled_webdev
  • 1,380
  • 14
  • 18
0

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

Atif.SQL
  • 1
  • 4
0

"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.

Frank Shearar
  • 16,631
  • 8
  • 63
  • 90
0

I had the same error. The Reply-to was causing the problem. I removed it.

$email->AddReplyTo( $admin_email, $admin_name ); 
Zeke
  • 382
  • 2
  • 11
0

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