CoderZone.org

Category: >> PHP Code >> Automatically process email Bookmark and Share

<< lastnext >>

Snippet Name: Automatically process email

Description: Automatically process incoming email with attachment(s) using PHP and PEAR Mail class.

Also see:
» Transfer or migrate messages betwe...
» Easy way to break apart an email a...
» Send iCal Email Meeting Requests u...
» Is email deliverable?
» Email attachments with PHP mail()
» Email w/ attachments class
» Heavy-duty SendMail function
» Use SendMail
» Live validation of email address
» Fake email addresses
» Validate email address #3
» Validate email address #2
» Validate email address #1

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: December 08th, 2010

<?php
 
/*
Prerequisites:
Linux/BSD
PHP with CLI enabled
PEAR
Sendmail with individual user account
 
Aim: Save attachments to server storage.
 
1. Setup sendmail account to forward to PHP script.
 
In the home directory (/home/user) on the foo.bar machine make 
and edit a file called ".forward". In this file put in the 
following.
 
|"/dir_to_php_script/process.php"
 
This will forward any email sent to user@foo.bar.com to the 
php script.
 
2. PHP script to process email "/dir_to_php_script/process.php"
[Don't forget to chmod 755 process.php]
 
*/
 
// begin contents of process.php
#!/usr/local/bin/php
<?php
// Need PEAR installed
include('Mail.php');
include('Mail/mime.php');
require_once 'Mail/mimeDecode.php';
 
// read email using stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
        $email .= fread($fd, 1024);
}
fclose($fd);
 
$params['include_bodies'] = true;
$params['decode_bodies']  = true;
$params['decode_headers'] = true;
 
$message=new Mail_mimeDecode($email);
$mailObj=$message->decode($params);
 
// Who is it from
$from=$mailObj->headers['from'];
// Get Subject
$subj=$mailObj->headers['subject'];
// Get Message Body
$body=$mailObj->parts[0]->body;
$gather="From:$from\nSubject:$subj\nBody:$body";
 
 
// Get and Save the Attachments
foreach($mailObj->parts as $key=>$val):
        $tmpObj=$mailObj->parts[$key];
        $tmp=$tmpObj->d_parameters['filename'];
        if(!empty($tmp)):
                $fd = fopen($tmp, 'w');
                fwrite($fd, $tmpObj->body);
        endif;
 
} //endforeach;
 
// end of contents of process.php
?>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7