Solved

how to use a perl script in php

Posted on 2001-06-19
18
313 Views
Last Modified: 2012-06-27
I have a perl-script for sending a mail (don't ask - I *need* it for sending mails!).
In perl I use it like:
--
open(MAIL, "|mail.pl");
print MAIL "From: \"xx\@xx.com\" <xx\@xx.com>\n".
          "To: \"xx\@xx.com\" <xx\@xx.com>\n".
          "Subject: TEST\n\n".
          "This is the body";
close(MAIL);
$error=($? >> 8);
if ($error) { print "Error: ".$error."\n";}
--
how could this be best implement in php (including the $error-thing!)

tanks
witty
0
Comment
Question by:witty
  • 6
  • 5
  • 2
  • +4
18 Comments
 
LVL 4

Expert Comment

by:ykf2000
ID: 6209029
HI try this:


$content = "Your content here";
$to = "myemail@mydomain.com";
$from = "youemail@yourdomain.com";
$subject = "Your Subject Here";

$success = mail($to,$subject,$content,"From: $from");

if(!$success)
{
   echo "Error occured during sending.";
}


0
 
LVL 1

Author Comment

by:witty
ID: 6209233
ykf2000:

thanks for your comment, but it *must* be a solution using MY perl-script "mail.pl" to mail (NOT the built-in mail-function!)!
---
open(MAIL, "|mail.pl");  <-- 1.how to do this???
print MAIL $rawmail;
close(MAIL);

2. and how to do the return-value-check???

thanks
witty
0
 
LVL 8

Expert Comment

by:us111
ID: 6209357
have a look at the network function fsockopen
Sorry I cannot help you more :(
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:LexZEUS
ID: 6209518
"maybe" this works (not tested) :

<?
srand((double)microtime()*1000000);
$temp_file = "temp".rand(0,32000);

$mail =  "From: \"xx\@xx.com\" <xx\@xx.com>\n".
         "To: \"xx\@xx.com\" <xx\@xx.com>\n".
         "Subject: TEST\n\n".
         "This is the body";

$fp=@fopen($temp_file,"w");@fputs($fp,$mail);@fclose($fp);
@exec ("cat $temp_file | .\mail.pl",$error_value);

@unlink($temp_file);
print "ERROR FLAG : ".join("",$error_value);
?>

PS: mail.pl must be in current directory, and must executable (chmod 777 mail.pl if u r not sure) ...

rgds,

Alex
0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6211506
Yea I totally agree with Alex but I advice you to use the php mail function as its easy and short.

harry
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6212731
Actually the code above can be replaced with function call popen, see from php manual at www.php.net ... you can pipe the mail string, then get the result ... it is worth to try ... but the code above is the easiest way ... :)

As harwan said, actually it is easier to use mail(), unless there is some bussines-rule behind your "mail.pl" ...
0
 
LVL 1

Author Comment

by:witty
ID: 6213155
LexZEUS:

thanks for your comments!
I think your code will work - but I don't like temporyra files!
I already tried with popen - works ok - but how to do the error-check???

I think I will describe the problem, I solved with perl - perhaps there is some other idea!

if I send a mail with the php-mail()-function, and the mail bounces (wrong address - server down ...), the sysadmin of my ISP will get the bouncing mail!
I tried all possible headers (Return-Path, Error-To, Sender, ...) to redirect the bouncing mail to my address but nothing worked - all bouncing-mails go to root!

In Perl I found a way to write a mail as usual, *but* I send the mail by specifying the real-sender and the real-receiver!!!

real-sender: bounce@myip.com
real-receiver: someoneelse@yourip.com

Mail to be sent:
--
From: me@myip.com
To: you@yourip.com
Subject: TestMail

Body-Text
--

If the mail is delivered, the receiver (someoneelse@yourip.com) will only see what is written in the mail above (he won't see the real-sender and the real-receiver in *any* header)!
If the mail isn't delivered correctly bounce@myip.com will get a message!

And I want to implement this with php!!!
Is this possible???

(for an example you can look at http://perl.about.com/compute/perl/library/weekly/aa022000d.htm )


thanks
witty
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6213246
Open your php.ini (I assume you're using Linux/UNIX), search and edit sendmail_path directive to :

sendmail_path=/usr/sbin/sendmail -f bounce@myip.com -t -i

Then, in your PHP code, you can use ordinary mail() function :

mail ("xx@anywhere.com", "Hello !", "This is message !",
      "From: \"Robot\" <robot@anywhere.com\n".
      "Reply-to: customer-service@anywhere.com\n");

It will send mail to xx@anywhere.com, the sender is Robot, and when user reply, it will go to customer service, but if it bounce, it will go to bounce@myip.com ...
(I think it will work fine) ...

rgds,

Alex
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6213250
sorry :
   "From: \"Robot\" <robot@anywhere.com\n".

suppose to be :

   "From: \"Robot\" <robot@anywhere.com>\n".

I miss out a ">" ...


0
 
LVL 1

Author Comment

by:witty
ID: 6213514
thanks again

1. (php.ini) I'm not root on this machine, but one of 200 other customers - so I can't change php.ini!
2. (tried sendmail-command) the bounce-functionality works, but I will find the "bounce-address" in the Reply-To:-header (you shouldn't see the address in *ANY* header!)
And the To:-header is the recipient (the real-recipient shouldn't be seen in any header to!!! except X-Deliver-To: or Received: ... for x@xx.com)

how to hide these?
(if you like, take a look at the perl-script mentioned above - it really works!!!)

thanks
witty
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6213690
Maybe you need a similar SMTP function for your case, I found this :

<?
  /* Pop3 Functions.
     Smtp Functions. [Added later, going to split]

     For a real example take a look at:
     http://www.triple-it.nl/pop3/

     I have just started to explore the world of PHP3,
     so excuse (and correct :) me if I'm doing something wrong.

     Unk. (rgroesb_garbage@triple-it_garbage.nl)
   */

  function pop3_open($server, $port)  
  {
        global $POP3_GLOBAL_STATUS;

        $pop3 = fsockopen($server, $port);
        if ($pop3 <= 0) return 0;

        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        return $pop3;
  }

  function pop3_user($pop3, $user)
  {
        global $POP3_GLOBAL_STATUS;

        fputs($pop3, "USER $user\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        return 1;
  }

  function pop3_pass($pop3, $pass)
  {
        global $POP3_GLOBAL_STATUS;

        fputs($pop3, "PASS $pass\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        return 1;
  }
   
  function pop3_stat($pop3)
  {
        global $POP3_GLOBAL_STATUS;

        fputs($pop3, "STAT\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        if (!eregi("+OK (.*) (.*)", $line, $regs))  
                return 0;

        return $regs[1];
  }

  function pop3_list($pop3)
  {
        global $POP3_GLOBAL_STATUS;
   
        fputs($pop3, "LIST\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        $i = 0;
        while  (substr($line  =  fgets($pop3, 1024),  0,  1)  <>  ".")
        {
                $articles[$i] = $line;
                $i++;
        }
        $articles["count"] = $i;

        return $articles;
  }

  function pop3_retr($pop3, $nr)
  {
        global $POP3_GLOBAL_STATUS;
   
        fputs($pop3, "RETR $nr\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        while  (substr($line  =  fgets($pop3, 1024),  0,  1)  <>  ".")
        {
                $data[$i] = $line;
                $i++;
        }
        $data["count"] = $i;

        return $data;
  }

  function pop3_dele($pop3, $nr)
  {
        global $POP3_GLOBAL_STATUS;

        fputs($pop3, "DELE $nr\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;


        return 1;
  }
  function pop3_quit($pop3)
  {
        global $POP3_GLOBAL_STATUS;

        fputs($pop3, "QUIT\r\n");
        $line = fgets($pop3, 1024);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
        $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;

        return 1;
  }

  function smtp_open($server, $port)
  {
        global $SMTP_GLOBAL_STATUS;

        $smtp = fsockopen($server, $port);
        if ($smtp < 0) return 0;

        $line = fgets($smtp, 1024);

        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;

        return $smtp;
  }

 
  function smtp_helo($smtp)
  {
        global $SMTP_GLOBAL_STATUS;

        /* 'localhost' always works [Unk] */
        fputs($smtp, "helo localhost\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;

        return 1;  }
 

  function smtp_mail_from($smtp, $from)
  {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "MAIL FROM: <$from>\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;

        return 1;
  }
 
  function smtp_rcpt_to($smtp, $to)
  {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "RCPT TO: <$to>\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;


        return 1;
  }

  function smtp_data($smtp, $subject, $data)
  {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "DATA\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "3") return 0;

        fputs($smtp, "Mime-Version: 1.0\r\n");
        fputs($smtp, "Subject: $subject\r\n");
        fputs($smtp, "$data\r\n\r\n");
        fputs($smtp, ".\r\n");
        $line = fgets($smtp, 1024);
        if (substr($line, 0, 1) <> "2")
                return 0;  

        return 1;
  }
 
  function smtp_quit($smtp)
  {
        global $SMTP_GLOBAL_STATUS;

        fputs($smtp, "QUIT\r\n");
        $line = fgets($smtp, 1024);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
        $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);

        if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;

        return 1;
  }  
?>  

   
Please see this function :
  function smtp_open($server, $port)
  function smtp_mail_from($smtp, $from)
  function smtp_rcpt_to($smtp, $to)
  function smtp_data($smtp, $subject, $data)
  function smtp_quit($smtp)

It looks similar to the functions you needed ...
0
 

Expert Comment

by:roglewis
ID: 6275155
Invoke your perl script using the following line in PHP:

$result = exec("/path/to/perlscript.pl arg1 arg2 arg3");

Then return something from the script (don't just print the error) and it will be sent to $result.

If you want to kick off the Perl script and not wait for returned information, use this syntax:

exec("/path/to/perlscript.pl arg1 arg2 arg3 >/dev/null &");
0
 
LVL 1

Author Comment

by:witty
ID: 6277442
roglewis:
you haven't even partly read my question ->
input is from STDIN and I want to get the errorlevel in return - no error-message!!!

witty
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6419162
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.

PLEASE DO NOT AWARD THE POINTS TO ME.

____________________________________________



Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.

Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange

P.S.  REGARDING POINTS, Guidelines and more:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6855277
SECOND REQUEST...

ADMINISTRATION WILL BE CONTACTING YOU ABOUT YOUR OPEN QUESTIONS SHORTLY.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20130529.html
http://www.experts-exchange.com/questions/Q.20138123.html
http://www.experts-exchange.com/questions/Q.10800821.html
http://www.experts-exchange.com/questions/Q.20184558.html
http://www.experts-exchange.com/questions/Q.20256079.html
http://www.experts-exchange.com/questions/Q.11284819.html
http://www.experts-exchange.com/questions/Q.20272298.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.11561299.html
http://www.experts-exchange.com/questions/Q.20094368.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 8

Expert Comment

by:us111
ID: 6855952
LexZeus
0
 
LVL 5

Accepted Solution

by:
LexZEUS earned 150 total points
ID: 6855967
thx :)
0
 
LVL 1

Author Comment

by:witty
ID: 6861714
sorry - I forgot to accept an answer!

sorry for the B-ranking - but it wasn't exactly what I wanted!

thx to all of you!!

regards
witty
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 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