Search code examples
phpemailgmailswiftmailer

Email not being sent with Swift Mailer using Gmail SMTP


I am trying to send emails from Swift Mailer using Gmail SMTP. It sends the emails conveniently for some time but then stops sending them altogether especially when I resume working after a day or so. It displays the following error:

Connection could not be established with host smtp.gmail.com [ #0]

Below is my sample code that I am using to send the email:

<?php
    require_once 'lib/swift_required.php';
    try
    {
        echo '<pre>';
        //Generating the Email Content
        $message = Swift_Message::newInstance()
                    ->setFrom(array('[email protected]' => 'No Reply'))
                    ->setTo(array('[email protected]' => 'Recipient'))
                    ->setSubject('Test Email')
                    ->setBody("This is a Test Email to check SwiftMailer.");

        // Create the Mail Transport Configuration
        $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
                    ->setUsername('[email protected]')
                    ->setPassword('appPassword');

        //local domain sending
        $transport->setLocalDomain('[127.0.0.1]');

        $mailer = Swift_Mailer::newInstance($transport);

        //Send the email
        $sentFlag = $mailer->send($message);
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }

?>

I am using App Password and I have enabled two-step verification in my Google Account Settings. I have been looking for a solution to this problem for a while now and i have already gone through many other related posts but didn't find a solution. Someone please suggest a permanent solution.

Thanks in advance.


Solution

  • How to use Swift Mailer Using Gmail SMTP

    Try this code like this:

    <?php
        require_once 'lib/swift_required.php';
        try
        {
            echo '<pre>';
            //Generating the Email Content
            $message = Swift_Message::newInstance()
                        ->setFrom(array('[email protected]' => 'No Reply'))
                        ->setTo(array('[email protected]' => 'Recipient'))
                        ->setSubject('Test Email')
                        ->setBody("This is a Test Email to check SwiftMailer.");
    
            // Create the Mail Transport Configuration
            $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
                        ->setUsername('[email protected]')
                        ->setPassword('appPassword')
                        ->setStreamOptions(array(
                         'ssl' => array(
                         'allow_self_signed' => true, 
                         'verify_peer' => false)));
    
            //local domain sending
            $transport->setLocalDomain('[127.0.0.1]');
    
            $mailer = Swift_Mailer::newInstance($transport);
    
            //Send the email
            $sentFlag = $mailer->send($message);
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
        }
    
    ?>
    

    Hope it helps