Forwarding incoming e-mail messages to a script file
cPanel allows you to configure e‑mail forwarders not only to other addresses but also to pipe messages directly into a script. This is useful for automating tasks such as help desk ticket creation, mailing list management, or custom processing.
How It Works
Section titled “How It Works”-
Instead of delivering mail to a mailbox, cPanel can pipe the message into a script.
-
The script runs on the server and processes the message in real time.
-
Common use cases:
- Help desk systems (e.g., automatically creating support tickets).
- Mailing list managers.
- Custom automation (e.g., logging, parsing attachments).
Steps to Forward Mail to a Script
Section titled “Steps to Forward Mail to a Script”-
Log in to cPanel.
-
On the Tools page, go to Email → Forwarders.
-
Under Create an Email Account Forwarder, click Add Forwarder.
-
Enter the address to forward (e.g.,
support@example.com). -
Under Destination, select Pipe to a Program.
-
Enter the relative path to your script file.
-
Example:
home/username/public_html/scripts/parse_mail.php -
Do not include leading slashes (
/).
-
-
Click Add Forwarder.
Script Requirements
Section titled “Script Requirements”- The script must be executable by the server.
- It should read the incoming message from standard input (stdin).
- Example in PHP:
#!/usr/bin/php -q<?php// Read email from stdin$fd = fopen("php://stdin", "r");$email = "";while (!feof($fd)) { $email .= fread($fd, 1024);}fclose($fd);
// Process the email (example: log to file)file_put_contents("/home/username/mail_log.txt", $email, FILE_APPEND);?>- Ensure the script has correct permissions (usually
755). - Place the script in a secure directory (outside public_html if possible).