How to check email send successfuly through mail function

When using the mail() function in PHP to send an email, you can check if the email was sent successfully by checking the return value of the mail() function, which is a boolean value indicating whether the email was sent or not.

Here's an example:

$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!';
}

In this example, if the mail() function returns true, it means the email was sent successfully, and the script will output "Email sent successfully!". If the function returns false, it means there was an error sending the email, and the script will output "Error sending email!".

You can also use the error_get_last() function to get more information about the error, if any:

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

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

This will output the error message if there was an error sending the email.

Additionally, you can also check the email logs on your server to see if the email was sent successfully. The location of the email logs may vary depending on your server configuration.

It's also a good practice to use a try-catch block to catch any exceptions that may occur while sending the email:

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

try {
    mail($to, $subject, $message);
    echo 'Email sent successfully!';
} catch (Exception $e) {
    echo 'Error sending email: '. $e->getMessage();
}

This will catch any exceptions that may occur while sending the email and output the error message.