I am working on a tool that allows a user to send an email to an address, specify a set of specific commands in the body of the message. A server process (this perl script) will receive this email, parse those commands out of the body of the message (not the Subject, these commands will be quite lengthy) and execute a wrapper script using these commands as "modifiers".

Right now, the code below works for the first test (testing for 'help' in the Subject line). I'm wondering if there's a way to do this by parsing out objects from the body, given a user's email to this address. Basically they'd send a message like:

To: er@foo.bar From: Joe User <juser@somewhere.com> Subject: Anything url: http://www.domain.com/ depth: 3 title: This is my title
I then need to parse out the objects passed after the keys (url, depth, title, though there are about 40 possible modifiers). I can do this with split on the ':' there, and I can enforce a specific format for the email, but I'm not sure of the "right" process to actually parsing keys and values out of the body of an incoming message.

Are there any modules that can help me here? I already have all the content/HTML parsing and generation code worked out, so that's not a problem.

Has anyone done something like this? Are there any security/processor issues with this? This will not be executing system commands, so that isn't an issue, but it will be passing commands into a wrapper script (which already works) and operating on them through this wrapper.

I need to expand this to allow parsing the keys and values passed in the body of the message. Once I have those parsed out, I need to then build a file which contains the results of this "run" of the wrapper script, tar'd up into a binary file, and sent as an attachment to the user as a reply to their original email.

My code so far looks like this, and works in this very limited scope.

use strict; use diagnostics; use warnings; use Date::Manip; use Mail::Mailer; my $date = UnixDate("today","%T on %b %e, %Y."); my $logfile = "/var/log/foo.bar.log"; my $mailer = Mail::Mailer->new(); my $admin = "admin\@foo.bar"; my $server_from = "\"Foo Reflector\" <er\@foo.bar>"; my $a = 1; my ($body, # entire message $message_header, # headers of message $message_body, # just the body itself $help, # help context ); while (<>) { my $line = $_; my $from = $line if /^From: /; help() if (/Subject.*?help.*/i); $body .= $line; $a++; } ($message_header = $body) =~ s/(.*?)\n\n.*/$1/s; ($message_body = $body) =~ s/.*?\n\n(.*)/$1/s; mail_admin(); open (LOGFILE, ">>$logfile") or die "$!"; print LOGFILE "-"x20, "\n"; print LOGFILE "$date - complete $0\n"; close LOGFILE; ########################################## # # Mail the administrator # ########################################## sub mail_admin { my $from =~ s/From: (.*)/$1/; chop($from); my $mailer->open({ From => $server_from, To => $from, Cc => $admin, Subject => "Test at $date", }) or die "Can't open $!\n"; print $mailer<<END_O_MAIL; $body $help ........................................... : The Email Server -+- er\@foo.bar : : for help, type HELP in the subject line : ........................................... END_O_MAIL $mailer->close; } ########################################## # # Display reponse to Help # ########################################## sub help { $help = " This is the help file. Thanks for needing help. If this had this been Real Help(tm), it would have helped. "; }

In reply to Email-based Content Generation Reflector by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.