How to check if mail to os working in php

To check if mail sending is working in PHP, you can use the following methods:

  1. Using mail() function and checking the return value:
    
    <?php
    $to = '[email protected]';
    $subject = 'Test email';
    $message = 'This is a test email';

if (mail($to, $subject, $message)) { echo "Mail sent successfully!"; } else { echo "Error sending mail!"; } ?>

In this example, the `mail()` function returns `true` if the mail is sent successfully, and `false` otherwise.

2. **Using a mail logging library**:
You can use a library like `SwiftMailer` or `PHPMailer` to send mail and log the results. These libraries provide detailed logs of the mail sending process, including any errors that may occur.

For example, with `SwiftMailer`:
```php
<?php
require_once 'swiftmailer/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 587)
    ->setUsername('your_username')
    ->setPassword('your_password');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test email')
    ->setFrom(array('[email protected]' => 'Your Name'))
    ->setTo(array('[email protected]'))
    ->setBody('This is a test email');

if ($mailer->send($message)) {
    echo "Mail sent successfully!";
} else {
    echo "Error sending mail!";
}
?>

In this example, the Swift_Mailer class provides a send() method that returns true if the mail is sent successfully, and false otherwise.

  1. Using a mail testing service: You can use a mail testing service like Mailinator or 10 Minute Mail to test your mail sending code. These services provide a temporary email address that you can use to receive test emails.

For example, with Mailinator:

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

if (mail($to, $subject, $message)) {
    echo "Mail sent successfully!";
} else {
    echo "Error sending mail!";
}
?>

In this example, you can use the [email protected] email address to receive test emails.

  1. Checking the mail server logs: You can also check the mail server logs to see if the mail is being sent successfully. The logs will show the status of the mail sending process, including any errors that may occur.

Remember to replace the placeholders (your_username, your_password, smtp.example.com, etc.) with your actual mail server credentials and settings.