php-general Digest 12 Nov 2011 20:02:50 -0000 Issue 7564

Topics (messages 315650 through 315660):

Re: Pipe To A Program
        315650 by: tamouse mailing lists
        315651 by: Ron Piggott
        315652 by: tamouse mailing lists
        315653 by: Stuart Dallas
        315657 by: Stuart Dallas

question about best practice for coding sql statements
        315654 by: tamouse mailing lists
        315655 by: Stephen

mysqli question
        315656 by: Peet Grobler

problem with sending AT command in php
        315658 by: a dehqan
        315659 by: Negin Nickparsa
        315660 by: a dehqan

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Sat, Nov 12, 2011 at 1:38 AM, Ron Piggott
<ron.pigg...@actsministries.org> wrote:
> Does anyone know what variable the e-mail message is assigned within the 
> context of “Pipe To A Program”?  Is there a way to find out?  I can’t figure 
> this out.
>
> What I have tried so far is below:
>
> ===
> #!/usr/local/bin/php -q
> <?php
>
>
> foreach($_REQUEST as $key => $val) {
>     $$key = $val;
>
> $email_body .= "KEY: " . $key . "\r\n";
> $email_body .= "VAL: " . $val . "\r\n";
> $email_body .= "\r\n";
>
> }
>
> mail( user@domain , "Test Pipe To Program" , $email_body );
> ===
>
> The mail command works, but the e-mail message body is empty.   I am at a 
> loss of how to proceed.
>
> Ron

I'm not sure what's going on with your program, exactly. Here's what I
tried:

>>start script<<
<?php

foreach($_REQUEST as $key => $val) {
    $$key = $val; // do not know what this is supposed to do here...

    $email_body .= wordwrap("KEY: " . $key . "\n",70,"\n");
    $email_body .= wordwrap("VAL: " . $val . "\n",70,"\n");
    $email_body .= "\n";

}

print_r($email_body);

mail( 'tamara@localhost' , "Test Pipe To Program" , $email_body );
>>end script<<

(Note: according to http://us3.php.net/manual/en/function.mail.php:

"Each line should be separated with a LF (\n). Lines should not be
larger than 70 characters."

Which is why I added the wordwrap on each line and terminated the
lines with "\n" instead of "\r\n".)

Calling the script, print_r showed the string as expected:

>>start output<<
tamara@caesar:~/$ curl 'http://localhost/~tamara/testmail.php?a=1&b=2&c=3'
KEY: a
VAL: 1

KEY: b
VAL: 2

KEY: c
VAL: 3

>>end output<<

And the email message showed up in my inbox:

tamara@caesar:~/$ from
<www-data@caesar>       Test Pipe To Program

And the contents of the email are:

>>start email<<
Return-Path: <www-data@caesar>
Delivery-Date: Sat Nov 12 02:54:13 2011
Envelope-to: <tamara@localhost>
Return-path: <www-data@caesar>
Received: from www-data by caesar with local (masqmail 0.2.27) id
 1RP9Lp-0oD-00 for <tamara@localhost>; Sat, 12 Nov 2011 02:54:13 -0600
To: tamara@localhost
Subject: Test Pipe To Program
X-PHP-Originating-Script: 1000:testmail.php
>From: <www-data@caesar>
Date: Sat, 12 Nov 2011 02:54:13 -0600
Message-ID: <1RP9Lp-0oD-00@caesar>


KEY: a
VAL: 1

KEY: b
VAL: 2

KEY: c
VAL: 3
>>end email<<

--- End Message ---
--- Begin Message ---


I used your code and it still didn't work. Would you show me what you put in for your Pipe To A Program settings?

What I used is:

Rules: "To" "Contains" customer service e-mail address

"Action"
/usr/local/bin/php /path/to/email_to_ticket_gateway.php

- I know the rule is working because I receive an empty e-mail, just not passing the e-mail content



Ron Piggott



www.TheVerseOfTheDay.info

-----Original Message----- From: tamouse mailing lists
Sent: Saturday, November 12, 2011 4:04 AM
To: Ron Piggott
Cc: php-gene...@lists.php.net
Subject: Re: [PHP] Pipe To A Program

