How can one send HTML-formatted emails using the mail() function in PHP?
To send HTML-formatted emails using the mail() function in PHP, you need to set the appropriate headers in the mail function to indicate that the content is HTML. This can be done by adding the "Content-Type" header with a value of "text/html". Additionally, you need to ensure that your email content is properly formatted with HTML tags.
$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "<html><body><h1>Hello!</h1><p>This is a test email in HTML format.</p></body></html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: sender@example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Keywords
Related Questions
- How can PHP developers effectively troubleshoot and debug FTP connection issues, especially when transferring data between different servers?
- What are the potential pitfalls of using str_replace for string manipulation in PHP?
- What are the key considerations for adjusting the width and height of a PHP forum to ensure it aligns with the website layout?