How can PHP be used to send personalized emails with styled templates?

To send personalized emails with styled templates using PHP, you can utilize the PHPMailer library. This library allows you to send emails with HTML content, which can include styled templates. You can dynamically insert personalized content into the email template by using variables and concatenation within the PHP code.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer();

// Set up the SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Set the email content
$mail->isHTML(true);
$mail->Subject = 'Your Subject Here';
$mail->Body = '<html><body><h1>Hello, {name}!</h1><p>This is a styled email template.</p></body></html>';

// Set the recipient and sender
$mail->setFrom('your@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Replace {name} with the personalized content
$name = 'John Doe';
$mail->Body = str_replace('{name}', $name, $mail->Body);

// Send the email
if(!$mail->send()) {
    echo 'Email could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Email has been sent';
}
?>