Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3664856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:41:38+00:00 2026-05-19T01:41:38+00:00

We have a cron’ed PHP script that checks an inbox every ten minutes. The

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:41:39+00:00Added an answer on May 19, 2026 at 1:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cron job running searching Twitter every 5 minutes, each 5 minutes
Assume I have a CI app that does the following: Allow users to log
I use django in project. I have many cron jobs which operate with database.
I have a few scripts that need to run concurrently as separate processes. My
I have a scenario where one PHP process is writing a file about 3
I have a daily cron task which automatically unrars a rar file and processes
I don't think this is an exact duplicate of other questions, but feel free
Have been pulling out my hair trying to find out why my sessions are
So I have been reading all i can for the last 2 hours and
here is a link to how my APC is running : [removed] As you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.