Trigger script upon email entry


#1

Hi.

I urgently need help with this here.

I have to create a system which allows to trigger immediate script execution when a specific email is received in my mailbox.

I explain … when I send an email to trigger@mydomain.com, I need to be able to process this email right away (via a script).

How can I do this?

Thanks,


#2

You need to setup 2 forwards.

First in the dreamhost panel go to Mail > Message Filters and add a filter to forward the email to the shell account on the Web Server. You can either use specific conditions or if you want to forward ALL emails received to the shell account then use something like “Subject” “Does not Contain” “lkdfjdskfjdsakfj” (a garbage string that will never be found in a Subject).

Next in the users Home directory on the webserver make a .forward file containing a single line such as

|/path/to/php /path/to/script

(Note the pipe as the first char)

(or perhaps it’s .forward-postfix – I can’t remember and do not have this set up anymore)

The reason for 2 forwards is that at dreamhost mail arrives to your mailbox on a different server, the first forward sends the mail from the mail server to a specific user on the webserver, the second forward (.forward file) takes new mail received on the webserver and pipes it to your script.


#3

[quote=“LakeRat, post:2, topic:63904”]
Next in the users Home directory on the webserver make a .forward file containing a single line such as

|/path/to/php /path/to/script

(Note the pipe as the first char)

(or perhaps it’s .forward-postfix – I can’t remember and do not have this set up anymore)[/quote]

It is “.forward.postfix”.
But this one exists already.

user@mysharedserver:~$ cat .forward.postfix "|/usr/bin/procmail"

Wouldn’t I mess around with regular e-mail traffic when changing this?

Thanks,


#4

[quote="maryam2000, "]
good done[/quote]

???


#5

It’s not messing with regular email because regular email isn’t run on your shared web server.

The only email that’s going to be processed by this .forward is email arriving at the shared server, which must be 1) generated by and “from” this shared server 2) forwarded to this shell account on this shared web server by a filter on a regular mailbox on the email server.

To be safe, leave the forward to procmail and add a ", " and another pipe. the .forward file can accept multiple forward addresses.


#6

I tried it initially very simplistically.

user@mysharedserver:~$ cat .forward.postfix "|/home/user/trigger.sh" user@mysharedserver:~$ cat trigger.sh #!/bin/sh TODAY=`date +%Y%m%d-%H%M%S` echo $TODAY >> trigger.log user@mysharedserver:~$ ls -l trigger.sh -rwxr-xr-x 1 user grp 65 Feb 10 00:33 trigger.sh

No joy!

trigger.log doesn’t fill!
trigger.sh works manually!

The forward in the DH web interface works as well. I tested it with an “Add this to the subject:”. Worked fine!

But as soon as I forward the filter rule to my shell account, .forward.postfix doesn’t fire up the trigger.sh.

What can be wrong?


#7

mutt is installed on shared servers. A basic mail client, you can use it to check mail from your shell users mail from the terminal window. This can be used to tell you if the first forward is working. If your not familiar with mutt just type mutt at the shell prompt, that and ctrl-q or ctrl-q is about all you need to know for this test.

I just tested a forward and it got to the shared server.

Also I see that I have an existing .forward.postfix file, but that doesn’t appear to work. .forward does appear to work tho, I did a simple test using an email address as the forward to address, that worked so the pipe should to, if you still have problems I’ll try testing with your trigger.sh miniscript next…

It occurs to me the last time that I was using this feature shared servers were still Debian rather than Ubuntu, perhaps that is the .forward vs .forward.postfix difference. (I can’t explain the difference)


#8

Hi.

I’ll use mutt as soon as my simplistic trigger,sh script works.

I’ll tested with .forward. No joy either … !!! trigger.sh isn’t fired when I write an e-mail to my trigger address.

usr@srv:~$ cat .forward "|/home/usr/trigger.sh" usr@srv:~$ ls -l .forward -rw------- 1 usr grp 27 Feb 11 02:33 .forward usr@srv:~$ cat trigger.sh #!/bin/sh TODAY=`date +%Y%m%d-%H%M%S` echo $TODAY >> trigger.log usr@srv:~$ ls -l trigger.sh -rwxr-xr-x 1 usr grp 65 Feb 10 00:33 trigger.sh

I re-tested the DH web interface forward settings. All fine! The forward works properly when adding something to the subject, so I believe the forward to shell should work as well.

Yes, you might be right thet the .forward.postfix file is a residue from Debian (which was indeed the former OS). Now Ubuntu 12.04.05.

Just to make it clear … you are talking about 2 forwards.

[quote=“LakeRat, post:2, topic:63904”]
You need to setup 2 forwards.

First in the dreamhost panel go to Mail > Message Filters and add a filter to forward the email to the shell account on the Web Server. You can either use specific conditions or if you want to forward ALL emails received to the shell account then use something like “Subject” “Does not Contain” “lkdfjdskfjdsakfj” (a garbage string that will never be found in a Subject).[/quote]

I didn’t do it like that (“lkdfjdskfjdsakfj” …) but a simple forward from a specific e-mail address to my shell account. Was this correct? Since you are talking about “2 forwards”, I believe you mean 1 forward on the DH web interface and 1 forward in the .forward file. Right?


#9

Yep 2 forwards. 1st is created in the dreamhost panel to forward from the email server to the web server.

that was a suggestion to help you figure out whether forward #1 works. Once this is working for you won’t need mutt.

I’ll mess with your trigger script later today and see if that fires for me.


#10

One small change is needed to trigger.sh, because trigger.sh when triggered by the piped email is running in a non-interactive shell (no logged in user) the full path to trigger.log is required.

