RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Piping Email in cPanel, and the Marketing or Finance Applications

Piping Email in cPanel, and the Marketing or Finance Applications

The cPanel software that most web hosts provide to their clients (this includes BeliefMedia) makes email management easy. Along with the typical features you're most likely familiar with - such as email creation, autoresponders, and forwarding - these's another option hidden away in the 'Advanced Options' tab that will 'Pipe to a Program'. This Piping routes an incoming email to hosted software that manages the message in some way. It's this piping that is the focus of this article.

Piping Options in cPanel

Piping Options in cPanel (Advanced Options)

Why Pipe email?

There are any number of reasons why one might Pipe their email to a PHP or other script. Consider the following limited examples:

  • Handle unrouted email. cPanel already provides a feature to send unrouted email to an email address, email account, or the system account, so this example example is a little redundant. However, we can personalise the experience by capturing those details of incoming emails, save those details to a database (and maybe even add the email to a general email marketing database), and then send a structured and potentially converting email as your 'Email Unavailable' reply (think of it as a custom "404 Not Found" reply for email).
  • Email to SMS List Features. Since we disabled our Outlook to SMS tools we're compelled to introduce easier and more efficient ways for our clients to send group texts (rather than logging into their self-hosted Platform and performing the task caveman-style). Piping permits this by evaluating the complex 'to' field and then, if an instruction is defined, we will schedule an SMS to all of the recipients in that particular list.
  • SMS Notifications. If you require notifications when a particular email is received, querying the subject line or sender could immediately trigger an SMS alert.
  • Manage Unused Domains. We have a large number of domain names - some of which were previously owned and some of which are generic enough that they're confused with another organisation (one of our domains receives thousands of weekly emails by virtue of the similarity to a Government department). We're somewhat obligated to notify those people that their email was sent into a black hole, and a reply is an appropriate means of communicating this message. However, rather than the generic reply cPanel provides via its 'Failure Message' options, a potential opportunity exists to introduce your business by way of a message that might spark a little interest. Keeping a database table populated with all inbound emails would prevent the message sent twice, and would also prevent that infinite loop should the sender also be providing a generic response.
  • Email Competition Registrations. For our clients, a competition or other similar venture might be served by having clients send an email with nothing other than their phone number in the subject line. Any message that you would otherwise disqualify (say, an empty or invalid subject) might trigger an email sending the user to a landing page with online options, while others would be subscribed to a particular competition and associated email campaign. Options and funnel-style flows are entirely customisable. This is obviously just one example; the same user might be able to subscribe via text message in the same way.
  • Finance Applications. It's the finance applications that we're currently working on. Based on the information within, say, an ApplyOnline email or similar, we're able to parse the message, find identifying information, and then trigger other actions... even if it's just adding a note to aggregator or internal systems, or creating an Outlook task based on the nature of the message. Pictured: Apply Online emails. Any email from xyz@applyonline.com.au might be evaluated for various tasks. Some systems, such as SalesTrekker, negate the need for this email-level intervention by way of webhooks.

     

    Apply Online

    Since the ApplyOnline notification emails follow a predictable pattern they can easily be parsed for data and then sent to a database for compliance keeping purposes. Email can often be a difficult medium for which to operate when looking for important information so it makes sense to make it fully indexed for permanency. This is just one of the features we plan to implement soon.

  • Support Desks. One might choose to evaluate all incoming email for a support code or similar in a subject field that identifies the message as best served by a particular team, certain software, or an individual.

PHP Code

If you'd like to have a play with the feature yourself, upload a PHP file to your server with the following code. It will simply dump the content of the email into a text file.

1
<?php 
2
#!/usr/bin/php -q
3
<?php
4
$fd = fopen("php://stdin", "r");
5
$message = '';
6
while (!feof($fd)) {
7
  $message .= fread( $fd, 204800 );
8
}
9
fclose($fd);
10
$name = time() . '-' . rand(1, 1000) . '.txt';
11
file_put_contents ('emails/' . $name, $message);

Note the first line is a hashbang (or shebang) instruction; no spaces should come before or after that line and your opening PHP tags.

Once your script location is defined in cPanel you'll start to see all your emails as text files in your emails folder. The below image gives you an idea of the frequency of genuine yet incorrectly addressed emails to just one of our dormant domains.

Unrouted Emails

To retrieve the primary email attributes, consider the following:

1
<?php 
2
     for ($i = 0; $i < count($lines); $i++) {
3
 
4
    /* Subject */
5
    if (preg_match("/^Subject: (.*)/", stripcslashes($lines[$i]), $matches)) {
6
 
7
      /* Decode MIME header elements */
8
      $subject = imap_mime_header_decode($matches[1]);
9
      $subject = $subject[0]->text;
10
    }
11
 
12
    /* From Email */
13
    if (preg_match('/^From:\s"?(.*)"?\s?<(.*)>/', stripcslashes($lines[$i]), $from_matches)) {
14
      $from_name = $from_matches[1];
15
      $from_email = $from_matches[2];
16
 
17
      /* Decode MIME header elements */
18
      $from_name = imap_mime_header_decode($from_matches[1]);
19
      $from_name = $from_name[0]->text;
20
 
21
      $remove = array('From: ', '"'); $replace = array('', '');
22
      $from_name = str_replace($remove, $replace, $from_name);
23
 
24
    }
25
 
26
    /* To Email */
27
    if (preg_match("/^To: \<?(.*)\>?/", $lines[$i])) {
28
      preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $lines[$i], $to_matches);
29
      $to = $to_matches[0][0];
30
    }
31
 
32
    /* Date */
33
    if (preg_match("/^Date:\s(.*)/", $lines[$i], $matches)) {
34
      $date = $matches[1];
35
    }
36
 
37
    /* Message-ID */
38
    if (preg_match("/^Message-ID:\s<(.*)>/", $lines[$i], $matches)) {
39
      $msgid = $matches[1];
40
    }
41
 
42
    /* X-Mailer */
43
    if (preg_match("/^X-Mailer:\s(.*)/", $lines[$i], $matches)) {
44
      $mailer = $matches[1];
45
    }
46
 
47
    /* Reply-To: */
48
    if (preg_match('/^Reply-To:\s"?(.*)"?\s?<(.*)>/', stripcslashes($lines[$i]), $matches)) {
49
      $replyto = $matches[1];
50
 
51
      /* Decode MIME header elements */
52
      $replyto = imap_mime_header_decode($matches[1]);
53
      $replyto = $replyto[0]->text;
54
    }
55
 
56
}

The above code is rather inefficient because it iterates through each line with multiple expressions. You might consider evaluating the entire text for an occurrence of the email field you're looking for and, if present, perform the expression. Or you might simply perform all expressions on the entire email string.

Perform an expression on each field that you would like stored in your database. In reality you won't save anything to a text file; once you're done with the data and performed any necessary action the email will makes its way into a big digital black hole.

The Problem

The problem as it relates to our clients is that we're asking that they host their primary email with Microsoft so that they might benefit from the countless features we're integrating with our systems. However, Microsoft does provide excellent webhooks that permits us to query or poll data when it's required. That said, many of our clients still have a secondary company email domain that they host with us for non-client purposes. Either way, we'll ensure that they have a solution for whatever systems we introduce.

Conclusion

A marketing company is compelled to do whatever it can at every touchpoint to optimise and convert in any way in which it can, and the examples provided on this page are a good example of where technology drives solutions when no generic product ever could. At the risk of sounding like I'm giving ourselves an out-of-place plug, our highly customised technology is just one reason why our finance clients enjoy industry-leading results.

Keep any eye out for other cPanel, and cPanel API related articles Tag: cpanel.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment