Form Tools

Full Version: Swift Mailer Module & Gmail SMTP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
This thread describes a way to use FT2 and the Swift Mailer module with SMTP to a Gmail account. It described changes to files in the Swift Mailer module v1.0.0-beta-20090409. It enables FT2 to meet the following Gmail requirements:

1) use a SSL (not TLS) encrypted connection to Gmail SMTP
2) authenticate to your Gmail account
3) use the Swift Mailer batchSend function which defaults to only show the individual's To: address on each email and suppress the others in the list (ignores the CC: and BCC: fields)
4) use the Swift Mailer anti-flood function to limit outbound emails to 100 or less per connection

First go into the Modules section and select Swift Mailer. Update the following values:

Enabled - Yes
SMTP Server - smtp.gmail.com
Port - 465
Requires authentication - Yes
Username - example@gmail.com
Password - your Gmail password
Authentication procedure - LOGIN

Next we need to makes 4 changes to the following file.

formtools/modules/swift_mailer/library.php - change 1 of 4

This change requires (adds) the anti-flood class included with the Swift Mailer modules for later use in the script.

change this:
Code:
// include the main files
  $current_folder = dirname(__FILE__);
  require_once("$current_folder/$php_version_folder/Swift.php");
  require_once("$current_folder/$php_version_folder/Swift/Connection/SMTP.php");

  $settings = ft_get_module_settings("", "swift_mailer");
to this:
Code:
// include the main files
  $current_folder = dirname(__FILE__);
  require_once("$current_folder/$php_version_folder/Swift.php");
  require_once("$current_folder/$php_version_folder/Swift/Connection/SMTP.php");
// gmail anti-flood
require_once("$current_folder/$php_version_folder/Swift/Plugin/AntiFlood.php");

  $settings = ft_get_module_settings("", "swift_mailer");

formtools/modules/swift_mailer/library.php - change 2 of 4

This change creates a SSL encrypted connection to Gmail. Notice I commented out the previous line. Gmail offers port 465 for SSL and 587 for TLS encrypted connections. I read that Swift Mailer supports SSL, not TLS, but that information may be dated now.

change this:
Code:
if (empty($port))
    $smtp =& new Swift_Connection_SMTP($smtp_server);
  else
    $smtp =& new Swift_Connection_SMTP($smtp_server, $port);
to this:
Code:
if (empty($port))
    $smtp =& new Swift_Connection_SMTP($smtp_server);
  else
//    $smtp =& new Swift_Connection_SMTP($smtp_server, $port);
// gmail secure connection, ENC_SSL for 465 or ENC_TLS for 587
$smtp =& new Swift_Connection_SMTP($smtp_server, $port, Swift_Connection_SMTP::ENC_SSL);

formtools/modules/swift_mailer/library.php - change 3 of 4

This change brings the anti-flood code into play so you don't exceed Gmail's 100 emails per connection limit. This setting sends out 90 emails per batch then waits 2 seconds before opening up the next connection if needed.

change this:
Code:
if ($settings["requires_authentication"] == "yes")
  {
    $smtp->setUsername($settings["username"]);
    $smtp->setPassword($settings["password"]);
  }

  $swift =& new Swift($smtp);
to this:
Code:
if ($settings["requires_authentication"] == "yes")
  {
    $smtp->setUsername($settings["username"]);
    $smtp->setPassword($settings["password"]);
  }

  $swift =& new Swift($smtp);
//gmail anti-flood
$swift->attachPlugin(new Swift_Plugin_AntiFlood(90, 2), "anti-flood");

formtools/modules/swift_mailer/library.php - change 4 of 4

This change uses the batchSend() instead of the send() function. Notice I commented out the previous line. This will cause individual emails to be sent with just the recipient's email in the To: field. It will not send the entire email list to each recipient. The CC: and BCC: fields are ignored with batchSend().

change this:
Code:
$swift->send($email, $recipients, $from);

  return array($success, $message);
}
to this:
Code:
//  $swift->send($email, $recipients, $from);
// gmail batchSend
$swift->batchSend($email, $recipients, $from);

  return array($success, $message);
}

Finally we need to make 1 change to the following file.

formtools/modules/swift_mailer/php5/ft_library.php - change 1 of 1

This change is not required unless you want to use the test functions. Notice I commented out the previous line again.

change this:
Code:
if (empty($port))
      $smtp =& new Swift_Connection_SMTP($smtp_server);
    else
      $smtp =& new Swift_Connection_SMTP($smtp_server, $port);
to this:
Code:
if (empty($port))
      $smtp =& new Swift_Connection_SMTP($smtp_server);
    else
