//Import PHPMailer Library require_once "PHPMailer/src/PHPMailer.php"; require_once "PHPMailer/src/SMTP.php"; //Create a new PHPMailer instance $mail = new PHPMailer\PHPMailer\PHPMailer(); //Set SMTP settings $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.gmail.com'; // Specify SMTP server $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'yourUsername@gmail.com'; // SMTP username $mail->Password = 'yourPassword'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption //Set email details $mail->setFrom('from@example.com', 'Your Name'); $mail->addAddress('to@example.com', 'Recipient Name'); $mail->Subject = 'Test Email'; $mail->Body = 'This is a test email.'; //Send email if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }
//Import PHPMailer Library require_once "PHPMailer/src/PHPMailer.php"; require_once "PHPMailer/src/SMTP.php"; //Create a new PHPMailer instance $mail = new PHPMailer\PHPMailer\PHPMailer(); //Set SMTP settings $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.gmail.com'; // Specify SMTP server $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'yourUsername@gmail.com'; // SMTP username $mail->Password = 'yourPassword'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption //Set email details $mail->setFrom('from@example.com', 'Your Name'); $mail->addAddress('to@example.com', 'Recipient Name'); $mail->Subject = 'Test Email'; $mail->isHTML(true); // Set email format to HTML $mail->Body = 'From the code examples, it is clear that PHPMailer is the package library used. We can also see that PHPMailer uses namespaces and we need to import the required classes from the library before using them. Overall, PHPMailer is a powerful library that makes it easy to send emails from PHP scripts.This is a test email
Hello world!
'; //Send email if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }