The first thing to remember is that Gmail provides its SMTP server. This offers two major features. First, you can use the SMTP server from your own e-mail application, which is a great help if you’re traveling and your usual e-mail provider is unreachable. The second use is that every single scripting language you might desire to use has standard SMTP support available as a library, and the support for TLS encryption, which you need to connect to Gmail, is being added apace.
First, though, the settings you’ll need:
- Server name: smtp.google.com
- Username: [email protected]
- Password: Your Gmail password
- Security: Yes, using TLS
One thing to note about this technique is that Gmail will rewrite your e-mail headers. It replaces the From: and Reply-to: lines with your Gmail address because Gmail also automatically adds the so-called Domain Keys to their outgoing e-mails, allowing spam-hit system administrators to block fake Gmail mail from their servers. This wouldn’t work without the Domain Keys, this wouldn’t work, but Gmail can’t send a mail with a different from: or Reply-to: address without breaking the Domain Key.
Using the SMTP Server Programmatically
If you want to talk directly to the SMTP server instead of using the APIs featured in the rest of this chapter, then you will need to use a library that can deal with TLS encryption. Unfortunately, there is no standard module to do this within Perl or PHP at this writing, but Python users can use the standard smtplib, which comes with the Python distribution.
Sending Mail with Perl
Mail:: Webmail::Gmail module and the Utils. pm code introduced. It takes input from the keyboard and sends the mail directly. It’s straightforward to understand, so no walkthrough is necessary.
That script is, as you can see, straightforward. But it does provide the basis for any number of more complicated scripts. Being able to send mail from a hand isn’t a new thing — it’s pretty easy to do without Gmail — but doing it via Gmail does give you some advantages. First, it’s easier, but second, the mail is automatically archived. Using Gmail to handle outgoing mail from your applications can be more resilient, certainly more effortless, and much more helpful than doing it any other way. Next, you looked at downloading and reading new mail. A script combines the techniques you learned there with your newfound skills at sending mail.
Running this script produces a list of the new messages and gives you the option to choose one and reply. You should see how this works from the code, but let’s walk through it. The start is simple enough. You’re using the Utils. Pm module you created, and you want to log in.
So, in this short chapter, you learned how to send mail. The next chapter looks at the much more advanced concepts of organizing your mail inside Gmail programmatically. This will allow you to go on and use Gmail for more complicated applications.