?
Solved

want to receive email

Posted on 2012-08-14
5
Medium Priority
?
354 Views
Last Modified: 2012-08-16
please see
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27817846.html


how can this code be run on localhost

do I save as email.php and run from a browser
or does this work similar to paypal ipn where the data arrives to me

#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = NULL;

// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin " . PHP_EOL;
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38294130
This is a server script

Refer to the comments in the script

// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION

This means that the script must not be browsable.

The script is supposed to be called by the OS when new mail arrives for a specific account.

The instructions give directions on how to pipe (|) the new mail to the script but this appears to be specific to the system it will be run on - reference to cpanel not sure if this will work in all hosting environments.

You need to check with your ISP on how to pipe email to a script (if they don't use CPanel).

What do you want to do with the email?
0
 

Author Comment

by:rgb192
ID: 38295516
I never used this script

does it store email (text, database)
0
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 38295602
No - it reads the email and then sends it to another email address.

The basic principle is to have the mail program deliver the email to this script instead of to a mailbox. The script reads the mail from stdin (standard input because the contents of the email are being piped in) and builds a new mail which it sends to another address. Which is not really efficient because you could do this with a standard email foward / alias.

The script is meant to show what you need to do to be able to realtime process incoming emails and do something with the contents of the email.

If it does not need to be realtime you could have a script that simply reads a mailbox and process the mail that way i.e. setup a cron job to run a script that connects to the mail server with a specific account, reads all the emails processes each one and then terminates.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 38297036
This is kind of the Hello World of email pipe scripts.  It shows the principles of the essential moving parts.  If you wanted it to actually do anything meaningful with your applications, you would add that code in place of lines 54-end.  If you're going to use a data base or write files to the server filesystem, you will find that the environment does not work exactly the same way as a regular web-based script.  You might want to insert phpinfo() into your tests of this script so you can see what the environmental variables are.

I don't think this makes any sense on a localhost kind of installation, where your script is not running on a server that receives conventional email.  i would recommend putting it on a normal, over-the-web, server that already has the ability to receive email.

HTH, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 38300828
thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 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