Skip to content

Instantly share code, notes, and snippets.

@amrography
Last active March 5, 2021 12:47
Show Gist options
  • Save amrography/cb28accda291d9c03bfd20a93cece62c to your computer and use it in GitHub Desktop.
Save amrography/cb28accda291d9c03bfd20a93cece62c to your computer and use it in GitHub Desktop.
Send email using PHPMailer and Gmail
<?php
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require __DIR__ . '/PHPMailer/src/PHPMailer.php';
require __DIR__ . '/PHPMailer/src/Exception.php';
require __DIR__ . '/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$from_email = "[email protected]";
$password = 'password';
$to_email = "[email protected]";
try {
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Username = $from_email;
$mail->Password = $password;
$mail->setFrom("[email protected]", "No Reply");
$mail->addAddress($to_email, "Receiver");
$mail->isHTML(true);
$mail->Subject = "New test email";
$mail->Body = "<b>Lorem</b> ipsum dolor sit amet consectetur adipisicing elit. <i>Omnis saepe molestias</i>";
$mail->AltBody = "failed html text";
$mail->send();
$mail->smtpClose();
echo "email sent";
} catch (Exception $e) {
echo "Message could not be sent. " . $mail->ErrorInfo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment