How to pipe an incoming email to a php script?

I would really appreciate if anyone could help me with this? I just killed the whole day today trying to solve this (and understand the cumbersome CP that Dreamhost has.) I also tried contacting their tech support that were less than helpful and just gave me this link.

OK, so here’s my task. I want to get my php script to be called (as a web callback) for each incoming email to my inbox, with the body, or at least subject line of such email. Simple, right? Well, I guess not …

Here’s what I’ve done:

  • I created an email account, say, notify@mydomain.com (obviously via Dreamhost CP.)

  • I then added a filter to that email account to forward it to my main shell account, let’s call it myacct1 (screenshot):
    https://i.imgur.com/86YtRqk.png

  • I then added a text fille .forward.postfix to /home/myacct1/mydomain.com directory that contains this:

      | /usr/bin/php /home/myacct1/mydomain.com/php/cron_eml.php
    
  • And created the cron_eml.php file that had this:

    #!/usr/bin/php -q
    <?
    $kroot = dirname(__FILE__);
    chdir($kroot);
    $_SERVER['DOCUMENT_ROOT'] = dirname($kroot);
    
    //Need this just to test that my PHP script is invoked
    mail("account@mydomain.com",
      "Test email",
      "This is a test\r\n",
      "From: TEST <account@mydomain.com>");
    ?>
    
  • I then tried sending an email to notify@mydomain.com and even though I can see that the email arrives, my php script doesn’t get called …

Couple observations:

  1. When I send email, it appears in the “/home/myacct1/Maildir/new” folder.
  2. If I just invoke my php script via the web browser, as mydomain.com/php/cron_eml.php it works fine and sends out my test email.
  3. All references in the Dreamhost documentation of what I’m trying to do seem to have non-working links. Here’s one example. But I also found this post, that I’m basing my solution on.
  4. Is there a way to see an error log or something that can tell me what am I doing wrong here?

This all is very frustrating! So any help would be appreciated.

Possible you need quotes around your forwarding pipe? I think the quotes are required if the command has 1 or more arguments.

Here’s a simple test of .forward piping I did with my Shared-Hosting account. Note how sending mail caused the testfile to be created:

$ cat .forward
"|touch /home/myuser/testfile"

$ ls -al .forward
-rw-rw-r-- 41 Mar 15 .forward

$ ls -l testfile
ls: cannot access 'testfile': No such file or directory

$ mail myuser
Subject: test
test
.
Cc: 

$ ls -l testfile
-rw------- 0 Mar 15 testfile

Oh, I didn’t think about quotes. Thanks, I’ll try it when I get home.

Quick question though, do I need to call that file .forward or .forward.postfix?

I think either name can be used. It looks like .forward.postfix is a DH specific form (probably for historical reasons), so I’d recommend using the plain .forward for forward compatibility.

The names are configured in DH’s Postfix main.cf:

$ grep -R forward /etc/postfix/
/etc/postfix/main.cf:# no .forward, instead .forward.postfix
/etc/postfix/main.cf:forward_path = $home/.forward, $home/.forward.postfix

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.