What are some best practices for configuring and using email functionality in PHP applications to ensure reliable delivery and avoid common errors?

Issue: To ensure reliable delivery and avoid common errors when sending emails in PHP applications, it is important to properly configure the email settings and handle potential errors that may occur during the sending process.

// Configure the PHP mail settings for reliable email delivery
ini_set('SMTP', 'your_smtp_server');
ini_set('smtp_port', 'your_smtp_port');
ini_set('sendmail_from', 'your_email_address');

// Send the email using the PHP mail function
$to = 'recipient@example.com';
$subject = 'Subject of the email';
$message = 'This is the message body';
$headers = 'From: your_email@example.com';

if (mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed';
}