Using puppet realm switch to select between beta/prod ( Wikimedia clusters )

Since the BounceHandler extension is currently installed only in the beta clusters ( Official testing servers of Wikimedia- deployment.wikimedia.beta.wmflabs.org ), writing a custom router in the exim configs of operations/puppet ( configuration repo managed by puppet ) to collect in all the bounce emails and HTTP POST to the extension API seemed risky. This was due to the fact that operations/puppet is meant for production and something beta specific in it, that too continuous API requests dont look sane ( we cant estimate the traffic yet ).
Marius Hoch ( WMF ) came with the idea of an exim-realm switch, which seemed to solve the issue. You can switch variables between beta and production by:
We edited

$ sudo <editor> templates/exim/manifests/mail.pp

and added this before the inclusion of exim4.conf.SMTP_IMAP_MM.erb.

# config - labs vs. production 
case $::realm {
    'labs' : {
	$verp_post_connect_server = 'deployment.wikimedia.beta.wmflabs.org'
	$verp_bounce_post_url = 'http://deployment.wikimedia.beta.wmflabs.org/w/api.php'
    }
    'production': {
	$verp_post_connect_server = 'appservers.svc."${::mw_primary}".wmnet'
	$verp_bounce_post_url = 'http://meta.wikimedia.org/w/api.php'
    }
    default: {
	fail('unknown realm, should be labs or production')
    }
}

The internal networks selection was done to make sure the bounce gets POSTed to the right wiki. Now, this configuration can be used in exim4.conf.SMTP_IMAP_MM.erb by editing the bouncehandler router by:

command = /usr/bin/curl -H 'Host: <%= @verp_post_connect_server %>' <%= @verp_bounce_post_url %> -d "action=bouncehandler" --data-urlencode "email@-"

Hope it helps someone in beta 🙂

Redirecting incoming emails to a PHP script using curl

While working with the bounce handling project for Mediawiki, we met with a challenge to get the bounce-emails redirected directly to a PHP script, which would accept the email as a $_REQUEST['email'] variable so that processing is easy and direct. The other version includes storing the emails using an IMAP client such as dovecot, and later retrieving the mails one by one using the PHP script.
* courtesy [ http://serverfault.com/questions/414148/redirect-all-incoming-mail-into-a-script ].

Assumptions:
* MTA running exim4 on the receiving end.
* We are redirecting mails to a router system_aliases and the transport is use_pipe
* PHP script located at http://localhost/mail.php
* The PHP script accepts accepts a $_REQUEST['email'] variable,
* The working of the script can be verified by writing the contents of $_REQUEST[['email'] to a sample.txt withing the PHP script.

Problem:
Redirect the bounce emails to a PHP script using curl

Solution:
* Edit the system_aliases router:
$ sudo nano /etc/exim4/exim4.conf

system_aliases:
debug_print = "R: system_aliases for $local_part@$domain"
driver = accept
transport = use_pipe

and edit the use_pipe transport to have:

use_pipe:
debug_print = " Using the pipe transport"
driver = pipe
command = /usr/bin/curl http://localhost/mail.php --data-urlencode "mail@-"
user = nobody
group = nogroup 
return_path_add
delivery_date_add
envelope_to_add

This will make sure that the receiving email reach the required $_REQUEST['POST'] in mail.php
Yay. Done.
PS:- If you want to test whether the email reached the PHP script completely, consider giving in something like this :-

$email = $_REQUEST['email'];
$fh = fopen("sample.txt","a+");
fputs($fh,$text);

Now that would put the contents of the email to a file sample.txt in the same folder. Happy Hacking!