Configuring Gallery2 to send email using Gmail

October 27th, 2008 | by Daniel |

I recently revived my online photo gallery but ran into a problem with sending mail. Unlike most of my other sites, I run this on a box that I have at home, which means that it’s affected by my ISP’s retarded firewall rules. Like most consumer ISPs, Rogers blocks outgoing connections on port 25 in a misguided effort to reduce spam from computers taken over by botnets and such. The only way to send out mail from your computer at home is to do so through the SMTP relay set up by Rogers (which involves jumping through some hoops in order to access successfully) or by connecting to an SMTP server that listens on an alternate port.

I chose the latter option, using Gmail to send my mail. The only snag here is that Gmail’s SMTP server requires a secure (SSL) connection, and Gallery’s admin screen doesn’t seem to be aware of secure settings. I took a look at Gallery’s SMTP code and it uses PHP’s fsockopen() to connect to the specified host, which means that it can do SSL easily under the hood. Gallery’s code doesn’t really take this into account when parsing the host name however, so we need to do a little tweak:

Line 68 in <gallery directory>/lib/smtp/smtp.php looks like this:

list ($config['smtp.host'], $port) = array_merge(explode(':', $config['smtp.host']), array(25));

In order to get it parsing the port and scheme properly, we’ll need to replace it with the following:

$url_info = parse_url($config['smtp.host']);
$config['smtp.host'] = '';
if( isset($url_info['scheme']) ) {
   $config['smtp.host'] = $url_info['scheme'].'://';
}
$config['smtp.host'] .= $url_info['host'];
$port = ( isset($url_info['port']) ) ? $url_info['port'] : 25;

Once that change is done, you need to set up your Site Admin > General > Email options in Gallery2. Server should be ssl://smtp.gmail.com:465 (the code change we made parses that URL correctly), and username/password should be self-explanatory. Note: You must use your entire Gmail address (e.g. username@gmail.com) here, and Google Apps users must use their domain name in place of gmail.com.

Now send out a test e-mail to make sure it works, and you’re done!

  • Digg
  • Reddit
  • del.icio.us
  • Facebook
  • Technorati
  • E-mail this story to a friend!
  1. 7 Responses to “Configuring Gallery2 to send email using Gmail”

  2. By dave on Dec 15, 2008 | Reply

    Hey i cant get it to work it just gives me a blank screen.

  3. By Daniel on Dec 16, 2008 | Reply

    Hey Dave, you’re right. I made some changes to the code after my initial post and I made two typos. I’m pretty sure gallery turns off PHP’s displaying of errors, and that’s why you get the blank screen.

    I forgot a “)” after the two isset() calls in the code snippet above. I’ve corrected it already.

    Thanks!

  4. By dave on Dec 16, 2008 | Reply

    Man.. I can’t believe I didn’t see that.

    Thanks.

  5. By Daniel on Dec 17, 2008 | Reply

    It was pretty tough to spot for myself. I always fall into that trap though, forgetting the closing bracket if I call a function within an if statement. :)

  6. By mike on Dec 24, 2008 | Reply

    What about using port 587? It’s the “SMTP Submission” port and is usually less restricted than port 25. It’s also preferred over port 25 for sending mail from clients. Since you are talking SSL, some servers still use 465 as “secure SMTP” so that might work as well.

  7. By mike on Dec 24, 2008 | Reply

    Boy did I misread some of that or what? Sorry for duplicating some info. Apparently I need another cup of coffee.

  8. By Daniel on Dec 24, 2008 | Reply

    No problem Mike :)

Post a Comment