How can PHPMailer be utilized to send emails in PHP code for website registration processes?

To send emails in PHP code for website registration processes, you can utilize PHPMailer, a popular email sending library for PHP. PHPMailer provides an easy way to send emails with attachments, HTML content, and more, making it ideal for sending registration confirmation emails to users.

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

require 'vendor/autoload.php'; // Include PHPMailer autoload file

// 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 = 'yourpassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

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

// Set email content
$mail->isHTML(true);
$mail->Subject = 'Registration Confirmation';
$mail->Body = 'Thank you for registering on our website.';

// Send the email
if($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Error sending email: ' . $mail->ErrorInfo;
}
?>