1] Passing e-mails to a shell script opens up a brave new world of possibilities
If you have a Postfix mail server setup and running as MTA(Mail Transfer Agent), or even as a LDA (Local Delivery Agent)
it is really easy to pipe a mail to your shell script.
you open up the aliases file mostly it is in /etc/aliases:
So you see you have a bunch of aliases the first part is the email address you sent to e.g : www@yourdomain.org
after the ":" is where the mail gets delivered so in our example the mail will be delivered to user1
if you want to redirect to your script change line 8 of the aliases file into:
www : |"/home/user1/myscript.sh/" user1
this will pipe all emails addressed to "www" to your script
2] Use Munpack to save the attachments of e-mails
One great use of the redirection is to extract the attachments of the e-mails to a separate directory while still preserving the original mail.
For this use I use munpack to extract the attachments.
munpack is part of the mpack package which can be installed on
ubuntu: apt install mpack
Arch : yay -S aur/mpack
Usage of: munpack email -C destdirectory
3] Example script extracting the attachments
myscript.sh
make shure your script is executable by the postfix user
Since you pipe your incoming mail to the script munpack reads from stdin.
4] Processing the new created attachments
We can go and take it a step further and parse our newly created attachment to for example a php script.
Here is one little tricky part if your e-mail contains more than one attachment you will need to filter out the one you need.
Here is my way to do this.
I use the munpack.log file which contains errors ,and notices from munpack and also the name of the attachments.
So I take this file read it line by line and select the one I need e.g. I need the first html file. This must be parsed by a php script.
So we can add this to our script:
5] The complete example script:
If you have a Postfix mail server setup and running as MTA(Mail Transfer Agent), or even as a LDA (Local Delivery Agent)
it is really easy to pipe a mail to your shell script.
you open up the aliases file mostly it is in /etc/aliases:
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: user1
www: user1
ftp: user1
abuse: root
So you see you have a bunch of aliases the first part is the email address you sent to e.g : www@yourdomain.org
after the ":" is where the mail gets delivered so in our example the mail will be delivered to user1
if you want to redirect to your script change line 8 of the aliases file into:
www : |"/home/user1/myscript.sh/" user1
this will pipe all emails addressed to "www" to your script
2] Use Munpack to save the attachments of e-mails
One great use of the redirection is to extract the attachments of the e-mails to a separate directory while still preserving the original mail.
For this use I use munpack to extract the attachments.
munpack is part of the mpack package which can be installed on
ubuntu: apt install mpack
Arch : yay -S aur/mpack
Usage of: munpack email -C destdirectory
3] Example script extracting the attachments
myscript.sh
#! /bin/bash
/usr/bin/munpack -C /home/user1/attachments/ > /home/user1/munpack.log
make shure your script is executable by the postfix user
Since you pipe your incoming mail to the script munpack reads from stdin.
4] Processing the new created attachments
We can go and take it a step further and parse our newly created attachment to for example a php script.
Here is one little tricky part if your e-mail contains more than one attachment you will need to filter out the one you need.
Here is my way to do this.
I use the munpack.log file which contains errors ,and notices from munpack and also the name of the attachments.
So I take this file read it line by line and select the one I need e.g. I need the first html file. This must be parsed by a php script.
So we can add this to our script:
while read p; do
if [[ $p == *"html"* ]]
then
IFS=' (' read -a array <<< "$p"
cat "/home/user1/attachments/${array[0]}" | /usr/bin/php /home/user1/parse_mail.php > /home/user1/parse_mail.log
fi
done </home/user1/munpack.log
5] The complete example script:
#! /bin/bash
/usr/bin/munpack -C /home/user1/attachments/ > /home/user1/munpack.log
while read p; do
if [[ $p == *"html"* ]]
then
IFS=' (' read -a array <<< "$p"
cat "/home/user1/attachments/${array[0]}" | /usr/bin/php /home/user1/parse_mail.php > /home/user1/parse_mail.log
fi
done </home/user1/munpack.log
Reacties
Een reactie posten