Use GMail’s SMTP gateway using the command line from !Ubuntu without lots of config #tips

I’m writing a few little scripts at the moment, and one of them needed to be able to send an e-mail. I’d not got around to sorting out what my SMTP gateway was from my ISP – but I do tend to use GMail’s SMTP gateway for non-essential stuff.

I thought I could easily setup sendmail, but no, that’s SCARY stuff, and then I thought of Postfix, but that needs an awful lot of configuration for an TLS based SMTP connection, so I did a bit of digging.

Thanks to this post over at the Ubuntu Forums, I worked out how to get a local port 10025 to run, but PHP kept complaining, so I next looked for a “sendmail replacement”, in comes nullmailer.

So, thankfully this is all rather easy.

  • sudo apt-get install openssl xinetd nullmailer
  • sudo tee /usr/bin/gmail-smtp <<EOF >/dev/null#!/bin/sh# Thanks to http://ubuntuforums.org/showthread.php?t=918335 for this install guide/usr/bin/openssl s_client -connect smtp.gmail.com:465 -quiet 2>/dev/nullEOFsudo chmod +x /usr/bin/gmail-smtp
  • sudo tee /etc/xinetd.d/gmail-smtp <<EOF >/dev/null# default: on# description: Gmail SMTP wrapper for clients without SSL support# Thanks to http://ubuntuforums.org/showthread.php?t=918335 for this install guideservice gmail-smtp{    disable         = no    bind            = localhost    port            = 10025    socket_type     = stream    protocol        = tcp    wait            = no    user            = root    server          = /usr/bin/gmail-smtp    type            = unlisted}EOFsudo /etc/init.d/xinetd reload
  • sudo tee /etc/nullmailer/remotes <<EOF >/dev/null127.0.0.1 smtp --port=10025 --user=your@user.tld --pass=Y0urC0mp3xGM@ilP@ssw0rdEOFsudo /etc/init.d/nullmailer reload

Setting all this lot up was pretty easy with these guides. There’s no reason why it wouldn’t work on any other version of Linux (provided you can install all these packages).

Good luck with your project!

12 thoughts to “Use GMail’s SMTP gateway using the command line from !Ubuntu without lots of config #tips”

  1. argghhhh didn’t work for me…i don’t even know where to look for some verbose or debug info…I have at least a question :before finding your tutorial, i tried to install ssmtp, which worked before i realized the code wasn’t updated anymore.with ssmtp i configured it on port 587 and declared the TLS active.these are two elements i dont see in you config, do you have any answers?Thanks again for this nice and, i hope someday helpful for me, tutorial !Adrien

  2. well sorry for my too quick post…TLS doesn’t seems to be “activated” or “declared”, and is guess the port is simply to be replaced from 465 to 587.forget my first question… ;)but still, would you have any idea to help me finding what the problem is?my php doesn’t say anything when sending emails, but i receive no email in the mailbox…thanx in advance and happy new yearAdrien

  3. Hi funkolector, Thanks for the note.First, if you do the above, and then, from a command line, run the command gmail-smtp, do you get a response like: 220 mx.google.com ESMTP 15sm5064500bwz.0If you do, then type the word QUIT to exit that session. If not, then the script isn’t running right. Re-perform the steps in the first box, then try it again. It should start with “sudo tee” and end “/usr/bin/gmail-smtp”Next, try telnetting to the port 10025 with this command:telnet localhost 10025Again, this should return the line something similar to:220 mx.google.com ESMTP 15sm5064500bwz.0If this works, then your xinetd is configured correctly. Again, if not, copy the second block, starting “sudo tee” and ending “xinetd reload”If it’s still not working after all of the above, then come back to me, and I’ll see what I can do :)

  4. Hello Jon, Sooo i got everything right, but still my php mail() function doesn’t return true…Actually i reinstalled the entire system tonite (yeah 3 o’clock…) and my system is pretty “clean”…I must be missing something… My sql, php and apache are ok (blogs runing on it).I don’t get it. I’ll try to have another look at my config files tomorrow.Thanx again for all your advices.A bientôt ;)Adrien

  5. Hi there, bit of an old entry this, but you might be able to help me out. I’ve done all of the above and the xinetd and bin test all work fine, i get the gmail smtp. when i do nullmailer-send i just get this though:Rescanning queue.Starting delivery, 3 message(s) in queue.Starting delivery: protocol: smtp host: 127.0.0.1 file: 1284728203.7247smtp: Failed: Connect failedSending failed: Connection refusedStarting delivery: protocol: smtp host: 127.0.0.1 file: 1284716297.5713smtp: Failed: Connect failedSending failed: Connection refusedStarting delivery: protocol: smtp host: 127.0.0.1 file: 1284686571.22076smtp: Failed: Connect failedSending failed: Connection refusedDelivery complete, 3 message(s) remain.which is slightly strange since when i telnet to localhost 10025 it works fine. so apart from using the send manually is there anywhere else i can see what goes wrong?

  6. I know this is a very old post, but just wanted to let you know that it helped me sort out my problems sending email from nullmailer via gmail.I think your code blocks have lost a few line breaks over time though!

  7. Hey Kevin,Sorry for not replying to the old note, but thanks for adding the new link… very interesting.Thanks for both of these, I hope they help someone else out!All the best, Jon

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.