How to check php mail working on server linux

To check if PHP mail is working on a Linux server, you can follow these steps:

  1. Check the PHP configuration: Make sure that the PHP mail function is enabled and configured correctly. You can do this by checking the php.ini file. Look for the following settings:
    • sendmail_path: This should point to the location of the sendmail binary (usually /usr/sbin/sendmail or /usr/lib/sendmail).
    • SMTP: This should be set to the SMTP server you want to use (e.g., smtp.gmail.com).
    • mail.log: This should be set to the location where PHP will log mail errors (e.g., /var/log/php-mail.log).
  2. Test the PHP mail function: Create a simple PHP script that sends an email using the mail() function. For example:
    
    <?php
    $to = '[email protected]';
    $subject = 'Test email';
    $message = 'This is a test email';
    $headers = 'From: [email protected]'. "\r\n".
            'Reply-To: [email protected]'. "\r\n".
            'MIME-Version: 1.0'. "\r\n".
            'Content-Type: text/plain; charset=UTF-8';

if (mail($to, $subject, $message, $headers)) { echo 'Email sent successfully!'; } else { echo 'Error sending email!'; } ?>


Save this script as `test-mail.php` and upload it to your server.

3. **Run the PHP script**: Run the script using the command line or a web browser. If the email is sent successfully, you should see the message "Email sent successfully!".
4. **Check the mail log**: Check the mail log file specified in the `mail.log` setting in your `php.ini` file. This will show you any errors that occurred during the email sending process.
5. **Check the email inbox**: Check the recipient's email inbox to see if the email was received.

Some common issues that may prevent PHP mail from working include:

* **Incorrect sendmail path**: Make sure that the `sendmail_path` setting in your `php.ini` file points to the correct location of the sendmail binary.
* **SMTP server issues**: Make sure that your SMTP server is configured correctly and that you have the correct credentials to send emails.
* **Firewall or network issues**: Make sure that your server is not blocked by a firewall or network issue that prevents it from sending emails.
* **PHP mail function not enabled**: Make sure that the PHP mail function is enabled in your `php.ini` file.

If you're still having trouble, you can try using a third-party email library like PHPMailer or SwiftMailer, which can help you troubleshoot and debug email sending issues.