DigitalBraine
DigitalBraine

Reputation: 143

PhpMailer Delay Sending First Email Only

I don't know why, but phpmailer, when sending first email to a particular email address takes up to 5minutes-2hours to arrive in inbox sometimes it never comes until you send a second mail.

But after the first email is received in inbox, subsequent mails to that particular email comes within seconds.

Could there be anything wrong with my code or there's something I'm doing wrong somewhere?

function loadMailData(array $mailData){
        $website = new \core\Website();
        $mail = new PHPMailer();
        //Enable SMTP debugging. 
        // $mail->SMTPDebug = 3;  
        // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                               
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();            
        //Set SMTP host name                          
        $mail->Host = $this->stmpHost;
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;                          
        //Provide username and password     
        $mail->Username = $this->smtpUsername;                
        $mail->Password = $this->smtpPassword;                           
        //If SMTP requires TLS encryption then set it
        // $mail->SMTPSecure = "ssl";   
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;                         
        //Set TCP port to connect to 
        $mail->Port = 465;                                   

        $mail->From = $mailData['sentBy'];
        $mail->FromName = $website->name;
        $mail->AddReplyTo($this->replyTo, 'Support');

        $mail->smtpConnect(
            array(
                "ssl" => array(
                    "verify_peer" => false,
                    "verify_peer_name" => false,
                    "allow_self_signed" => true
                )
            )
        );

        $mail->addAddress($mailData['email']);

        $mail->isHTML(true);

        $mail->Subject = $mailData['subject'];
        $mail->Body = $mailData['message'];
        //$mail->AltBody = "This is the plain text version of the email content";

        if($mail->send()){
            //echo "Message has been sent successfully";
            return true;
        }else{
            //echo "Mailer Error: " . $mail->ErrorInfo;
            return false;
        }
        
    }

:::::::::UPDATE::::::::

I Just tried sending an email from my hosting server's webmail and it went within seconds. Unlike when I use phpMailer with the webmail's config as smtp.

Currently, my emails don't even arrive on time any more be it the first or second or 3rd. Takes upto 3hrs sometimes

It's kind if frustrating.

Upvotes: 0

Views: 705

Answers (1)

Synchro
Synchro

Reputation: 37730

Welcome to the world of SMTP! SMTP is a disconnected protocol that does not provide any time guarantees on deliveries, and it can sometimes take days to deliver messages – it was designed to cope with systems that are only connected intermittently.

In your case it's most likely to be greylisting, where a receiving server says "I'm busy, come back later" in the hope that a well-behaved, compliant SMTP server will do just that, but a dumb spambot will probably not. Having done that successfully once, it will then receive subsequent messages from the same IP and from address without incurring the delay, so you should only expect this delay on the first message.

Separately, you should not be disabling TLS certificate verification. If it's failing it's most likely due to outdated certificates on your own server, so make sure it's up to date. There's plenty about this in the PHPMailer docs.

Upvotes: 2

Related Questions