Problem using Swift with Gmail

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
joshmaker
Forum Commoner
Posts: 25
Joined: Mon May 15, 2006 2:53 pm
Location: Arlington VA

Problem using Swift with Gmail

Post by joshmaker »

I am a web developer, and since I have to build a lot of contact forms, I've been using Swift with various gmail accounts. I assumed that since Gmail is free, stable, and automatically keeps a record of the emails that it sends out that it would be easy to create accounts for use with Swift for each of online contact forms. Unfortunately, I receive periodic errors when I try to connect to the Gmail server:
Fatal error: Uncaught exception 'Swift_ConnectionException' with message 'Authentication failed using username '********'' and password '********'' in /home/foxcraft/public_html/includes/Swift/lib/Swift/Connection/SMTP.php:414 Stack trace: #0 /home/foxcraft/public_html/includes/Swift/lib/Swift/Connection/SMTP.php(331): Swift_Connection_SMTP->runAuthenticators(''********''', ''********''', Object(Swift)) #1 /home/foxcraft/public_html/includes/Swift/lib/Swift.php(264): Swift_Connection_SMTP->postConnect(Object(Swift)) #2 /home/foxcraft/public_html/includes/Swift/lib/Swift.php(220): Swift->handshake(Object(Swift_Events_ResponseEvent)) #3 /home/foxcraft/public_html/includes/Swift/lib/Swift.php(101): Swift->connect() #4 /home/foxcraft/public_html/includes/classes/TmiGmailer.php(19): Swift->__construct(Object(Swift_Connection_SMTP)) #5 /home/foxcraft/public_html/pages/public/contact.php(100): TmiGmailer->TmiGmailer(''********''', ''********''') #6 /home/foxcraft/public_html/includes/classes/PageDisplayer.php(200): requi in /home/foxcraft/public_html/includes/Swift/lib/Swift/Connection/SMTP.php on line 414
(I've starred out the address & password for privacy reasons. When this happens, I can go to http://www.gmail.com and login, and I will be presented with a captcha to solve before I can do so. Once I have solved the captcha and login the Swift email will work as expected.

I found another thread about someone who had a similar sounding issue: viewtopic.php?f=52&t=77887&start=0&st=0&sk=t&sd=a

This thread suggests that the user's problem may be volume: apparently Gmail will temporarily kick you out after sending too many emails in a short time. However, I use gmail accounts for a number of different Swift / online contact forms and this problem seems altogether uncorrelated with volume, or with anything else that I can think of. The most recent time that this happened to me was with an account that had work successfully sending out low volumes of emails (10 - 20 total) for over a month. I've also had accounts that have been active longer with no problems.

It sounds like I am tripping some sort of "spam" setting on gmail, but since this issue is so random, I can't diagnose it. Is there anything I can do / setting that I can change to make Gmail a more reliable email host for me?
joshmaker
Forum Commoner
Posts: 25
Joined: Mon May 15, 2006 2:53 pm
Location: Arlington VA

Re: Problem using Swift with Gmail

Post by joshmaker »

I'm not sure this is going to be helpful (since it seems like the issue may be with Gmail rather than with Swift) here is the code that I am using:

Code: Select all

<?php
require_once("./includes/Swift/lib/EasySwift.php");
require_once("./includes/Swift/lib/Swift.php");
require_once("./includes/Swift/lib/Swift/Message.php");
require_once("./includes/Swift/lib/Swift/Message/Attachment.php");
require_once("./includes/Swift/lib/Swift/Connection/SMTP.php");
require_once("./includes/Swift/lib/Swift/Address.php");
require_once("./includes/Swift/lib/Swift/RecipientList.php");
require_once("./includes/Swift/lib/Swift/BatchMailer.php");
class TmiGmailer
{
    private $swift;
 
    function TmiGmailer($user, $pass)
    {
        $connection = new Swift_Connection_SMTP("smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
        $connection->setUsername($user);
        $connection->setPassword($pass);
        $this->swift = new Swift($connection);
    }
    
    function Mail($to, $subject, $body, $attachments=null, $replyto=null)
    {
        $mailer = new Swift_BatchMailer($this->swift);
        $reciepients = new Swift_RecipientList();
        if(is_string($to))
            $reciepients->AddTo($to);
        elseif(is_array($to))
        {
            foreach($to as $value)
            {
                if(is_array($value))
                {
                    foreach($value as $to_details)
                    {
                        switch($to_details['type'])
                        {
                            case "":
                            case "to":
                                $reciepients->AddTo($to_details['address'], $to_details['name']);
                                break;
                            case "cc":
                                $reciepients->AddCc($to_details['address'], $to_details['name']);
                                break;
                            case "bcc":
                                $reciepients->addBcc($to_details['address'], $to_details['name']);
                                break;
                        }
                    }
                }
                else 
                {
                    $reciepients->AddTo($value);
                }
            }
        }
        $message = new Swift_Message($subject, $body);
        $files = array();
        if(is_string($attachments))
        {
            $attachment = new Swift_Message_Attachment(file_get_contents($attachments), basename($attachments));
            $message->attach($attachment);
        }
        elseif(is_array($attachments))
        {
            foreach($attachments as $attachment)
            {
                if(file_exists($attachment))
                {
                    $att = new Swift_Message_Attachment(file_get_contents($attachment), basename($attachment));
                    $message->attach($att);
                }
                else 
                {
                    echo 'Error: Attaching file.';
                }
            }
        }
        if($replyto)
            $message->setReplyTo($replyto);
        $sender = new Swift_Address("default@gmail.com");
        if($mailer->send($message, $reciepients, $sender))
        {
            echo 'Your Email has been sent.';
        }
        else 
        {
            echo 'Sorry, I was not able to send your email.';
        }
    }
}
?>
As you can see, since I intended to use Gmail for all of my quick email forms, I wrote a class to make sending messages easier. This is how the class would be implemented in a website:

Code: Select all

    
$mailer = new TmiGmailer("random.gmail.account", "password");
$mailer->Mail( 
    array("my@clients-email-address.com", "my@personal-email-address.com"), // address to send the email too
    "[Website Form] " . $_POST['subject'],  // subject line
    $message,  // body of the message
    null, 
    $_POST['email_address'] // reply address
);
 
colnector
Forum Newbie
Posts: 9
Joined: Thu Nov 06, 2008 1:30 am

Re: Problem using Swift with Gmail

Post by colnector »

I've been sending through a GMail account (actually not at Gmail.com but under my domain colnect.com) for almost a month now and everything seems to be running smooth. I hope that won't stop.

Anyway, I've noticed some differences in the way you connect to GMail (encoding is one and I don't know what port number you use).
This is my code:

Code: Select all

 public static function send($mailFrom, $mailTo, $mailSubject, $mailBody) {
    if (empty($mailFrom))   $mailFrom = sfConfig::get('app_email_mailer');
    if (empty($mailTo)) $mailTo = sfConfig::get('app_email_mailer');
        try {
          // Create the mailer and message objects
            $connection = new Swift_Connection_SMTP(sfConfig::get('app_email_host_addr'), 
                465, Swift_Connection_SMTP::ENC_SSL);
            $connection->setUsername(sfConfig::get('app_email_host_user'));
            $connection->setPassword(sfConfig::get('app_email_host_pwd'));
     
            $mailer = new Swift($connection);
          $message = new Swift_Message($mailSubject, $mailBody, 'text/html');
          $message->setReplyTo($mailFrom);
         
          // Send
          $mailer->send($message, $mailTo, $mailFrom);
          $mailer->disconnect();
        }   catch (Exception $e)    {
            throw $e;
          if (isset($mailer)) $mailer->disconnect();         
          // handle errors here
        }   
  }
Post Reply