awwx.ws

sendgmail

Send email from Arc through Gmail via Perl

I happened to have some Perl code lying around which sends an email by logging into by Gmail account and sending my email through Gmail. This is nice because I don’t have to run my own mail server, and if I can successfully send an email from my Gmail account and it doesn’t get eaten as spam or whatever, I know it will also work when my application sends an email.

Personally I don’t care what language a useful library is written in as long as I can really easily call it from Arc. So here’s what a Perl program embedded in Arc looks like, at least when using my template hack:

(= gmailprog ‡
use strict;
use MIME::Lite;
use Net::SMTP::TLS;

my $msg = MIME::Lite->new(
        From    => «from»,
        To      => «to»,
        Subject => «subject»,
        Type    => 'TEXT',
        Data    => «message»
    )->as_string();

my $mailer = new Net::SMTP::TLS(
        "smtp.gmail.com",
        Port     => 587,
        User     => «user»,
        Password => «password»);
$mailer->mail(«user»);
$mailer->to(«to»);
$mailer->data;
$mailer->datasend($msg);
$mailer->dataend;
$mailer->quit;
‡)

(def sendgmail (data)
  (thread (fromstring (gmailprog strperl:data) (system "perl"))))

The ‡...‡ hack constructs a template, which is filled in by passing it a table (or a table-like object that can be called with a key to return a value) to fill in the field values. strperl converts an Arc string to a Perl string literal, carefully escaping quote and backslash characters as needed. Composing the table object data passed to sendgmail with strperl means that a data value such as “what's that?” will be inserted into the template as a Perl string like 'what\'s that?'. I run the whole thing in a separate thread, since it can take a few seconds to connect to Gmail.

Call sendgmail with a table containing your Gmail account name and password, and the From, To, Subject, and text of your email message:

(sendgmail
 (obj
  user     "andrew.wilcox"  ; put your Gmail account name here
  password "xxxsecret"      ; and your Gmail password here

  from     "Andrew Wilcox <andrew.wilcox@gmail.com>"
  to       "fred@example.com"
  subject  "Greetings Earthling!"
  message  "Press the green button... press it now..."))

Get this hack

You’ll need Perl and the Perl modules MIME::Lite and Net::SMTP::TLS. You can check if you have these modules installed by typing at the command line:

$ perl -MMIME::Lite -e ''
$ perl -MNet::SMTP::TLS -e ''
$

If you have the modules installed, Perl will print nothing (no news is good news), otherwise you’ll get an error. For information on how to install the modules, see How do I install Perl modules?

Then you’ll need this hack, sendgmail0.arc, and the hacks it relies on: scheme-values, ac, span, template, and toperl.

Or, using the hackinator:

$ hack \
    ycombinator.com/arc/arc3.1.tar \
    awwx.ws/scheme-values0.patch \
    awwx.ws/ac0.patch \
    awwx.ws/ac1.arc \
    awwx.ws/span0.arc \
    awwx.ws/template2.arc \
    awwx.ws/toperl0.arc \
    awwx.ws/sendgmail0.arc

unLicense

This code is in the public domain.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com