Last active
November 21, 2018 11:55
-
-
Save Fed0t/0145ce5c329fe39c70a2e5572f51e9ab to your computer and use it in GitHub Desktop.
SMTP send emails with PHPMailer over TLS
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\PHPMailer; | |
use PHPMailer\PHPMailer\Exception; | |
class MailController { | |
private $mailer; | |
public function __construct($body,$subject) | |
{ | |
$this->mailer = new PHPMailer(true); | |
// $this->mailer->SMTPDebug = 2; | |
$this->mailer->isSMTP(); | |
$this->mailer->SMTPKeepAlive = true; | |
$this->mailer->Host = 'mail.host.test'; | |
$this->mailer->SMTPAuth = true; | |
$this->mailer->Username = '[email protected]'; | |
$this->mailer->Password = 'testmailpass'; | |
$this->mailer->SMTPSecure = 'tls'; | |
$this->mailer->Port = 26; | |
$this->mailer->CharSet = "UTF-8"; | |
$this->mailer->isHTML(true); | |
$this->mailer->setFrom('[email protected]', 'Mail Online'); | |
$this->mailer->addReplyTo('[email protected]', 'Mail Online'); | |
$this->mailer->Subject = $subject; | |
$this->mailer->Body = $body; | |
return $this->mailer; | |
} | |
public function sendEmail($email) | |
{ | |
$this->mailer->addAddress($email); | |
// Add attachments | |
// $this->mailer->addAttachment('/var/tmp/file.tar.gz'); | |
if ($this->mailer->send()) { | |
echo 'Email Sended Successfully to '.$email.PHP_EOL; | |
} else { | |
echo 'Failed to Send Email'.$email.PHP_EOL; | |
} | |
$this->mailer->clearAddresses(); | |
} | |
public function closeConnection(){ | |
$this->mailer->SmtpClose(); | |
} | |
} | |
?> | |
//For optimized SMTPKeepAlive need to order the emails by host! | |
$mailController = new MailController($content, $subject); | |
$mailController->sendEmail($email); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment