Last active
March 5, 2021 12:47
-
-
Save amrography/cb28accda291d9c03bfd20a93cece62c to your computer and use it in GitHub Desktop.
Send email using PHPMailer and Gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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