Skip to content

Instantly share code, notes, and snippets.

@Fed0t
Last active November 21, 2018 11:55
Show Gist options
  • Save Fed0t/0145ce5c329fe39c70a2e5572f51e9ab to your computer and use it in GitHub Desktop.
Save Fed0t/0145ce5c329fe39c70a2e5572f51e9ab to your computer and use it in GitHub Desktop.
SMTP send emails with PHPMailer over TLS
<?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 = 'no-reply@mail.host.test';
$this->mailer->Password = 'testmailpass';
$this->mailer->SMTPSecure = 'tls';
$this->mailer->Port = 26;
$this->mailer->CharSet = "UTF-8";
$this->mailer->isHTML(true);
$this->mailer->setFrom('no-reply@mail.host.test', 'Mail Online');
$this->mailer->addReplyTo('no-reply@mail.host.test', '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