Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: question about forwarding variables to php from perl

by nathan0601 (Initiate)
on Dec 15, 2003 at 08:30 UTC ( [id://314783]=note: print w/replies, xml ) Need Help??


in reply to question about forwarding variables to php from perl

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

Replies are listed 'Best First'.
Re: Re: question about forwarding variables to php from perl
by Chady (Priest) on Dec 15, 2003 at 11:57 UTC

    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/
Re: Re: question about forwarding variables to php from perl
by SquireJames (Monk) on Dec 15, 2003 at 08:54 UTC
    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.
Re: Re: question about forwarding variables to php from perl
by Roger (Parson) on Dec 15, 2003 at 12:02 UTC
    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.
        OK, we are making progress. I have perl and php talking now.

        How can I pass vars without a form action? Say I have the vars working if I use a submit button (I suppose they are 'hidden' vars.

        Now I want the perl script to perform a redirect sort of function, bringing the vars to the php script without any interactive user action needed- In other words I want to run blah.cgi and have it pass any values defined in blah.cgi to blah.php.

        Thanks for your patience and time with me. Nathan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-18 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found