On Sat, Nov 12, 2011 at 1:38 AM, Ron Piggott
<ron.pigg...@actsministries.org> wrote:
Does anyone know what variable the e-mail message is assigned within the context of “Pipe To A Program”? Is there a way to find out? I can’t figure this out.

What I have tried so far is below:

===
#!/usr/local/bin/php -q
<?php


foreach($_REQUEST as $key => $val) {
    $$key = $val;

$email_body .= "KEY: " . $key . "\r\n";
$email_body .= "VAL: " . $val . "\r\n";
$email_body .= "\r\n";

}

mail( user@domain , "Test Pipe To Program" , $email_body );
===

The mail command works, but the e-mail message body is empty. I am at a loss of how to proceed.

Ron

I'm not sure what's going on with your program, exactly. Here's what I
tried:

start script<<
<?php

foreach($_REQUEST as $key => $val) {
   $$key = $val; // do not know what this is supposed to do here...

   $email_body .= wordwrap("KEY: " . $key . "\n",70,"\n");
   $email_body .= wordwrap("VAL: " . $val . "\n",70,"\n");
   $email_body .= "\n";

}

print_r($email_body);

mail( 'tamara@localhost' , "Test Pipe To Program" , $email_body );
end script<<

(Note: according to http://us3.php.net/manual/en/function.mail.php:

"Each line should be separated with a LF (\n). Lines should not be
larger than 70 characters."

Which is why I added the wordwrap on each line and terminated the
lines with "\n" instead of "\r\n".)

Calling the script, print_r showed the string as expected:

start output<<
tamara@caesar:~/$ curl 'http://localhost/~tamara/testmail.php?a=1&b=2&c=3'
KEY: a
VAL: 1

KEY: b
VAL: 2

KEY: c
VAL: 3

end output<<

And the email message showed up in my inbox:

tamara@caesar:~/$ from
<www-data@caesar> Test Pipe To Program

And the contents of the email are:

start email<<
Return-Path: <www-data@caesar>
Delivery-Date: Sat Nov 12 02:54:13 2011
Envelope-to: <tamara@localhost>
Return-path: <www-data@caesar>
Received: from www-data by caesar with local (masqmail 0.2.27) id
1RP9Lp-0oD-00 for <tamara@localhost>; Sat, 12 Nov 2011 02:54:13 -0600
To: tamara@localhost
Subject: Test Pipe To Program
X-PHP-Originating-Script: 1000:testmail.php
From: <www-data@caesar>
Date: Sat, 12 Nov 2011 02:54:13 -0600
Message-ID: <1RP9Lp-0oD-00@caesar>


KEY: a
VAL: 1

KEY: b
VAL: 2

KEY: c
VAL: 3
end email<<


--- End Message ---
--- Begin Message ---
On Sat, Nov 12, 2011 at 5:12 AM, Ron Piggott
<ron.pigg...@actsministries.org> wrote:
>
>
> I used your code and it still didn't work.  Would you show me what you put
> in for your Pipe To A Program settings?
>
> What I used is:
>
> Rules: "To" "Contains" customer service e-mail address
>
> "Action"
> /usr/local/bin/php /path/to/email_to_ticket_gateway.php
>
> - I know the rule is working because I receive an empty e-mail, just not
> passing the e-mail content

This sounds more like a CPanel question than a PHP question, however,
if your action above is what gets executed, there will be no $_REQUEST
global as it is run as a cli program.

--- End Message ---
--- Begin Message ---
On 12 Nov 2011, at 07:38, Ron Piggott wrote:

