Send email from Perl routed through Google Apps SMTP
I use Google Apps to manage the few email addresses I have for the listcentral.me domain. Google Apps is amazing! Very easy to use, and super powerful. For today’s small business, there is no better service for managing email, doc shares, and online calendars, specially considering the price!
Unfortunately, I have encountered a problem with using Google Apps. Fortunately there is also a solution to my problem!
The Problem
I have a few, on demand, features on List Central that sends emails to the users, for instance, there is an “Email this list” feature. This requires that I send emails with the from address as lists@listcentral.me from my webserver to the varied email addresses of my future users. Because Google knows all about my domain listcentral.me, if any email I send from my webserver arrives at a google managed email address, google knows that the email didn’t come from it’s system, and it assumes that it must be dealing with a spammer, or other such scam. This is not good for business, as I cannot have the emails sent from the List Central system being marked as spam!
The Solution
It is possible to route the emails sent from the List Central system through the Google Apps SMTP (Simple Mail Transfer Protocol) system, which has the same effect as actually sending the email through the Google Mail interface. A copy is even saved in the Sent folder!
Using the instructions detailed by Robert Maldon, I was able to get my emails sending through Google’s SMTP server, and passing though the spam checks without too much hassle!
There was one sticking point! For those on Debian, one of the perl packages that you much install, Net::SMTP::SSL, is available in a Debian package only for lenny. I opted to upgrade from etch for this!
The Implementation
I added a little bit to Maldon’s solution to include the multipart/alternative option in order to send both plain text and html versions of the email and allow the email viewer to choose which to display. The code is as follows:
my $fromEmail = 'lists@listcentral.me';
my $emailPassword = 'emailpassword';
my $toEmail = 'someone@somewhere.com';
my $text = 'Some email message without fancy formatting!';
my $html = '<b>Some email message with fancy html formatting!!</b>';
my $smtp;
if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com',
Port => 465,
Debug => 1)) {
die "Could not connect to mail servern";
}
$smtp->auth($fromEmail, $emailPassword) || die "Authentication failed!n";
$smtp->mail($fromEmail . "n");
my @recepients = split(/,/, $toEmail);
foreach my $recp (@recepients) {
$smtp->to($recp . "n");
}
$smtp->data();
$smtp->datasend("From: " . $fromEmail . "n");
$smtp->datasend("To: " . $toEmail . "n");
$smtp->datasend("Subject: " . $subject . "n");
$smtp->datasend("MIME-Version: 1.0n");
$smtp->datasend("Content-Type: multipart/alternative; boundary="$boundary"n");
$smtp->datasend("n--$boundaryn");
$smtp->datasend("Content-Type: text/plain; charset=iso-8859-1n");
$smtp->datasend("Content-Transfer-Encoding: quoted-printablen");
$smtp->datasend($text . "nn");
$smtp->datasend("n--$boundaryn");
$smtp->datasend("Content-Type: text/html; charset=iso-8859-1n");
$smtp->datasend("Content-Transfer-Encoding: quoted-printablen");
$smtp->datasend($html . "n");
$smtp->dataend();
$smtp->quit;
In my figuring this out, I nearly fell into a couple of traps. Here’s the lessons learned:
- Google Email only allows SSL Authentication. There is no way around it.
- This means a Debian upgrade to Lenny if you want to use the pre-built Debian package libnet-smtp-ssl-perl
- You do not have to upgrade to Google Apps Enterprise to make this happen. There is some Google documentation about Email Routing that can lead you to think you might need to upgrade for this feature, but this is a completely different feature!






















Discussion