//      $smtp =& new Swift_Connection_SMTP($smtp_server, $port);
// gmail added (, Swift_Connection_SMTP::ENC_SSL) port 465 for SSL or 587 for TLS
$smtp =& new Swift_Connection_SMTP($smtp_server, $port, Swift_Connection_SMTP::ENC_SSL);

Hopefully this thread will help others looking to use Gmail as their SMTP server. It will work well for an email list with only 1 address to very long lists. Keep in mind that if you try to use this technique to send spam that Gmail will identify the activity by the higher rate of returns and rejects and lock your account for 24-48 hours. Abuse this at your own risk! YMMV!

Chuck
Terrific post, Chuck.

It's rather tempting to add your code to the module, but I am a little worried about people using it for spam... though I suppose Google are watching for it.
Ben.

My swift mailer was working fine with gmail till day before yesterday. But from yesterday onwards, the form submission goes to an error page that says.

Fatal error: Uncaught exception 'Swift_ConnectionException' with message 'The SMTP connection failed to start [tls://smtp.gmail.com:465]: fsockopen returned Error Number 110 and Error String 'Connection timed out'' in /home/kerala36/public_html/formtools/modules/swift_mailer/php5/Swift/Connection/SMTP.php:309 Stack trace: #0 /home/kerala36/public_html/formtools/modules/swift_mailer/php5/Swift.php(216): Swift_Connection_SMTP->start() #1 /home/kerala36/public_html/formtools/modules/swift_mailer/php5/Swift.php(101): Swift->connect() #2 /home/kerala36/public_html/formtools/modules/swift_mailer/library.php(196): Swift->__construct(Object(Swift_Connection_SMTP)) #3 /home/kerala36/public_html/formtools/global/code/emails.php(1102): swift_send_email(Array) #4 /home/kerala36/public_html/formtools/global/code/emails.php(1070): ft_process_email_template('2', 2, '4') #5 /home/kerala36/public_html/formtools/process.php(317): ft_send_emails('on_submission', '2', 2) #6 /home/kerala36/public_html/formtools/process.php(49): ft_process_for in /home/kerala36/public_html/formtools/modules/swift_mailer/php5/Swift/Connection/SMTP.php on line 309

I haven't made any changes with the configuration or anything. please guide.
when testing the SWIFT MAILER , i am getting the following error.

There was a problem communicating with SMTP: The SMTP connection failed to start [tls://smtp.gmail.com:465]: fsockopen returned Error Number 110 and Error String 'Connection timed out'
Hi, How you solve it , i got same error regards.


(Sep 11th, 2009, 12:07 AM)sujith Wrote: [ -> ]when testing the SWIFT MAILER , i am getting the following error.

There was a problem communicating with SMTP: The SMTP connection failed to start [tls://smtp.gmail.com:465]: fsockopen returned Error Number 110 and Error String 'Connection timed out'

Hello,

I'm having exactly the same error.

Code:
There was a problem communicating with SMTP: The SMTP connection failed to start [ssl://smtp.gmail.com:465]: fsockopen returned Error Number 110 and Error String 'Connection timed out'

Also, in module I'm entering the correct values for my SMTP server, and the same...

Code:
There was a problem communicating with SMTP: The SMTP connection failed to start [tls://mail.mydomain.com:587]: fsockopen returned Error Number 0 and Error String ''

I know the values are correct, because I'm using them also in Livezilla.

Is anybody getting the same errors?
I'm getting the same error "110" during the test. I'm at a total loss. Anyone?
Just an update, I'm getting this error message when using any SMTP configuration NOT hosted on the same domain as my FT installation. Not just Gmail. I've submitted a ticket to my web host to see if the problem is on that end. Will update when I find out.
Final update for everyone. I heard back from the web host, who said:

Quote:The secure port(465) in the firewall was not allowed for outgoing connections.

They enabled it and it works flawlessly now.

YAY!
Very good to hear!

btw, I'm going to sticky this post - I see it's at 10,000+ page views (!?); clearly it's of interest to people.

- Ben

Nice!

But opening the secure port(465) port, only makes now the GMail smtp to work.

Can I kindly ask you, Ben, that you provide us with a list of the ports Swift Module uses to connect on?

I really want to use my own SMTP server.

Thank you!!
I'm having this same issue/error. I've tried changing code where applicable, and now have reverted back to the stock code. My Host says that port 465 is open for ssl connections outbound to any SSL enabled SMTP connection. I still get the same error. It doesn't matter if I use a local SMTP on the host or anything outside, I get this same error. Anyone else got any ideas of where to look or other possible solutions? It doesn't work in either the test mode inside swift mailer, or with forms. PHP mail will work but only with addresses within the domain, and I need to be able to send attachments. I also tried the gmail route - established an account and made all changes accordingly, but no luck there either. DOH!! I dunno where to go from here....

Peace,

Bill
Pages: 1 2 3