This is one of the most interesting thing that I have worked on and implemented in one of my project. One can actually perform various actions based on free text message sent over mail. My use case was to approve the ticket through mail in ticketing tool. Let’s see how we can implement this.
Mailing Address
The most basic requirement is mailing address to which we will send and receive the messages. Now all the messages/mails coming to that email address needs to be redirected to the Mail Parser (application running on server to read mail text). Redirection can be done by configuring mail forwarding for that mailbox in mail exchange (https://technet.microsoft.com/en-us/library/dd351134(v=exchg.160).aspx).
Message Receiver and Transporter
This is the most trickiest part of whole setup. Setting up mail receiver includes lot of configuration.
- Sendmail: Sendmail service required to be running to receive the forwarded messages coming to the mailbox. It is the default mailing transfer agent on the RedHat Linux distribution.
/etc/init.d/sendmail start
- Change default Sendmail to allow sendmail to receive mail: By default sendmail receives mail from local server, but as we need to accept mails from external mail exchange so change the described setting to allow mails from other sources.
Edit the file /etc/mail/sendmail.mc and change/comment the line:
From: DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')
To: dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')
- Relay: We have allowed mail receiving from all sources. But it is a good practice to define the legit senders and relay mails received by them only. Or in other sense block the spammers.
File: /etc/mail/access
Connect:localhost.localdomain RELAY
Connect:localhost RELAY
Connect:127.0.0.1 RELAY
Connect:server1.mailexchange RELAY
Connect:server2.mailexchange RELAY
- Alternate host names: Sendmail will not accept mail for a domain unless it is permitted to do so. It is necessary to define all the alternate host names of the server.
File: /etc/mail/local-host-names
server1.mailexchange
mail1.exchange
- Local mail user: All the mails coming to particular domain on sendmail are sent to particular user which must be there on server. So create local user which will receive all mails for particular domain and process it.
Command: useradd sample
- Separate emails by domain: It is optional but will be required if you have multiple domains and want to segregate mails coming from all domains. Map each domain to particular user to which mails will go.
File: /etc/mail/virtusertable
sample@domain1.com sample
sample@domain2.com test
test@domain2.com test
- Generate a new configuration file: Post all the changes run below command to load new config.
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
- Restart sendmail:
/etc/rc.d/init.d/sendmail restart
Mail Parser
This is one another important part of setup. Till now sendmail has sent the mail to specific user of domain. Now we need to process the mail and filter out spammers by matching the valid formats. There are few options available but the one I used is procmail (mail delivery agent).
Create file .procmailrc inside home directory of local mail user and define configuration to filter the message and pass it to application to parse the mail and perform the task.
/home/sample/.procmailrc
#.procmailrc
LOGFILE=/home/sample/procmail_log
VERBOSE = YES
:0
* ^(to|Cc|To|cc).*sample@domain1.com
| /usr/bin/python2.7 /opt/scripts/email_approval.py
procmail is automatically invoked by sendmail as soon mail comes. The message has reached to your application, now it is all up to you to parse it and perform the action based on mail text message.









