Frage

Wir haben einen cron'ed PHP-Skript, dass die Kontrollen eine Inbox alle zehn Minuten. Der Zweck dieses Skripts ist „STOP zu beenden“ Funktionalität für unseren SMS-Benachrichtigungsdienst bieten wir zu handhaben. Wenn das Skript am Anfang der E-Mail alle E-Mails mit dem Wort „STOP“ findet, wir den Benutzer unserer Benachrichtigung Datenbank entfernen.

unsere Basen decken, würden wir alle E-Mails gerne, die an einer anderen E-Mail-Adresse weitergeleitet nicht die oben genannten Kriterien erfüllen, wird (die ein Alias ??ist), dass mehrere Personen empfangen und stündlich überprüfen. Aber wir in Probleme laufen die E-Mails von dieser PHP-Skript weitergeleitet werden.

Zu wissen, wie die mail Funktion von PHP funktioniert, es ist ziemlich offensichtlich, wir müssen die Header vor dem Absenden wieder einzusetzen. Allerdings immer MIME Multipart-E-Mails als garble Text zugesandt bekommen, einschließlich der Barrieren und alle Base64-codierte Anhänge.

Kennt jemand eine einfache Möglichkeit, eine E-Mail zu übernehmen und leitet es auf richtig ein PHP-Skript verwenden?

Wir verwenden die nativen IMAP-Funktionen eingebaut, um PHP 5. Wir haben auch Zugriff auf das Modul PEAR Post haben. Aber wir haben es nicht gelungen, irgendwelche Beispiele oder Personen, die ähnliche Aufgaben zu finden, die Google Suche.

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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.

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);

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top