Jump to content

Processing an email with attachment(s) using PHP and PEAR Mail class


afrojojo

Recommended Posts

#!/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;

?>

 

When I send an email with no attachment from a local mail client and echo the $body variable, it shows up empty. The body shows up when there is an attachment though. It also shows up when sent from a webmail app whether there is an attachment or not. What gives?

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.