Get the e-mail header

Posted on 2005-04-04
Medium Priority
Last Modified: 2007-12-19
Im trying to understand/write email forwarder. When incoming email arrive, im feeding it to program a.cgi where it will get the sender, title and body and put in database. Later i can forward it to appropriate person.

My question is: how can i get the variable sender/title/body without depending on other module???
Thank in advance.
Question by:spreq
LVL 20

Accepted Solution

jmcg earned 100 total points
ID: 13704332
I thought I could answer this, but, on re-reading, I find I can't answer with any definiteness.

Where is the maill arriving from? Are you presenting users with an email HTML form and a.cgi is the action for the form? Or is the mail coming from somewhere else?

If you have an HTML form, you have control over the naming of the form variables that will contain the sender, title, and body.

Writing cgi scripts without using the CGI module is an exercise for learning some of the mechanics of how CGI works, but you'll need to do that yourself for it to be of any use.
LVL 85

Expert Comment

ID: 13704436

Expert Comment

ID: 13704713
just use regular expression to get the important strings from the mail
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13704778
To jmcg:
This email is just like ordinary email, and for one particular account (eg: mrX) i want to 'feed to program' named a.cgi. (the term used by webmin). Notice that this doesnt involve any html form... Thats it, no need for auto reply.
This program then will parse, extract all info and put to the db. My concern is just how to get the data in easier way (not using hevyweight program and module, if any).

To ozo:
I spent all morning to look for the suitable prgram, and also tried the mail::simple. (of course with all the prerequisite). Also read from http://www.perl.com/pub/a/2004/06/10/email.html
I got problem installing the module:
"Can't locate Email/Simple.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-sol
aris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_
perl/5.005 .) at a.cgi line 2." although i already did make..makeinstall ...

Just back from lunch, i will try again install the module..
but i really appreciate if any of you could provide non-module-dependant solution.

my humble thank you for both of you...

Author Comment

ID: 13704798
that what im trying to do Talmash.. :-)
the problem is just, to create that variable...

is there any variable that contain/convert all incoming email?
i think i can get the data using reg exp as you suggested..

LVL 20

Expert Comment

ID: 13705744
So is this program a cgi script? It sounds like it's not.
LVL 18

Assisted Solution

kandura earned 100 total points
ID: 13705948
Generally email filters get their data from pipes spawned by the smtp daemon.
Sometimes this is as easy as adding an entry to /etc/aliases:

    support: admins,"|/usr/bin/rt-mailgate 'Company Support' correspond"

That one's from Request Tracker. Email to support@mydomain.com gets forwarded to the alias admins, and to the program rt-mailgate.

I recently did something like this in Postfix, where I had to process any email for any address that comes into the server.
I added these lines to /etc/postfix/master.cf:

    # mail filter for project archive
    # added: 2005-02-14 23:56:00 UTC by Kandura
    filter    unix  -       n       n       -       10      pipe
        flags=Rq user=nobody argv=/usr/local/projects/mailfilter.pl -f ${sender} -- ${recipient}
Any mail coming into the server is now piped through mailfilter.pl.
That script looks a bit like this:

    use Email::Filter;
    use Email::MIME;
    use Email::Address;

    my $mail      = Email::Filter->new( ); # read from STDIN
    my($mid)      = Email::Address->parse(
                    $mail->header('Message-ID') );
    $mid        ||= Email::MessageID->new;

    my $mime      = Email::MIME->new(
                    $mail->simple->as_string );

    my $subject   = $mime->header('Subject');
    my($sender)   = Email::Address->parse( $mail->header('From') );

    my %seen;
    my(@rcpts)    = sort grep !$seen{$_}++, map { $_->address } Email::Address->parse(

It then goes on to process the mail according to our needs, but this should be enough to demonstrate the basics.
As you can see, by using the modules from the Perl Email Project, the script is quite readable, and therefore maintainable.
I'm not saying you can't write all that yourself, but using those modules gives you the time to concentrate on your specific needs, instead of having to reinvent the wheel.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question