Sometimes a mail server can be of a lot of value if we know how to handle it properly. In today's article we will see how to use our MTA to automatically execute actions when receiving a specific email or with the parameters that define it. Once we know the theory raised two case studies as examples.
To begin we will need a Linux server with qmail as MTA . I guess any other mail server will ( Sendmail , Postfix ), I customize in Qmail because it is the one I know and use, but I am sure that others can do the same.
The theory
To understand how the idea we must first understand how Qmail delivers messages in local mailboxes. It is a matter on which there is plenty of literature searching on Google but can not be very clear on a quick reading. This is the famous mess of files . qmail .
Each time you create a system user must be created, if you receive email, a. Qmail-default in your user directory.
- # cat /home/jiglesias/.qmail-default [Jiglesias Lerez @ ~] # cat / home / jiglesias / .qmail-default
- . / Maildir /
Normally this file is automatically created when you install qmail as it will have created a copy of this file to create user skeleton / etc / skel:
- # ls -la /etc/skel [Jiglesias Lerez @ ~] # ls-la / etc / skel
- 56 total
- Jul 29 18 : 29 . drwxr-xr-x 4 root root 4096 July 29, 1918: 29.
- Dec 30 04 : 08 .. drwxr-xr-x 97 root root 12 288 Dec 30 04: 08 ..
- Feb 1 2008 .bash_logout -Rw-r-r-1 root root 33 February 1, 2008. Bash_logout
- Feb 1 2008 .bash_profile -Rw-r-r-1 root root 176 February 1, 2008. Bash_profile
- Feb 1 2008 .bashrc -Rw-r-r-1 root root 124 February 1, 2008. Bashrc
- Aug 29 2007 Maildir drwx - 5 root root 4096 Aug 29 2007 Maildir
- Jan 2 2008 .qmail-default -Rw-r-r-1 root root 12 Jan 2 2008. Qmail-default
- Sep 12 2006 .zshrc -Rw-r-r-1 root root 658 Sep 12 2006. Zshrc script
For those who do not know, the skeleton files are copied to the user directory (with permissions of it) every time you create a new one. If you want all your users have a file automatically, this is your site. In our case we see that addition. Qmail-default Maildir directory is the default delivery of mail in qmail. Taking a user this file and this directory, you can receive mail.
Okay okay, going too fast. You have not told us why the. Qmail-default that. Right. The. Qmail rules indicate delivery of messages based on two parameters:
- The name of the file. Qmail-xx refers to the mail account on which it acts.
- The manual provides information on what to do with the mail.
Suppose a user (jiglesias) that receives mail from two separate accounts (jiglesias osus @ @ ... and ...).
By default all your mail will come to your mailbox as it is indicating the file. Qmail-default. We now that the behavior is different depending on the account to which it is addressed, then the files created. Qmail for directions:
- . Qmail-jiglesias: Controls the mail to be jiglesias @ ...
- . Qmail-osus: Controls the mail to be osus @ ...
We can even go a step further with a file. Qmail-default-jiglesias, and monitored in the mail that goes to any type steering jiglesias-XXXX @ ..., ie, any address that begins with jiglesias-will be controlled by this file. qmail.
In the base case, which is what we saw, the supply is the user's mailbox (Maildir directory) but we could do other things depending on the content of the file. Qmail delivery charge of processing the mail:
Forwarding to another account:
- # cat /home/jiglesias/.qmail-jiglesias [Jiglesias Lerez @ ~] # cat / home / jiglesias / .qmail-jiglesias
- reenvio@osusnet.com
Referral to a program / script:
- # cat /home/jiglesias/.qmail-osus [Jiglesias Lerez @ ~] # cat / home / jiglesias / .qmail-osus
- | Preline / usr / bin / program
A combination of these
- # cat /home/jiglesias/.qmail-jiglesias [Jiglesias Lerez @ ~] # cat / home / jiglesias / .qmail-jiglesias
- . / Maildir /
- reenvio@osusnet.com
- | Preline / usr / bin / program
The case that interests us is the second, ie, pass the email delivery to a script that will analyze the email and make decisions.
You will have noticed the | preline the file. qmail. It is the system that prepares an email to be processed and delivered to another script by adding a few headers. The output of the script that the email is important because it influences the outcome of the final delivery of mail in particular is important to know that if we reject an email that will complete the script with an exit (100), this indicates a qmail should reject this message. We will see later the value of this output.
Practice
Once we know how to pass control of a received email to a script, see how to treat it. What we do with a script PHP . In our archive. Qmail will do something like:
- # more .qmail-jiglesias [Jiglesias Lerez @ ~] # more. Qmail-jiglesias
- | Preline / usr / bin / php / home / jiglesias / test.php
We have now completed work on the mail server. Let us see how to parse the email from PHP.
The first thing to do is pick up the contents of the email from script through standard input, then we can process the email as a text string.
- <? Php
- ?>
With these simple two lines of code in our script we will email content. Now we only process it. We can do it line by line on our own or rely on any library. I use Mail_mimeDecode of Pear . The advantage of this bookstore is that we can also obtain the text of email attachments.
- <? Php
- 'include_bodies' ] = true ; $ Params ['include_bodies'] = true;
- 'decode_bodies' ] = true ; $ Params ['decode_bodies'] = true;
- 'decode_headers' ] = true ; $ Params ['decode_headers'] = true;
- 'input' ] = $email ; $ Params ['input'] = $ email;
- ( $params ) ; $ Structure = Mail_mimeDecode:: decode ($ params);
- ( trim ( $structure -> headers [ 'from' ] ) ) ; $ From = addslashes ( trim ($ structure -> headers ['from']));
- [ 1 ] ; $ Name = $ pp [1];
- ?>
So we can already process the email and make decisions that we consider appropriate. We insert it in a database, launch other automated processes, sending SMS alerts ... what comes to mind.
Case Studies
The key question, after seeing the theory, why would this serve me?. I propose two applications that I have done.
Support System
The typical support ticket system. In the email subject will drag the ticket identifier, eg [# 123 445]. We will have to review the case and check whether predefined pattern appears. If there are facing a new ticket and insert the data in our database, otherwise it is a response to a previous incident and that we will have the identifier. Simple right?. We may even add to our files indicendias they can get in the email.
Sending files from phone
The second example is similar to the previous technically but conceptually distinct. Most phones (except the high end) can not send files from wap forms (<input type="file">). The alternative is that the user sends an email or MMS (most operators allow sending of email addresses) with your files. Our script will process the contents of mail received, decrypt files and treated as appropriate.
Two simple examples of how to use email to automate tasks, but as I said, we could do everything you can think of, from sending an SMS alert to even restart your server or launch any other task.










5 users have commented on "Other utilities to a mail server"
Feed comments for this entry TrackbackI would like you to help me create the mailboxes of some mail server from a php script, you can do?
thank you very much forward to your cometary
Hi Andrew,
It depends on many things.
What mail server you use?
How to perform user management?
[...] If you have installed qmail as MTA, and want to take action when you receive an email, such as running a php to filter spam, control returns mail redirects, autoresponders, etc can do what we said in Osus your blog. [...]
Osus Hello,
Thank you very much for this tutorial, I will try to implement, is very clear and very interesting.
Regards
[...] Idea seemed very appropriate to give a practical example of something we saw some time on other utilities to a mail server and, as happened to me a few months ago, which was an article becomes [. ..]
Leave a response