0

Below is the sample code where first time calling is working but second time similar thing failing to send mail: You can see both of the functions are symmetric.

<?php
    include_once "Swift-5.1.0/lib/swift_required.php";
    $transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
    $transport->setUsername('Username');//smtp Access
    $transport->setPassword('Password');//smtp Access
    $swift = Swift_Mailer::newInstance($transport);

    function call1(){
        //...subject/from/to/text/html
        global $swift;
        $message=NULL;
        $message = new Swift_Message($subject);
        $headers = $message->getHeaders();
        $message->setFrom($from);
        $message->setTo($to);

        $htmlPart = new Swift_MimePart($html, 'text/html'); 
        $message->attach($htmlPart); 

        $textPart = new Swift_MimePart($text, 'text/plain'); 
        $message->attach($textPart); 
        if ($recipients = $swift->send($message, $failures))
        {
            $mailsent = true;
            return true;
        } else {
            $mailsent = false;
            return false;
        }
    }
    function call2(){
        //...subject/from/to/text/html
        global $swift;
        $message=NULL;
        $message = new Swift_Message($subject);
        $headers = $message->getHeaders();
        $message->setFrom($from);
        $message->setTo($to);

        $htmlPart = new Swift_MimePart($html, 'text/html'); 
        $message->attach($htmlPart); 

        $textPart = new Swift_MimePart($text, 'text/plain'); 
        $message->attach($textPart); 
        if ($recipients = $swift->send($message, $failures))
        {
            $mailsent = true;
            return true;
        } else {
            $mailsent = false;
            return false;
        }
    }

    call1();//Working
    call2();//Not Working
    call1();//Not Working
    call2();//Not Working
?>
itsazzad
  • 6,868
  • 7
  • 69
  • 89

1 Answers1

0

Make sure your mail app isn't set to check for new email too frequently. If your mail app checks for new messages more than once every 10 minutes, the app’s access to your account could be blocked.

Check here: https://support.google.com/mail/answer/14257