0

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.

  • Might be a spam filter somewhere holding and analysing it. Are you letting the end user set the From field of the mail, or anything silly like that? – ADyson Mar 12 '22 at 05:38
  • everything is done by me. the end user just provides an email address. that's all. – DigitalBraine Mar 12 '22 at 05:43
  • You mean they provide the FROM address or the TO address? – ADyson Mar 12 '22 at 05:44
  • End user provides only TO ADDRESS – DigitalBraine Mar 12 '22 at 05:52
  • OK. And you made the FROM address be something from the same domain as the smtp server, I hope? – ADyson Mar 12 '22 at 06:03
  • Exactly... Yes it's noreply@domain.com... same smtp server config – DigitalBraine Mar 12 '22 at 06:23
  • Maybe the SMTP server is not well configured or something. It can be a complicated business. Have a look at https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail at the main answer there. Not everything in it is relevant since you're already using phpmailer, but the mail server setup stuff may be useful – ADyson Mar 12 '22 at 06:47
  • since I'm receiving the mail, I don't think is a misconfiguration. cos misconfiguration means error and error means not receiving email. in my case, I receive the email in inbox but it takes alot of time before it arrives! – DigitalBraine Mar 12 '22 at 07:09
  • It can still be a misconfiguration which makes the email look suspicious to the receiving server and causes it to stop and analyse it. That's my suspicion. Misconfiguration can cause problems without causing outright failure – ADyson Mar 12 '22 at 07:18
  • 1
    That may be [greylisting](https://en.wikipedia.org/wiki/Greylisting_(email)). My mail provider implements it. It's annoying, but it really works to reduce spam volume. – Álvaro González Mar 12 '22 at 10:19

1 Answers1

2

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104