How to pipe an email to a PHP ?

In some cases you might need to redirect a given mailbox to a PHP script to handle the incoming messages. This can be easily done with cPanel -> Forwarders tool.

To set up pipe forwarding, you can follow the steps below:

- Log in to your cPanel;

- Click on the Forwarders icon, under the Mail section;

- Click on the Add Forwarder button;

- Fill in Address to Forward and put the mail address you would like to pipe the messages from.

- Click on the Advanced options link and select Pipe to a Program. Then fill in the full path to the script which will handle the messages. Bear in mind that the path is relative to your home directory, so if you create a script/file called pipescript.php placed inside your home/ folder you should fill in only pipescript.php inside the Pipe to a Program field. If the script is situated inside the public_html/ folder you should fill in public_html/pipescript.php

There are several important things you should check regarding the PHP script which is handling the email messages:

- Ensure the very first line of the script is a hashbang (also called shebang). This is a special line which identifies the file as a PHP script. In most cases it should look like this:

#!/usr/bin/php -q

- Make sure that there are no whitespaces or blank lines before the above line as this will be sent to the mail server, which will result in a bounced message. The –q option instructs PHP not to print its version either, since this will also result in a bounced message.

- Make sure that the script permissions are set correctly. In most cases, you would simply need tochange the permissions, either via your cPanel -> File Manager or through an FTP client and set them to 755. This will make the script executable.

Below you can find a sample PHP script which handles piped messages. The script will read the input from the STDIN and then save the full message into a file called pipemail.txt (make sure you provide the full path to the pipemail.txt file).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/php -q
<?
/* Read the message from STDIN */
$fd=fopen("php://stdin","r");
$email="";// This will be the variable holding the data.
while(!feof($fd)){
$email.=fread($fd,1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw=fopen("/home/user/pipemail.txt","w+");
fwrite($fdw,$email);
fclose($fdw);
/* Script End */
?>




where the user from the 
/home/user/pipemail.txt line is your cPanel username.
  • 0 Uživatelům pomohlo
Byla tato odpověď nápomocná?

Související články

Differences between IMAP, POP3 and SMTP

SMTP is an abbreviation that stands for Simple Mail Transfer Protocol. This protocol allows...

Why email server seems to be blacklisted?

If you receive a bounced-back message saying that IP address X.X.X.X is blacklisted by a spam...

How to add image signature to your RoundCube Webmail Client?

In most cases, it is preferable to add your signature in a local mail client such as Mozilla...

How to change Sent, Drafts and Deleted mail folders for an iPhone device?

In order to reconfigure an iPhone to keep the sent, drafts or deleted messages on the mail...

How to create auto-responder in cPanel ?

cPanel Auto Responder feature allows you to automatically send response messages in reply to...