Jump to content

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


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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.