This page has been locked and cannot be edited.
!!!Send Your MIME with Perl Philip J. Hollenback%%% [@philiph|http://www.twitter.com/philiph] !!Introduction [Ben Rockwood | http://www.twitter.com/benr ] recently wrote a fairly comprehensive post about [sending mime messages from the command line | http://cuddletech.com/blog/?p=685] which I enjoyed thoroughly. It's always fun to analyze exactly how things like mime encodings and mail transports work. Well, I think it's fun anyway. Ben's solution of basically hand-crafting multipart mime messages and feeding them to sendmail is a fine way to go, although I find it a little error-prone and a little cumbersome. What about alternative solutions? You folks who know me have probably already figured out where this is headed: let's do it in perl! Thus I set off on my favorite pastime, translating all sysadmin tasks into little perl scripts. I probably need therapy. !!The Tools Perl! What else would you want to use? While there are other tools, perl is the best one based on my sample size of one (I polled myself). Anyway, once we've picked perl we need to figure out a way to generate and send emails with mime attachments. Let's step back a minute: why not just send straight text in the body and ignore mime? Well, there are a few reasons you might want to consider mime. As Ben notes, it tends to look nicer in modern mail programs. Also, what if you want to send both a text version of your data and an html version? Again, separate mime parts are the way to go. Perl is great because there are so many modules available on [CPAN | http://www.cpan.org]. On the other hand, getting things done in perl is extremely confusing because you have to wade through so many different modules on CPAN. I usually start by googling <code>perl blah</code> to get a sense of which modules to consider. I find that [Stack Overflow | http://www.stackoverflow.com ] often has the best answers. In this case, a few queries on <code>perl send mime mail</code> brought me to [this stackoverflow question |http://stackoverflow.com/questions/4113974/perl-mime-problem ] which at least gave me a sense that [MIME::Lite | http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm] was a good module to look at. Ultimately I settled on it because: # it's well-supported # it's a 'lite' module and we don't need a lot of features # I liked the docs !! The Goal Now my plan was formed: write a perl script using ~MIME::~Lite to send mail. One other requirement is that the script accept it's input on stdin. I sat down and did some coding. Let's look at that. First of course the standards: <verbatim> #!/usr/bin/perl -w use MIME::Lite; </verbatim> Next, create a message: <verbatim> # create a new mime message $msg = MIME::Lite->new( To => "email@example.com", Subject => "I'm a pretty princess", Type => 'multipart/mixed' ); </verbatim> Now we have a message. However, there's no content. Fix that by adding an attachment with <code>attach</code>. <verbatim> # add a text attachment $msg->attach( Type => 'TEXT', Data => "some text" ); </verbatim> and of course finally send the message: <verbatim> # send the message. $msg->send; </verbatim> I tested this script and it worked just fine. But of course there are a few problems. I can't change the subject or recipient, and I can't pipe anything to the script. Let's fix the problem of not being able to change the recipient and subject first. Since this script is a relatively quick and dirty hack, I'm not going to bother with getopts or do real option parsing. Just grab the command line arguments directly like this: <verbatim> $msg = MIME::Lite->new( To => $ARGV, Subject => "I'm a pretty princess", Type => 'multipart/mixed' ); </verbatim> Then you can call the script (let's name it <code>mmail.pl</code>) like this: <verbatim> mmail.pl firstname.lastname@example.org "I'm a pretty Princess" </verbatim> to control who to send the message to anw what the subject should be. !! Tubes Ok, so now we have a script that sends an email with some text as a mime attachment. However, we're still missing one big thing: piping text from the command line. I want to be able to do something like this: <verbatim> echo "Brony 4Lif" | mmail.pl email@example.com "I'm a pretty princess" </verbatim> My first thought was I could slurp the input into a an array and feed that to ~MIME::~Lite: <verbatim> my @lines = <STDIN>; ... # add stdin as a text attachment $msg->attach( Type => 'TEXT', Data => \@lines ) </verbatim> and indeed that works just fine. But wait, ~MIME::~Lite supports filehandles directly, so let's optimize prematurely: <verbatim> $msg->attach( Type =>'TEXT', FH => STDIN ); </verbatim> and that's about it. Throw a tiny amount of error checking in the script and here's the finished result: <verbatim> #!/usr/bin/perl -w use MIME::Lite; $ARGV || die "must supply to"; $ARGV || die "must supply subject"; # create a new mime message $msg = MIME::Lite->new( To => $ARGV, Subject => $ARGV, Type => 'multipart/mixed' ); # add stdin as a text attachment $msg->attach( Type =>'TEXT', FH => STDIN ); # send the message. $msg->send; </verbatim> !!! Wrap It Up Now obviously there are many ways to shave this yak. You could use pure shell as Ben did. You could use [mpack | http://linux.maruhn.com/sec/mpack.html]. You could call mutt on the command line (actually I like that option a lot). None of these are exactly the 'right' answer, that depends on your particular requirements. I do hope this article demonstrates that it's quite simple to write a small perl script as one solution to this problem. One nice thing about using perl is the flexibility it gives you. It's easy to add other mime parts or extend this script to a more general solution if you want to. Also, using a module like MIME::Light takes care of a lot of annoying details like ensuring your mime parts have the right content-type. Because who wants to deal with that sort of low-level baloney? CategoryGeekStuff CategoryPerl CategoryBlog
comments powered by Disqus.
What links here
Recent Changes Cached