[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cgi-list
Subject:    Re: [CGI] E-mail to Webpage script! Thanks!
From:       Michael Dabrowski <michael () dabrowski ! org>
Date:       1998-11-23 21:20:00
[Download RAW message or body]

At 09:53 PM 11/23/98 +0200, you wrote:
>I am still working on e-mail the script the file.  I am running on LINUX with
>an Apache server on a x86 box (I guess all Linux is, for that matter).  I
have
>access to the CRONTAB.  I simply do not know how to read e-mail.  I can
set up
>an account and the mail gets written to any directory I choose.

If you do not have procmail on your system (easy way to check is to type
procmail on the command line), 

Then you would edit .forward in your client's home directory and put in
something that looks like this. (with the ")

"|/usr/local/bin/perl /home/client/dostuff.pl"

if you had procmail, you would invoke it by putting the following in
.forward instead:

"|IFS=' ' && exec /opt/gnu/bin/procmail -f- || exit 75 #accountname"

Account name being the account name of the client, /opt/gnu/bin/procmail
being the location of procmail.

If you do have procmail, you would then also create a .procmailrc file in
the client's home directory which would have something like the following:

:0 c
* ^TO.*listserv
| /bin/perl /disk35/usr/jong/public_html/news/listserv.pl

The procmail would filter all mail to the perl script that had "listserv"
in the To: line of hte email message, you can make it ^Subject.*whatever,
or ^From.*whatever, you can read up on the format in the manual pages of
procmail. This is an advantage, becuase it will put the mail back in the
inbox and send you a copy (with :O c), or it will send you the original
message you want, and put all others into the mail box. This makes you have
to think less about deciding which email goes where in your script.

If you do not have procmail and are going to be using .forward to forward
the email to your script, then you will have to decide if the email should
be processed or not with your own filters. Procmail is convenient in that
it will filter only the email messages that are pertinent to the script,
and put the rest back into the client's mail box, if you use .forward
without procmail, you will have to put the email back into the client's
mailbox yourself.

As far as how to process the mail, here's a few excerpts on how to read an
email. Both procmail, and .forward will run your script and feed your
script the email through standard input (STDIN).

------------------------
local($header) = "";    ## Stores mail header
local($body) = "";      ## Stores mail body
local($f) = 0;          ## header/body seperator flag
       
while(<STDIN>) {
        ## Read data from STDIN, and dump to $header
        ## or $body, depending on if a newline on its
        ## own line was found, thus delimiting the
        ## header form the body
        if($_ eq "\n") { $f = 1; }
        if($f == 0) { $header .= $_; }
        if($f == 1) { $body .= $_; }
}

=head1

One can seperate the header of the email from the body by a line with only
a '\n' on it.

=cut1

&parseheader($header);  ## Parse header into %array referenced by
                        ## keywords.

$body = &parsebody($body);
                        ## Remove possible HTML codes and
                        ## convert url's to links if possible 
sub parseheader
{
        local($header) = $_[0];
        local(@array);

        %array = ();
        $header =~s/\n\s+/ /g;
        @array = split(/\n/, $header);

        foreach $_ (@array)
        {
                ($keyw, $val) = m/^([^:\s]+):\s*(.*\S)\s*$/g;
                $keyw =~ y/A-Z/a-z/;
                if (defined($array{$keyw}))
                { $array{$keyw} .= ", $val"; }
                else { $array{$keyw} = $val; }
        }
}                     


=head1

	You can now reference all your header information through the %array.

	Example:

		print "From: $array{'from'}\n";
		print "To: $array{'to'}\n";
		print "Subject: $array{'subject'}\n";
		etc.....

parsebody() has already been included in a previous email of mine. That
will do some thorough URL checking and rewrite the body of your email
accordingly.

=cut1

------------
After you have the header and the body, you can manipulate it however you
want to.. including printing out html to a file. You can also decided if
the email that your script recieved is a valid one for the web, or if its a
private communication which should be put back into the mail box. In which
case, I would suggest doing @data = <STDIN>, and doing a foreach (@data)
instead of the while(<STDIN>) on the top, so that you can have a copy in an
array which you can append to the mailbox without having to reconstruct the
email from %array: open(MAIL, ">>/var/spool/mail/client"); print MAIL
@data; close(MAIL);



- Michael Dabrowski

____________________________________________________
Webmaster -- Datacourse Internet Solutions
Direct: 391-5974	email: mcd@datacourse.com
Cell: 829-9167	
Pager: 334-4244



-----------------------------------------------------------------
To unsubscribe, send mail to "majordomo@jann.com" with "unsubscribe cgi-list"
in the body.

The CGI Tips & Tricks website (and archive of the list) is located
at http://www.jann.com/Perl/

cgi-list is hosted by Summit Communication Design (http://www.summitdesign.net).

Summit Communication Design is a full-service web hosting firm
providing web design, cgi programming and inter/intra-net support.
-----------------------------------------------------------------

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic