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!

Hey i cant get it to work it just gives me a blank screen.
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!
Man.. I can’t believe I didn’t see that.
Thanks.
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. :)
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.
Boy did I misread some of that or what? Sorry for duplicating some info. Apparently I need another cup of coffee.
No problem Mike :)
Thanks. It worked for me.
That’s great – thanks for the hint. Will smtp servers still work? Can this change be integrated into Gallery?
Thanks for your efforts
Hey Mike,
Yep, that should still work if you use a regular old SMTP server. I’m sure it could be integrated into Gallery, but it’s not an incredibly robust solution – they’d probably want to make it a bit user friendlier before adding it to the core. I suppose I could contribute the patch in any case though :). Glad you got some use out of it!
Hi Daniel:
For some reason – it’s stopped working for me. Have google changed it to a pay only feature or something like that? Does it still work for you?
Hey Mike,
Not that I know of. I regularly use Gmail’s SMTP server and haven’t run into any problems. I don’t use this hack on my gallery install anymore because I run gallery2 on a colocated server now, not on my home internet connection.
Do you get any errors or is your mail just never delivered?
I set this up on my server and it worked great! First try with a google apps account even. Please push it at the gallery folks for inclusion?!
I set this up on my server and it worked great!
tnx
For those still having problems sending email after this, you might want to check your PHP already have OpenSSL setup…
Thanks Ronald, you’re right, fsockopen needs openssl to be able to do this…
worked!!!
Thank You!
Sweet. still not included in core but patch still works flawlessly
Thanks, worked a treat.
Justin
It just works fine for me
;) thanks