fastest way to send mails using phpmailer smtp?

前端 未结 2 1290
别跟我提以往
别跟我提以往 2021-02-11 09:33

i am using following phpmailer function to send 1000+ mails



        
2条回答
  •  伪装坚强ぢ
    2021-02-11 10:14

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

提交回复
热议问题