Friday, 29 December 2017

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);
}


EmoticonEmoticon

Ads

Make Fun WIth Her Free

Recent Posts Widget