Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

question about forwarding variables to php from perl

by nathan0601 (Initiate)
on Dec 15, 2003 at 07:46 UTC ( [id://314774]=perlquestion: print w/replies, xml ) Need Help??

nathan0601 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am working with a host that has Mojo Mail installed.

What I would like to do is have the parse aspect of mojo receive the email sent to the command line email (piped to the mojo send mail script) grab the email, parse the information and then pass off the vars to a separate script in php either as post or get method so I can work with the variables with a php app as well.

Has anyone parsed email in perl and then passed the info off to php? asp? how about to another perl script?

I know that the mojo_send.pl file has the parsing handled, so what I thought could work would be to have that script stop shy of sending the email using mojo, and instead post the variables it has parsed to my php script.

Does perl have the ability to do this? The host I am working with does not have the ability to facilitate command line php email alias reception, so I am having to learn perl in a hurry.

Thanks in advance.

Edited by Chady -- fixed formatting of paragraphs
  • Comment on question about forwarding variables to php from perl

Replies are listed 'Best First'.
Re: question about forwarding variables to php from perl
by Roger (Parson) on Dec 15, 2003 at 08:11 UTC
    How about a perl script that reads STDIN for the Email text, and then forward to a php CGI via a POST method.

    use strict; use warnings; use LWP::UserAgent; use HTTP::Request; # get email from STDIN my $email = do { local $/; <STDIN> }; # do the parsing of EMAIL here... # ... # build variables to pass to PHP via post # var1, var2 are CGI variables my %vars = ( var1 => $var1, var2 => $var2, # ... ); # send the POST to php my $url = "http://server/cgi-bin/script.php"; my $us = new HTTP::Request; my $res = $us->post($url, \%vars);
Re: question about forwarding variables to php from perl
by SquireJames (Monk) on Dec 15, 2003 at 08:13 UTC
    Short Answer: Almost certainly yes

    You can do pretty much anything with Perl when it comes to manipulating Text. Using regular expressions will enable you to focus on the section of the "email" that you want and rip out the information that you're after.

    After that it's simply a case of passing the variables to the PHP script, so really it's reliant on a couple of major things.

    1. How the mail message gets to the Perl Script, and
    2. How the PHP script will accept your input.

    For example, are you simply running a PHP script and supplying the variables to STDIN, or do you need to send it as CGI data.

    More information/source code examples/sample "mail messages" for parsing would make it much more likely that you can get some more relevant help.

Re: question about forwarding variables to php from perl
by nathan0601 (Initiate) on Dec 15, 2003 at 08:30 UTC
    use Mail::Address; use Getopt::Long; use MOJO::Config; # configuration variables use MOJO::Mail::Send; # use the MOJO::Mail::Send module +to send e-mails with use MOJO::App::Guts; # use MOJO::Guts for misc subroutin +es use MOJO::MailingList::Subscribers; use strict; # get header, footer my $header; my $body; { local $/ = ""; $header = <STDIN>; undef $/; $body = <STDIN>; } my $test = 0; GetOptions("test" => \$test); #create a MOJO::Mail::Send object my $mh = MOJO::Mail::Send->new(); #get headers in name=>value pair my %mail_headers = $mh->return_headers($header); # 'normalize' the headers %mail_headers = $mh->clean_headers(%mail_headers); # extract 'To:' addresses my @To_addresses = Mail::Address->parse($mail_headers{To}); # extract anything in the CC: header my @Cc_addresses = Mail::Address->parse($mail_headers{Cc}); # plop them together @To_addresses = (@To_addresses, @Cc_addresses); # extract the 'From:' address my $From_address = (Mail::Address->parse($mail_headers{From}))[0]; my $from_to_check = 0; foreach my $this_address(@To_addresses){ my $from_test = $From_address->address; if($this_address->address =~ /$from_test/i){ $from_to_check = 1; last; } } if($from_to_check == 1){ warn("$PROGRAM_NAME $VER ERROR, quitting script to stop possible i +nfinite loop!"); exit; } # at this point is where I need the vars to post. Make sense? Obviousl +y, we would not need to use all of the above modules/libraries # the mojo script goes on and on to process emails as it would, but we + wish to avoid that so here we would execute the lines mentioned by R +oger. # build variables to pass to PHP via post my %vars = ( var1 => $var1, # the email vars would be here I believe? var2 => $var2, # ... ); # then we would do the post thing # send the POST to php my $url = "http://domain.com/test.php"; my $req = new HTTP::Request; my $res = $us->post($url, \%vars);


    The current set up is that the command line email alias forwards the message test@domain.com to this script. This script is then usually used to operate a perl mailing list discussion pacakge called mojo mail. I wish to simply use it to parse the email and move the vars on to php.

    Thanks, Nathan

      Maybe this will help you visualize this. (excuse the bad ASCII art)

         YOU    YOU
          |      |
          V      V
        +-----------+
        | WebServer |
        +-----------+
          |       |
          |       +------+
          V              V
       +------+     +------------+
       | Mojo |---->| Php Script |
       +------+     +------------+
      

      Calling Mojo is the blue path. In Mojo, you are posting to the script, which is the red path, and that's probably not what you want to do. You want the green path.

      So, you need Mojo to return to you, and YOU will have to post to the php script. which should be roughly something like this:

      use CGI qw/:standard/; my $url = "http://domain.com/test.php"; print start_form('POST', $url); print hidden($_, $vars{$_}) for keys %vars; print submit('Click to continue'); print end_form;

      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
      So, how do the variables that you want to pass enter the message?

      For example, if there is a space between each variable and an equals sign between each value (ie. var1=var1 var2=var2 var3=var3), and they are just entered in on ine line, you'd probably want something like this:

      chomp $body; # To get rid of the Carriage Return from the STDIN foreach my $temp (split(/ /,$body)) { # This will generate a loop for +each variable/value pair by splitting on spaces my @variable = split (/=/,$temp); # This splits by the equals sign + that is delimiting our values and variable names $vars{$variable[0]} = $variable[1]; # So generate a hash of the va +riable name and the variable value }
      This will allow you to generate the proper hash that you need to pass.
      As far as the web stuff goes, I'd better leave that to somebody else as I really haven't got a huge amount of experience with it, but it looks OK to me.
      Hi Nathan,

      What are the variables that you want to pass to PHP anyway? It will be quite easy to setup the CGI variables once you know what are the names and contents of the variables to be passed to the PHP script.

      I suspect that your PHP script might expect variables such as below:
      my %vars = ( from => $From_address, # assume multiple addressed are separated by comma to => join(',', @To_addresses), cc => join(',', @Cc_addresses), subject => $Subject, body => $Mail_body, # etc. ); # and then invoke the PHP script via POST # send the POST to php my $url = "http://domain.com/test.php"; my $req = new HTTP::Request; my $res = $us->post($url, \%vars);
      You might want to check the return result $res to see if the mail has been processed properly by the PHP script.

        Hi, The goal of what I am doing is simply to create a list serv app using php. So the php script needs to receive vars (everything that would have been in the email sent to the perl script, parsed into vars) The above posts are helpful. I will play with the script later after class. Thanks for your help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://314774]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found