Send Email Using The GMail SMTP Server From A PHP Page

To send an email using the Gmail SMTP server from a PHP page, you can use the PHPMailer library. Here’s an example code:

<?php
require 'PHPMailer/PHPMailerAutoload.php';

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

// Enable SMTP debugging
$mail->SMTPDebug = 2;

// Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

// Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

// Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

// Set the SMTP authentication mechanism
$mail->SMTPAuth = true;

// Set the username and password
$mail->Username = "[email protected]";
$mail->Password = "your-password";

// Set the sender and recipient address
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');

// Set the email subject and body
$mail->Subject = 'Test Email from PHPMailer';
$mail->Body = 'This is a test email sent from PHP using the Gmail SMTP server.';

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

Make sure to replace "[email protected]" and "your-password" with your own Gmail credentials. Also, ensure that you have downloaded the PHPMailer library and included the correct path to the PHPMailerAutoload.php file.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!