How does MIME mail play a role in sending HTML-formatted emails through PHP?
When sending HTML-formatted emails through PHP, MIME mail plays a crucial role in properly formatting the email content. By using MIME (Multipurpose Internet Mail Extensions), PHP can encode the HTML content and set the appropriate headers to ensure the email is correctly interpreted by the recipient's email client.
<?php
$to = 'recipient@example.com';
$subject = 'HTML Email Test';
$message = '<html><body>';
$message .= '<h1>Hello, this is a test email!</h1>';
$message .= '<p>This is an example of sending an HTML-formatted email through PHP.</p>';
$message .= '</body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: sender@example.com' . "\r\n";
// Send the email
mail($to, $subject, $message, $headers);
?>
Related Questions
- What is the common mistake in the provided PHP code that leads to the else statement always being executed?
- What are some best practices for handling user input in PHP scripts to ensure security?
- What are the best practices for approaching PHP programming tasks, especially for beginners with limited knowledge?