Automatically Process New Subscribers That Join Your List? Here’s How.

And how you can use email piping for that

Have you ever needed to automatically process new subscribers that join your list(s)? I have.

If you don’t own an autoresponder service, but you are technically savvy, you can do that.

Now let’s explain a bit how things work for Adrian’s Hub.

As you might’ve noticed from the menu, we have a Private Hub for back-end purposes. But there isn’t a direct way to join the Private Hub (and that’s how I want it).

Here’s how things work in our case: subscribe to Adrian’s Hub newsletter, then you can have access to your own Private Hub back-end by (re)setting your password.

So, for this particular case (and others), we need to be able to automatically process each new subscriber to our list.

There are two ways to do that:
1. If your autoresponder service has an application programming interface (API) and allows callback functions to be attached to various events (including new subscriber signing up), then that’s the way to do it. It works in real-time and should be secure. You might as well be interested in events like subscriber opening an email, subscriber updating his or her information (name, email etc.), subscriber opting out from a campaign.

2. But what if your autoresponder service doesn’t have that? Well, there are two options here: either switch to one that has or… process new subscriber email alerts you receive from your autoresponder.

Now, let’s elaborate on the latter, because that’s what I did.

The way to process incoming emails, when your site is hosted on a Linux server is this:

1. You need to write a script that will analyze every incoming email to the email address where you receive the notifications from your autoresponder service. The new subscriber email alerts have a standard structure, so you simply need to parse the emails and look for what interests you.

2. Make sure your script doesn’t display anything on the standard output (any errors or other messages you need to send yourself, log them to a file) or the email will bounce back to the sender with a “delivery failed” error (even if it was delivered successfully!). On a shared hosting, that’s all you can do usually. On your own server, you can edit the exim configuration file (typically found here: /etc/exim.conf) and replace both occurrences of return_output with return_fail_output. That will prevent email bouncing back to sender if your script displays something (not if delivery really fails, though). In php, you can also use ob_start to direct any output to a buffer instead of the screen, which you then clean and ignore by using ob_end_clean.

3. Your script needs to start with a hashbang (before <?php !). For a php script, your hashbang should look like this: #!/usr/bin/php -q. You can see the path to the php installation in your cPanel. -q instructs php to run quietly. Make sure there are no blanks between the hashbang and your script beginning (<?php). They are displayed and sender receives the “delivery failed” bounce-back.

4. Make your script executable by changing its permissions. In my case, I set the permissions to 0755.

5. In your cPanel, you need to add a new email forwarder (Mail -> Forwarders in my cPanel X). Set the email you wish to monitor for incoming emails and choose, from the advanced options, to pipe to a program. Make sure you put in the full path to the script which actually does the processing. Depending on your cPanel version, your “home” (typically /home/username/) path might be added by default, and you only need to add the relative path from there to your script. Then you add the forwarder.

6. Now all your emails coming to the address you’ve set above will be piped to your script. In your script, you need to read from STDIN to get each incoming email. In php, you can use something like that:
// read email using stdin
$fd = fopen("php://stdin", "r");
$incoming_email = "";
while (!feof($fd)) {
$incoming_email .= fread($fd, 1024);
}
fclose($fd);

7. You now have each incoming email. It’s time to parse it to get and process the information you are interested in (make sure the sender, email type and format are what you expect).

I hope this article will be useful to some. You can use email piping for other purposes as well. And I had to do a lot of digging until everything worked as expected. Might save you of some of this trouble.

Related Articles

Tips & Tricks

Web Development

 
About Adrian Gurgui

Creator of AdriansHub.com, I enjoy working from home and having a flexible schedule. My mission: to grow myself, then teach and help others to step up and inspire them to offer help at their turn.

2 Comments on Automatically Process New Subscribers That Join Your List? Here’s How.

  1. Patsy j Payne - konnektions konsultant // October 3, 2015 at 12:11 pm //

    Thank U Adrian
    Although I do subscribe to an autoresponder, this article will help me get going with that
    My next task … on my google calendar

  2. Appreciate your comment, Patsy! I’m glad you have found the article useful.

Leave a comment

Your email address will not be published.


*


CommentLuv badge