10

We have a cron'ed PHP script that checks an inbox every ten minutes. The purpose of this script is to handle "STOP to quit" functionality for our SMS notification service we provide. If the script finds any emails with the word "STOP" at the beginning of the email, we remove the user from our notification database.

To cover our bases, we'd like any emails that don't meet the above criteria to be forwarded on to another email address (which is an alias) that several people receive and check hourly. However, we're running into problems forwarding the emails from this PHP script.

Knowing how the mail function of PHP works, it's quite obvious we need to reinsert the headers before mailing. However, MIME multipart emails always get sent as a garble of text, including the barriers and any base64 encoded attachments.

Does anyone know of a simple way to take an email message and forward it on properly using a PHP script?

We're using the native IMAP functions built in to PHP 5. We also have access to the PEAR Mail module. However, we have been unable to find any examples or people doing similar tasks by searching Google.

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
  • Still don't have a good solution to my problem. Adding a +50 bounty to a person who can provide proof-of-concept code or give good direction towards creating code that can forward emails via PHP while retaining MIME boundaries and attachments. – Michael Irigoyen Jan 08 '11 at 00:47
  • What MTA are you using (Postfix, sendmail,etc...)? – webbiedave Jan 10 '11 at 17:19

5 Answers5

6

I coded this method a long time ago to parse an email message into their appropriate parts using IMAP:

function Message_Parse($id)
{
    if (is_resource($this->connection))
    {
        $result = array
        (
            'text' => null,
            'html' => null,
            'attachments' => array(),
        );

        $structure = imap_fetchstructure($this->connection, $id, FT_UID);

        if (array_key_exists('parts', $structure))
        {
            foreach ($structure->parts as $key => $part)
            {
                if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT')))
                {
                    $filename = null;

                    if ($part->ifparameters == 1)
                    {
                        $total_parameters = count($part->parameters);

                        for ($i = 0; $i < $total_parameters; $i++)
                        {
                            if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME'))
                            {
                                $filename = $part->parameters[$i]->value;

                                break;
                            }
                        }

                        if (is_null($filename))
                        {
                            if ($part->ifdparameters == 1)
                            {
                                $total_dparameters = count($part->dparameters);

                                for ($i = 0; $i < $total_dparameters; $i++)
                                {
                                    if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME'))
                                    {
                                        $filename = $part->dparameters[$i]->value;

                                        break;
                                    }
                                }
                            }
                        }
                    }

                    $result['attachments'][] = array
                    (
                        'filename' => $filename,
                        'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($this->connection, $id, ($key + 1), FT_UID))),
                    );
                }

                else
                {
                    if ($part->subtype == 'PLAIN')
                    {
                        $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID);
                    }

                    else if ($part->subtype == 'HTML')
                    {
                        $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID);
                    }

                    else
                    {
                        foreach ($part->parts as $alternative_key => $alternative_part)
                        {
                            if ($alternative_part->subtype == 'PLAIN')
                            {
                                echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                                $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }

                            else if ($alternative_part->subtype == 'HTML')
                            {
                                echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                                $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }
                        }
                    }
                }
            }
        }

        else
        {
            $result['text'] = imap_body($this->connection, $id, FT_UID);
        }

        $result['text'] = imap_qprint($result['text']);
        $result['html'] = imap_qprint(imap_8bit($result['html']));

        return $result;
    }

    return false;
}

I've never tested it deeply and I'm sure it has some bugs, but it might be a start... After adapting this code you should be able to use the $result indexes (text, html, attachments) with your forwarding script (using SwiftMailer for instance), without worrying about keeping the MIME boundaries intact.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 1
    Using your code, I was able to make slight modifications to get a "proof of concept" script working with my existing code. This definitely help put me in the right direction. Thank you! – Michael Irigoyen Jan 11 '11 at 17:46
  • That is quite alot of code just for forwarding an email, Im sure it can be lots smaller. :) Though I dont use the imap functions i only use mail. I know the downside of my decision but I sending as a bot and not me. :) – JamesM-SiteGen Jan 18 '11 at 08:17
  • @JamesM: This isn't neither an email sender nor an email fowarder, it's a parser. Regarding the size: maybe... Would you like to give a shot? – Alix Axel Jan 18 '11 at 11:18
  • that seems to have a lot of magic constants like "html" and "text", it seems unlikely that it will accurately copy all emails. – Jasen Oct 10 '14 at 02:15
  • @Jasen it's just parsing either multipart or alternative/plain emails. The magic stuff comed from the protocol itself. – Alix Axel Oct 10 '14 at 07:20
4

This isn't really an answer, but a suggestion for an alternate method. I think it would be much simpler and less prone to error (i.e., no delivery issues) if you simply moved the messages around to different folders within the existing account. I.e., the cron runs and processes all emails in INBOX. If if finds STOP, it does the required work and then (via IMAP functions) simply moves the email to a subfolder named "Processed" or similar. Otherwise, it moves the email to a subfolder named "Check These" or similar. Then you don't need to worry about forwarding, or further deliveries, or a second account, and everyone can monitor the processed, unprocessed, and pending mails directly.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • 1
    We actually already do this in the script. We have an "unsubscribed" folder for those STOP messages and a "processed" folder for everything else. However, we need to forward as most of the people on the receiving end don't (and won't) know the password to this email account. (There are a handful of student help that we don't want to have access to this account but receive the forwards.) – Michael Irigoyen Dec 21 '10 at 21:31
1

Have you taken a look at functionality using Swiftmailer library ?

http://swiftmailer.org/

I have used this in the past and have gotten good result, altho not in an application like you have described, I have however utilized it for PHP based 'mailing lists' where I checked for subject and sent to proper group.

But I created a new message, did not forward. Hope that helps.

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • Swiftmailer is half-arsed it doesn't hadle nested multiparts in the general case. so it cant't be used to resend emails of unknown origin. – Jasen Oct 10 '14 at 01:50
1

This has happened to me before. In order to fix it I had to do a imap_base64() on the body of the email after I used imap_fetchbody().

$body = imap_fetchbody($imap, 1, 1);
$headers = imap_headerinfo($imap, 1);
$body = imap_base64($body);
jb1785
  • 722
  • 1
  • 7
  • 19
  • yeah, imap decodes the body, so you should check if the original was encoded and re-code it. thios whole php extension is just bindings for "c-client" and most for the documenatation for c-client is applicable. – Jasen Oct 10 '14 at 01:45
0

use an IO handler to capture the content of the email as a string, split out the headers and then use the php 'mail()' function to send it.

Else if you really want to do this with php-imap,

the php-imap extension is libc-client which is part of the pine email client software figure out the manual steps needed to do it using pine and then look at the c-client calls pine makes to do it. that will give you the steps needed in php.

the c-client documentation is fairly minimal, going to the pine source is the best way to get usage details.

I think you may find that the author of the php extension has "for your convenience or protection" omitted or changed stuff that blocks this path.

Jasen
  • 11,837
  • 2
  • 30
  • 48