Add an email address that forwards to a script

I previously wrote a post aboutĀ receiving e-mail and saving attachments with PHP. I was so focused on having solved that part of my problem that I neglected to write about how to send e-mail to a script in the first place. Thanks to a helpful commenter, today I am rectifying that oversight.

Using CPanel

Here’s how I set up an e-mail address to be sent to a PHP script in CPanel.

First, go to the Forwarders section…

Add an E-mail forwarder

Click the “Add Forwarder” button…

Add a new forwarder

Finally, enter your desired e-mail address and the name of the script you wish to have process your e-mails.

Enter the forwarder data

Not Using CPanel

ThisĀ  article over at Evolt.org looks like it has good instructions on setting up Sendmail, Qmail or Exim to forward to a PHP script. I have not tested those instructions so I don’t know that they work for sure, but the article seems well written so I would give them a try.

This entry was posted in Computers and tagged , , . Bookmark the permalink.

4 Responses to Add an email address that forwards to a script

  1. Pingback: Recieve E-mail and Save Attachments with a PHP script | Stuporglue.org

  2. Sofus says:

    Hi Stuporglue. I was thrilled to see your script on saving emails to mysql and attachments to a local folder. But i cant use any of the Cpanel, Exim etc. I am trying to work solutions that work on all servers. So i was wondering if we cant use this script to show the list of email (MBOX) in then redirect them to your script:

    First I have a connect to mailserver file: mbox_connect.php:

    And then this line of code to echo a table with my emails.

    Nmsgs ."\n\n";
    $msgCount = $hdr->Nmsgs;
    } else {
    echo "failed";
    }
    $MN=$msgCount;
    $overview=imap_fetch_overview($connection,"1:$MN",0);
    $size=sizeof($overview);

    echo "";

    for($i=$size-1;$i>=0;$i--){
    $val=$overview[$i];
    $msg=$val->msgno;
    $from=$val->from;
    $date=$val->date;
    $subj=$val->subject;
    $seen=$val->seen;

    $from = ereg_replace("\"","",$from);

    // MAKE DANISH DATE DISPLAY
    list($dayName,$day,$month,$year,$time) = split(" ",$date);
    $time = substr($time,0,5);
    $date = $day ." ". $month ." ". $year . " ". $time;

    if ($bgColor == "#F0F0F0") {
    $bgColor = "#FFFFFF";
    } else {
    $bgColor = "#F0F0F0";
    }

    if (strlen($subj) > 60) {
    $subj = substr($subj,0,59) ."...";
    }

    echo "$from$subj
    $date\n";
    }
    echo "";
    imap_close($connection);
    ?>

    Can you see some how that the two scripts could go together. With a final cronjob or similar script to execute inserting the email to the db and saving the file to a folder.
    Like your blog! It covers a lot.

    Kind Regards Sofus

    • stuporglue says:

      Thanks for stopping by!

      I’m afraid I’m not familiar enough with how MBOX files store the data internally to give you a solid answer, but here’s my guesses on a good approach:

      1) If MBOX stores the raw email message, an MBOX to database script could connect to the MBOX and for each message just pipe the message to my email handling script (or one similar). The email handling script takes the data in from STDIN, so this should work.

      2) Based on the code you pasted, it looks like the MBOX is storing a pre-parsed email message. If this is the case, their parsing is probably much more robust than the code I wrote. I would recommend writing new code to handle those pre-parsed fields since you’re less likely to get an email you can’t handle that way.

      Best of luck!

      And if you do end up implementing it, post the code somewhere and I’d be happy to link to it.

    • Rick Tap says:

      Hi,

      I know I’m late to the party, but as I stumpled upon this, others might as well. The best way to solve Sofus’ Problem would be to filter directly the smtp traffic.

      For postfix that means adding a filter in the master.cnf that redirects the messages to your script. in the main.cnf you point to an adress database, that tells postfix which addresses to filter.

      Anyone beeing interested in that, should check out:
      http://blog.thecodingmachine.com/content/triggering-php-script-when-your-postfix-server-receives-mail

      So long.

Leave a Reply

Your email address will not be published. Required fields are marked *