How to check if mail is working php

There are several ways to check if mail is working in PHP:

  1. Using the mail() function: You can use the mail() function to send a test email and check if it's delivered successfully.

Example:

<?php
$to = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email';

if (mail($to, $subject, $message)) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email!';
}
?>
  1. Using a mail testing service: There are several online services that allow you to test your email sending functionality, such as Mailinator, Mailtest, or Sendmail Test. You can use these services to send a test email and check if it's delivered successfully.

  2. Using a PHP mail library: You can use a PHP mail library such as PHPMailer or SwiftMailer to send a test email and check if it's delivered successfully.

Example using PHPMailer:

<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';

$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');

$mail->Subject = 'Test email';
$mail->Body = 'This is a test email';

if ($mail->send()) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email!';
}
?>
  1. Checking the mail log: You can check the mail log on your server to see if the email was sent successfully. The location of the mail log varies depending on your server configuration.

Example:

<?php
$log_file = '/var/log/mail.log';
if (file_exists($log_file)) {
    $log_contents = file_get_contents($log_file);
    if (strpos($log_contents, ' Sent')!== false) {
        echo 'Email sent successfully!';
    } else {
        echo 'Error sending email!';
    }
} else {
    echo 'Mail log not found!';
}
?>
  1. Using a PHP script to check the email: You can write a PHP script that sends a test email and then checks if the email is delivered successfully by checking the email inbox.

Example:

<?php
$to = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email';

if (mail($to, $subject, $message)) {
    $email_contents = file_get_contents('php://input');
    if (strpos($email_contents, $message)!== false) {
        echo 'Email sent successfully!';
    } else {
        echo 'Error sending email!';
    }
} else {
    echo 'Error sending email!';
}
?>

Note: Make sure to replace the placeholders (e.g. [email protected], [email protected], your_password) with your actual email addresses and passwords.