How to check mail is send or not in php

In PHP, you can use the mail() function to send an email, but it doesn't provide a built-in way to check if the email was sent successfully or not. However, there are a few ways to achieve this:

  1. Using the mail() function with a callback function: You can pass a callback function as the last argument to the mail() function. This function will be called after the email is sent. If the email is sent successfully, the callback function will be executed. Here's an example:
    
    $to = '[email protected]';
    $subject = 'Test email';
    $message = 'This is a test email';

if (mail($to, $subject, $message, 'From: [email protected]')) { echo 'Email sent successfully!'; } else { echo 'Error sending email!'; }

In this example, the callback function is a simple `echo` statement that prints a success message if the email is sent successfully.

2. **Using a third-party library**:
There are several PHP libraries available that provide a way to send emails and check if they were sent successfully. Some popular ones include:

* PHPMailer: A popular PHP email library that provides a way to send emails and check if they were sent successfully.
* SwiftMailer: Another popular PHP email library that provides a way to send emails and check if they were sent successfully.

Here's an 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!';
}

In this example, the send() method returns a boolean value indicating whether the email was sent successfully or not.

  1. Using a custom solution: You can also create a custom solution to check if an email was sent successfully by using a combination of the mail() function and a database or a file to store the email status. Here's an example:
    
    $to = '[email protected]';
    $subject = 'Test email';
    $message = 'This is a test email';

// Send the email using the mail() function if (mail($to, $subject, $message, 'From: [email protected]')) { // Store the email status in a database or file $email_status = 'sent'; // Update the database or file //... } else { $email_status = 'failed'; // Update the database or file //... }

// Check the email status if ($email_status == 'sent') { echo 'Email sent successfully!'; } else { echo 'Error sending email!'; }


In this example, the email status is stored in a database or file, and then checked later to determine if the email was sent successfully or not.

Note that the `mail()` function is not reliable and may not work in all cases, especially if the recipient's email server is down or if there are network issues. It's always a good idea to use a third-party library or a custom solution to ensure that your emails are sent successfully.