i am using following phpmailer function to send 1000+ mails
Untested, but this should work.
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);
}
You should start by reading the docs provided with PHPMailer where you will find this example.
Of particular note in there, make sure you use SMTPKeepAlive
- you may find benefit in sorting your list by domain to maximise connection re-use.
As zerkms said, you should submit to a local mail server for best performance, though surprisingly using mail
or sendmail
options in PHPMailer is not always faster than SMTP to localhost, largely because postfix' sendmail binary opens a synchronous SMTP connection to localhost anyway - postfix' docs recommend SMTP to localhost for best performance for this reason.
If you are sending to localhost, don't use auth or encryption as the overhead doesn't gain you anything, but if you are using a remote server, use tls on port 587 in preference to the obsolete ssl on port 465.
Generally sending directly to end users is to be avoided - the SMTP client in PHPMailer is somewhat dumb - it does not handle queuing at all, so any domains with greylisting or delivery deferrals for traffic control will fail to be delivered. the best approach is to use SMTP to a nearby MTA and leave the queue handling to that. You can get bounces back from that as well so you can remove bad addresses from your list.