PHPMailer

PHPMailer is a popular library for sending emails via PHP, supporting various protocols like SMTP and offering advanced functionality. Version 6.9.3 is compatible with PHP 5.5 and later, including PHP 8.2.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

// Email sender and recipient details
$From = "[email protected]";
$FromName = "YourName";
$EmailTo = "[email protected]";
$ToName = "RecipientName";
$Subject = "Your Subject Line";
$Body = "Your email body text here.";
$AltBody = "Your alternative plain text here.";

// Create a new PHPMailer instance
$mail = new PHPMailer; 
$mail->SMTPDebug = false; // Disable verbose debug output
$mail->isSMTP(); // Use SMTP
$mail->Host = 'your.mailserver.com'; // SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'your-password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption
$mail->Port = 587; // TCP port

// Email headers and content
$mail->From = $From;
$mail->FromName = $FromName;
$mail->addAddress($EmailTo, $ToName); // Add recipient
$mail->addReplyTo($From, $FromName);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->AltBody = $AltBody;

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

?>

Leave a Reply

Your email address will not be published. Required fields are marked *