PHP Mail() Function Server and Localhost Not Working

Why mail() PHP function does not work with WAMP default installation?

To be able to send email you need an outgoing email server (MTA). In most Linux systems there exists one by default, and PHP will use it by submitting mail to sendmail, a Linux app/alias for submitting mail to whichever MTA you have installed.

Windows doesn't include an MTA by default. In Windows, to be able to send mail from PHP you need to have access to some outgoing email server and tell PHP the address and port of it. This is done in php.ini using the SMTP and smtp_port settings. It will default to localhost on port 25. Unless you have set up a mail server on that machine yourself, this will fail.

If your ISP gives you an outgoing mail server, for example, you could use its address and port number. Or, if you're serious about sending mail, you'd set up your own mail server on the local machine or somewhere in your local network.

PHP mail function is not working on the server

It returns FALSE, it means there is a problem with your mail configuration. You would need to check the mail log to see if you are getting any errors. The kicker is that php may be handing off the mail, your server may be sending off the mail, and it may get spam filtered along the way.

<?php 
mail('nobody@example.com', 'the subject', 'the message', null, '-fwebmaster@example.com');
?>

php mail function not working in new server

There is a library called PHPMailer. You could use it instead of the native mail() function. This is how the code looks like with PHPMailer

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';
// here you create the mail object
$mail = new PHPMailer(true);

I suggest using composer to install the library and its dependencies.
But, since your app is already in the server, you can just download if from GitHub and put it manually there

More information:
https://github.com/PHPMailer/PHPMailer



Related Topics



Leave a reply



Submit