cPanel : Pipe email script to extract attachments

If you want to automatically process images or other attachments that are sent to a mailbox address in your cPanel account, then you need look no further than the Pipe to Program option in the cPanel Forwarders screen.

This is an often overlooked feature of cPanel accounts, probably because it’s not that easy to understand from a novice’s point of view. However, I’m going to do my best to explain how useful this feature is, and give an example of how you can pipe email using Perl.

What is piping anyway?

We’re not talking about the pretty decorations around a cake! In computer speak a pipe is a method of passing the standard output (stdout) from one process as the standard input (sdtin) to another. In Linux pipes allow many commands to be stuck together to achieve quite powerful results (or disasters!).

cPanel allows you to set up email forwarders that, instead of sending the email message onto another email address, pipe email straight to a program, or script. The script itself must be located somewhere in your home directory, but could be written in any suitable scripting language (PHP, Perl, Ruby, Bash etc.) or could be a compiled executable. It’s up to you!

How to configure the pipe

For this example, lets assume that we have a CTTV system that sends an email with an attachment every time it senses movement. We have configured the system to sent the email to cctv@endlessgeek.com. Now, we don’t actually need this mailbox to exist – as long as the forwarder is there, the mail system will accept the messages.

To get started, log into cPanel and open the Forwarder screen from the Mail section.

cPanel Pipe to Program

On the next screen, click the Add Forwarder button (NOT the Add Domain Forwarder button).

On the Add a New Forwarder screen, fill in the Address to Forward: with the address from which you want to pipe email. Click the Advanced Options link to reveal the Pipe to a Program option button, which you should click and then enter the path and name of your script in the box provided. In this case our script will be placed in the top level of our home directory and will be a Perl script called cctv.pl

Setting up the Pipe

Click the Add Forwarder button, and you’re done! You should see something like this in your Forwarders now:

Pipe configured

Now for the script!

Perl script to strip attachments from MIME message

Excuse the verbose title – search engines, you know. OK, so this perl script should run fine on any cPanel server v11.40 and later. Time::Piece is a core module and should already be installed. However, MIME::Parser is not, so open a terminal session via SSH, and type in:

perl -MMIME::Parser -e ‘exit;’

If the module is installed, you won’t get any message at all. If it isn’t you will get an error message, and will need to ask your host to install the module for you. Many cPanel hosts allow you to install your own Perl modules via the Perl Modules icon in Software/Services:

Install a cPanel Perl Module

Once you are happy that MIME::Parser is installed, then you can copy and paste this script and save it to your home directory, using the name you gave when setting up the piped forwarder above. Don’t forget to make sure that you set the permissions for this file to chmod 755 otherwise it isn’t going to be able to do its magic.

Not a lot more to say really – if all is well you should be able to email a message containing an image attachment. Once the message is received by your server it will be piped to your script and you should find the event in the logfile extracted.log and the image in the extracted directory 🙂

,

2 Responses to cPanel : Pipe email script to extract attachments

  1. Andre November 11, 2014 at 14:19 #

    Hello, im trying to get this script working, but i ran into a problem. When i try to send mail to the forwarded email i get mail delivery failed return message that says :

    cagefs_enter: /home/xxxx/mailfwd.pl: No such file or directory

    The autofiller in cpanel forward finds the file.
    Logfile is not created.

    Any suggestions ?

    • steve December 4, 2014 at 12:22 #

      Interesting – I use this script on a server that also runs CloudLinux/CageFS so I’m unsure what your error relates to… have you tried running

      cagefsctl –force-update

      on this server?

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.