Email reading with PHP for anonymizer like Craigslist's

I need to set up an system that allows me to issue an annonymous email address to allow people to reply to a classified ad, and then forwards that email to the actual email address that we have on file.

I am proficient with PHP and mysql, so that side is no problem, but I don't know how to capture the emails or the process to create unique addresses on the fly.

I am trying to duplicate the anonymize email system used by Craigslist.

I'd appreciate your help with code, concepts or pointing me to classes or programs.

Thanks!
LVL 6
birwinAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
The thing you most probably want is called an email "pipe" script.  Here is my teaching example.  The pipe script is started automatically whenever an email message is received at a given email address.  The pipe script can read the entire incoming email message, including all headers.  Given access to a data base, it is easy to envision how the pipe script might do a data base lookup and forward the email message with a PHP mail() command.

I am not quite sure how you would set things up to connect all the pipes.  Your inbound mail needs an address for each pipe script.  One idea might be to make an educated guess about how many classified ads you will have outstanding at any one time, and set up that many pipe addresses.  They could all go to the same PHP script.  Another way might be to have one pipe email address and put a unique id into the subject line of the message.  The unique id could facilitate a data base lookup.

Thoughts?
#!/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
 
birwinAuthor Commented:
Thank you Ray.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.