> I am looking at CPanel’s “E-Mail filtering” option “Pipe To A Program”
> http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/FilterOptions
> 
> The goal I am working towards is saving the contents of an incoming e-mail 
> address into a mySQL table.  I am thinking of trying to program a customer 
> contact center application.
> 
> Does anyone know what variable the e-mail message is assigned within the 
> context of “Pipe To A Program”?  Is there a way to find out?  I can’t figure 
> this out.  
> 
> What I have tried so far is below:
> 
> ===
> #!/usr/local/bin/php -q
> <?php
> 
> 
> foreach($_REQUEST as $key => $val) {
>     $$key = $val;
> 
> $email_body .= "KEY: " . $key . "\r\n";
> $email_body .= "VAL: " . $val . "\r\n";
> $email_body .= "\r\n";
> 
> }
> 
> mail( user@domain , "Test Pipe To Program" , $email_body );
> ===
> 
> The mail command works, but the e-mail message body is empty.   I am at a 
> loss of how to proceed.


When you pipe email to a program the mail server literally does that - it pipes 
the contents of the email to the stdin of your program.

In PHP you would then read the email contents like so...

    $email = file_get_contents('php://stdin');

Note that what you get is the raw email, complete with headers. When I do this 
in PHP I use the Mailparse extension: http://php.net/book.mailparse

Here's a (somewhat over-complicated) example from an old version of TwitApps 
when I processed Twitter email notifications: https://gist.github.com/1360403

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
On 12 Nov 2011, at 11:49, Ron Piggott wrote:

> Ok.  It works.  I finally am able to display the contents of an e-mail!  Now 
> I am able to write my apps
> 
> I see your monster of all incoming e-mail processors ;)  Thanks for showing 
> me this.  I will look it over when I am more awake.  I kind of get it right 
> now, but I need to look more closely.

Good stuff. I've resurrected an article that used to be on my site that 
explains that example code in a bit more detail: 
http://stut.net/2011/11/12/handling-email-notifications/ - hopefully that will 
make it less of a monster :)

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


> -----Original Message----- From: Stuart Dallas
> Sent: Saturday, November 12, 2011 6:21 AM
> To: Ron Piggott
> Cc: php-gene...@lists.php.net
> Subject: Re: [PHP] Pipe To A Program
> 
> On 12 Nov 2011, at 07:38, Ron Piggott wrote:
> 
>> I am looking at CPanel’s “E-Mail filtering” option “Pipe To A Program”
>> http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/FilterOptions
>> 
>> The goal I am working towards is saving the contents of an incoming e-mail 
>> address into a mySQL table.  I am thinking of trying to program a customer 
>> contact center application.
>> 
>> Does anyone know what variable the e-mail message is assigned within the 
>> context of “Pipe To A Program”?  Is there a way to find out?  I can’t figure 
>> this out.
>> 
>> What I have tried so far is below:
>> 
>> ===
>> #!/usr/local/bin/php -q
>> <?php
>> 
>> 
>> foreach($_REQUEST as $key => $val) {
>>    $$key = $val;
>> 
>> $email_body .= "KEY: " . $key . "\r\n";
>> $email_body .= "VAL: " . $val . "\r\n";
>> $email_body .= "\r\n";
>> 
>> }
>> 
>> mail( user@domain , "Test Pipe To Program" , $email_body );
>> ===
>> 
>> The mail command works, but the e-mail message body is empty.   I am at a 
>> loss of how to proceed.
> 
> 
> When you pipe email to a program the mail server literally does that - it 
> pipes the contents of the email to the stdin of your program.
> 
> In PHP you would then read the email contents like so...
> 
>   $email = file_get_contents('php://stdin');
> 
> Note that what you get is the raw email, complete with headers. When I do 
> this in PHP I use the Mailparse extension: http://php.net/book.mailparse
> 
> Here's a (somewhat over-complicated) example from an old version of TwitApps 
> when I processed Twitter email notifications: https://gist.github.com/1360403
> 
> -Stuart
> 
> -- 
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/

--- End Message ---
--- Begin Message ---
I'm curious how others feel about this. When I'm creating an SQL
string, either for a non-prepared or prepared execution, I build it in
a variable and then pass the variable to the query or prepare
function. This lets me easily add something to view the SQL statement,
and also if there's an error, emit the SQL statement. Thus:

