How does the SMTP protocol handle email delivery and error notifications?

The SMTP protocol handles email delivery by connecting to the recipient's mail server and transferring the email message. If there are any errors during the delivery process, the SMTP server will send back error notifications to the sender's mail server. These error notifications typically include an error code and a brief description of the issue.

// Example of sending an email using SMTP in PHP
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email sent via SMTP.";
$headers = "From: sender@example.com";

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

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