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 with CLI enabled
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 machine make 
and edit a file called ".forward". In this file put in the 
This will forward any email sent to 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
// Need PEAR installed
require_once 'Mail/mimeDecode.php';
// read email using stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
        $email .= fread($fd, 1024);
$params['include_bodies'] = true;
$params['decode_bodies']  = true;
$params['decode_headers'] = true;
$message=new Mail_mimeDecode($email);
// Who is it from
// Get Subject
// Get Message Body
// Get and Save the Attachments
foreach($mailObj->parts as $key=>$val):
                $fd = fopen($tmp, 'w');
                fwrite($fd, $tmpObj->body);
} //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 
© | users online: 7