$sql = "SELECT * FROM TABLE WHERE id=$id";
if ($debug) error_log($sql." from ".__FILE__.' at '.__LINE__' in
'__FUNCTION__.PHP_EOL); // just an example
$rslt = $db->query($sql) or die("Could not get result from $sql:
".$db->errno.": ".$db->error.".".PHP_EOL);

and so on...

--- End Message ---
--- Begin Message ---
On 11-11-12 06:30 AM, tamouse mailing lists wrote:
I'm curious how others feel about this. When I'm creating an SQL
string, either for a non-prepared or prepared execution, I build it in
a variable and then pass the variable to the query or prepare
function. This lets me easily add something to view the SQL statement,
and also if there's an error, emit the SQL statement. Thus:

$sql = "SELECT * FROM TABLE WHERE id=$id";
if ($debug) error_log($sql." from ".__FILE__.' at '.__LINE__' in
'__FUNCTION__.PHP_EOL); // just an example
$rslt = $db->query($sql) or die("Could not get result from $sql:
".$db->errno.": ".$db->error.".".PHP_EOL);

and so on...
While I am not a big fan of OOP, I do like PDO, and recommend its use.

This is a sample function I have to maintain a record based on POSTed changes:

function updatecategory($dbh, $x) {

 $sql = "UPDATE gallery_category
          SET category_name = :name,
              category_description = :description
          WHERE category_id = :id";

  $catname = fieldcheck($x['catname']);
  $catdescription = textfieldcheck($x['catdescription']);

  $stmt = $dbh->prepare($sql);

  try {
    $stmt->bindParam(':name', $catname);
    $stmt->bindParam(':description', $catdescription);
    $stmt->bindParam(':id', $x['cid']);
    $stmt->execute();
  } catch (PDOException $e) {
    return 'Error updating category orders: ' . $e->getMessage();
  }

return "Maintained category " . $catname;

}

PDO is very efficient when you are looping through updates, and the error handling is simple to code.

Using a disciplined format keeps everything readable and easy to use as a template for additional work.

Cheers
Stephen

--- End Message ---
--- Begin Message ---
Not sure if this is the correct mailing list to be asking this question
but here goes:

I've got a prepared statement.

$stmt = $dbh->prepare ("insert into test values (?, ?)")
        or die ("Error: " . $dbh->error);
$stmt->bind_param ('ii', $var1, $var2)
        or die ("Error: " . $dbh->error);
$stmt->execute()
        or die ("Error: " . $dbh->error);

Okay I've taken the "or die" statements and put it into a function
called 'db_error'. So it looks kinda like this:

        or db_error ($dbh, $__FILE__, $__LINE__);

My db error then prints out "$dbh->error" and FILE and LINE variables so
I know exactly where the error is.

I would, however, love to print out the actual SQL text that failed.
There's no way to determine it, the closest way I can think of is using
a $sql variable holding the 'prepare' sql - but that's not what's going
to the database.

On a mysqli error - I can only see the actual sql that caused the
problem on the development system, as mysql logging is turned on on
that. It's not possible to turn on on the live system the overhead would
probably kill us.

Anyone found a way to do this?

Peet

--- End Message ---
--- Begin Message ---
In The Name Of Allah The compassionate merciful

Hello ;
Am trying to send AT commands to my GSMmodem device , in linux with PHP
Am sure it is /dev/ttyusb1
But with [URL="http://pastebin.com/aut4ygXf"]this code[/URL] device respond
the same command and code prints that command
e.g if we send ATD00 it responds ATD00 , or with AT it responds AT instead
of Ok, why ?

Regards dehqan

--- End Message ---
--- Begin Message ---
are you sure about ATD03518726535\n?

 can you try if ( dio_write($handle, 'AT') )?

--- End Message ---
--- Begin Message ---
dio_write($handle, 'AT') & dio_write($handle, "AT") make firefox times out
on Waiting for localhost ... .
But dio_write($handle, "AT\n") makes it prints AT exactly the same command
or  Atttt > Atttt , ..

On Sat, Nov 12, 2011 at 10:02 PM, Negin Nickparsa <nickpa...@gmail.com>wrote:

> are you sure about ATD03518726535\n?
>
>  can you try if ( dio_write($handle, 'AT') )?
>

--- End Message ---

Reply via email to