What are some common best practices for troubleshooting email sending issues in PHP scripts on different server environments?

Issue: Common email sending issues in PHP scripts on different server environments can be caused by misconfigured SMTP settings, firewall restrictions, or email server blacklisting. To troubleshoot these issues, it is recommended to double-check the SMTP settings, ensure the firewall allows outgoing connections on the SMTP port, and verify that the email server is not blacklisted. PHP Code Snippet:

// Set the SMTP server and port
ini_set('SMTP', 'smtp.example.com');
ini_set('smtp_port', 25);

// Set the sender's email address
$from = 'sender@example.com';

// Set the recipient's email address
$to = 'recipient@example.com';

// Set the subject and message
$subject = 'Test Email';
$message = 'This is a test email sent from a PHP script.';

// Send the email
if (mail($to, $subject, $message, 'From: ' . $from)) {
    echo 'Email sent successfully.';
} else {
    echo 'Failed to send email.';
}