#!/bin/sh
TODAY=`date +%Y%m%d-%H%M%S`
echo $TODAY >> /home/USER/trigger.log

$ cat trigger.log
20170211-052527
20170211-054237
20170211-054808

First entry was test from the shell prompt, the other two are successful tests with inbound email sent from email originating outside of Dreamhost.

One interesting thing I noticed, is that forward behavior seems to be different now than it was years ago when I was using this functionality. At that time, both forwards would leave copies of the email behind in the respective mailbox as it was forwarded. That doesn’t seem to be the case anymore. This is good from the prospective of ongoing trash cleanup, but makes it harder to see what is happening during setup/testing/debugging.

So in regards to mutt, it’s now only helpful at a specific point in testing. After forward #1 is established in the web panel and before the .forward is created. Once .forward is created mail no longer reaches the local mailbox on the webserver.

So to restate all from Step 1:

1 - Dreamhost panel > Mail > Message Filters. Create a filter to forward emails to a specific user on your web server. I used “Subject” “contains” “t3st” for my testing today. In the past, I’ve found a good method to forward ALL is to use “Subject” “does not contain” “kadjfhdJsfhdsajfh48ZZu9erwifkj” (garbage string) since the dreamhost panel doesn’t have a method to “forward all”.

To test if THIS forward is working: log into the correct web server account then invoke mutt. mail forwarded to the webserver should at this point be showing up in mutt’s inbox. (Once we complete step 2, this appears to no longer be true)

2 - Create /home/USER/.forward on the webserver. Make it a single line file starting with a pipe.

[sharedserver]$ cat .forward
|/home/USER/trigger.sh

Testing this .forward with:

[sharedserver]$ cat trigger.sh
#!/bin/sh
TODAY=`date +%Y%m%d-%H%M%S`
echo $TODAY >> /home/USER/trigger.log
[sharedserver]$ cat trigger.log
20170211-052527
20170211-054237
20170211-054808

#11

This did the job.
The trigger.sh fires up when the trigger e-mail is received.
Many thanks !!!

So I believe that this is not necessary anymore now?!

.forward is setup as follows (trigger.sh only for testing purpose):

I noticed that I need procmail so that the e-mail arrives in $HOME/MailDir/new

Now I need to process that e-mail. Scripting is no problem, but I’m newbie in mail programs (like procmail, sendmail, …).

Details what I need to do:

  1. The e-mail should have an .pdf attachment, otherwise the e-mail should be ignored.
  2. The .pdf should be stored somewhere and processed using a tool called mypdftoics.
  3. The resulting .ics file must be retured to the sender of the e-mail.

Perhaps someone can help me to set this up … or give some hints.

e-mails to USER@MYSHAREDSERVER.dreamhost.com should arrive in ~/MailDir/cur. Is that correct? If yes, this doen't work for me.

#12

The email was piped to the Shell (and script) that was spun up to handle the .forward. Since trigger.sh didn’t read the data in the pipe and output, the data evaporated when the script (and shell) terminated.

Bash can be tricky for this… watch out for sub-shells.

As I mentioned before, it’s interesting that copies of the forwarded mails don’t exist in 2 places, in years prior multiple copies did exist. In fact, managing those copies manually was one of two primary reasons we quite using this method.

That won’t work, unless you send the email from a machine inside the dreamhost network.

(I suspect the reason is to prevent shared servers from getting mail bombs)


#13

I changed my posting at the same time you answered it.
Sorry for that.
There are some more details now …


#14

The first thing you’ll need to do is decode the mime attachment. There are pear libraries for that, which I believe are available on shared hosting. php is better for catching the email stream from the pipe than bash is anyway.

Honestly tho, move off shared hosting if the email volume will be great and/or the pdf’s have much size. This isn’t really as task that lends itself to a shared hosting account. What happens is too much CPU gets used decoding attachments and processes start getting killed by procwatch. Don’t forget mypdftoics processing is also processor load. It really all boils down to a shared hosting account can easily become overloaded with this type of file processing.


#15

I got it … all!

  1. .procmailrc looks like this now:

[code]…
:0

  1. This is trigger.sh:

[code]#!/bin/sh

pmemail=$HOME/tmp/pmemail.tmp # procmail piped email
mydftoics=$HOME/mypdftoics # binary including path
logdir=$HOME/log # directory where log file is saved

cat > $pmemail
pmpdf=$(munpack -fq $pmemail | sed ‘s/.[^.]*$//’)
from=$(cat $pmemail | grep From: | grep -i -o ‘[A-Z0-9._%±]+@[A-Z0-9.-]+.[A-Z]{2,4}’)
$HOME/mypdftoics pmpdf.pdf
cat $pmpdf.sum | mutt -e ‘my_hdr From:noreply@mydomain.com’ -a $pmpdf.ics -s “.pdf to .ics file” – $from
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) $pmroster.pdf, $from” >> $logdir/trigger.log
rm -f $pmemail
exit 0[/code]

  1. I use munpack to extract the .pdf from the email. Works great!

  2. The .pdf files are very small (# 15 KB). Hence no need for more performance / web space. Also mypdftoics only needs very little performance. The monthly .pdfs are max. 100 … so no big deal.

Any remarks are welcomed!

I’ll do some fine tunings and then I have exactly what I was looking for.

Many thanks for your help !!!


#16

That won’t work, unless you send the email from a machine inside the dreamhost network.

(I suspect the reason is to prevent shared servers from getting mail bombs)
[/quote]

I just tested this.
Seems to work now.
DH might have changed something!
A friend send me an e-mail to the account : USER@MYSHAREDSERVER.dreamhost.com and it arrived!