I have followed this answer Pipe all Postfix email for a domain to PHP script - Centos7/EC2 to direct incoming mail for a specific address to a PHP script.

My aliases file entry looks like:

mail-incoming-php: "/usr/bin/php -q /var/spool/mail/php/incoming.php"

And the Postfix virtual file maps the e-mail address to


My current test PHP script is set to create an empty file, which I have tested. When sending mail to the address in question the PHP script does not create a file, and the maillog contains this error:

cannot append message to file /usr/bin/php -q /var/spool/mail/php/incoming.php: cannot create file exclusively: No such file or directory

I have temporarily set the PHP script's permission to 777 and get the same error.

Maybe this is not the best way to solve this problem or something else needs doing?

All I need the script to do is read the contents of the incoming e-mail, the e-mail doesn't have to be relayed after this.


Unlike in the linked question, you are not piping the mail to the script, i.e.

mail-incoming-php: "| /usr/bin/php -q /var/spool/mail/php/incoming.php"

Because you are missing the | you are trying to write (append) to a file, instead:

  • /usr/bin/php -q /var/spool/mail/php/incoming.php is an invalid path.

  • /usr/bin/php would be appending the message to the PHP binary.
    (Luckily there's no permission for that!)

  • /var/spool/mail/php/incoming.php would be appending to the script.

Also, you shouldn't have the script globally writable 777, but (at most) 755.

  • I had tried it like that initially, I have put it back and indeed maillog looks better e.g delivered to command: /usr/bin/php -q /var/spool/mail/php/incoming.php... but still the PHP file has not created the file. Is there some way I can log the execution of the PHP script?
    – Appalling
    Jun 19 '20 at 12:18
  • Don't worry, it was the parent directory permissions not being correct. Do you know what user will be running the PHP script?
    – Appalling
    Jun 19 '20 at 12:41
  • By default it's run as user nobody, but you can change it. See e.g. serverfault.com/a/453438/274176 Jun 19 '20 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.