Fastest way to send mails using phpmailer smtp?



Basically, it reuses the original object (thus reducing memory allocations).
require_once 'PHPMailer/PHPMailerAutoload.php';

class BatchMailer {

var $mail;

function __construct () {
$this
->mail = new PHPMailer;
$this
->mail->isSMTP();
$this
->mail->Host = 'smtp.example.com;smtp.example.com';
$this
->mail->SMTPAuth = true;
$this
->mail->Username = 'newsletter@example.com';
$this
->mail->Password = 'password';
$this
->mail->SMTPSecure = 'ssl';
$this
->mail->SMTPKeepAlive = true;
$this
->mail->Port = 465;
$this
->mail->From = 'newsletter@example.com';
$this
->mail->FromName = 'xyz';
$this
->mail->WordWrap = 50;
$this
->mail->isHTML(true);
$this
->mail->AltBody = 'Please use an HTML-enabled email client to view this message.';
}

function setSubject ($subject) {
$this
->mail->Subject = $subject;
}

function setBody ($body) {
$this
->mail->Body = stripslashes($body);
}

function sendTo ($to) {
$this
->mail->clearAddresses();
$this
->mail->addAddress($to);

if (!$this->mail->send()) {
// echo 'Mailer Error: ' . $this->mail->ErrorInfo;
return false;
} else {
return true;
}
}

}

$batch
= new BatchMailer;

$batch
->setSubject('sample subject');
$batch
->setBody('sample body');

foreach ($emails as $email) {
$batch
->sendTo($email);
}

Comments

Popular posts from this blog

App Store Connect ITMS-90078: Missing Push Notification Entitlement - Your app appears to register with the Apple Push Notification service, but the app signature's entitlements do not include the "aps-environment"

RGPV CSE 8th Semester Soft computing Notes and Lectures

Facts about bb ki vines bhuvan bam