How can PHP be used to dynamically generate and send HTML emails?
To dynamically generate and send HTML emails using PHP, you can use the PHP `mail()` function along with setting appropriate headers for the email content type. You can create an HTML template for the email content and use PHP variables to inject dynamic data into the email before sending it.
<?php
$to = 'recipient@example.com';
$subject = 'Subject of the Email';
$message = '<html><body>';
$message .= '<h1>Hello!</h1>';
$message .= '<p>This is a dynamically generated HTML email.</p>';
$message .= '</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
- Is it recommended to create a custom template system in PHP instead of using existing ones like Smarty? What are the potential benefits and drawbacks of doing so?
- What are the best practices for handling MySQL database connections and queries in PHP scripts to avoid errors?
- What is the best practice for displaying new forum posts for individual users in PHP?