WebFaction
Community site: login faq

I am coding with python/django.

I have hundreds of email addresses going to one mailbox. When a NEW email goes to the mailbox, I would like it to trigger a script where the argument is the email address that the email was sent to

For example: If I have 2 email addresses (YES@mywebsite.com, NO@mywebsite.com) going to the mailbox "binary". When mailbox "binary" receives a new email, it triggers a script:

If sent_to == 'YES@mywebsite.com':

print 'yes!'

else:

print 'no!'

.... I saw this tutorial, http://docs.webfaction.com/user-guide/email.html#filtering-by-sender but I'm not sure how to filter by 'sent to' and I'm not sure how to return the sent-to-address to python.

Thanks!

asked 16 Mar '11, 20:54

benjamin
1146
accept rate: 0%

edited 16 Mar '11, 20:55


That link is for procmail rules, which are not quite what we want. They are for filtering SPAM and for soring within the mailbox. The documentation you would want to review is here,

http://docs.webfaction.com/user-guide/email.html#sending-mail-to-a-script

Although the exact code you will need will more than likely need to be written from scratch.

permanent link

answered 16 Mar '11, 21:02

johns
5.4k312
accept rate: 23%

I don't know enough to make use of that mail2script guide. Could someone post a very simple example?

Specifically I don't understand how to find the location of the email and how to know when there's a new email.

thanks!

(17 Mar '11, 00:06) benjamin

There's a slight paradigm difference between procmailrc and mail2script which is causing this confusion. With mail2script you will be sending the email to two email Targets in the Control Panel: mailboxname, /path/to/script.py

When a new email is received, it goes to both the mailbox and the script. The script receives all of the headers to the email (the entire email is sent to the script via stdin), so it can do the processing you request.

Meanwhile, the email is simultaneously delivered to the mailbox since it was also listed as a target.

I will post another Answer with an example.

(17 Mar '11, 00:18) ryans ♦♦

To follow up. There may or may not be a problem with having multiple addresses pointing to the same script.

Regardless, a solution is to use sub-addressing: http://docs.webfaction.com/user-guide/email.html

Just create one address (for example script@mywebsite.com ) that points to /home/username/script.py for example. Then you can add countless extensions to the address (script+yes@mywebsite.com , script+no@mywebsite.com, script+123@mywebsite.com).

script.py can then pull the extension from the address.

Thanks to webfaction crew for all the help!

permanent link

answered 23 Mar '11, 19:05

benjamin
1146
accept rate: 0%

edited 23 Mar '11, 19:06

Overview:

In your example, you have two email addresses:

  • YES@mywebsite.com
  • NO@mywebsite.com

And these are both delivering to a single mailbox named "binary". In the control panel, this would be expressed as creating two email addresses with the single target of "binary".


Solution 1:

Email addresses can take multiple targets. So, we could just set the following targets:

  • YES@mywebsite.com --> binary, /home/username/print_yes.py
  • NO@mywebsite.com --> binary

Where "print_yes.py" simply prints 'yes!' all of the time. This works because NO@mywebsite.com delivers only to the mailbox, but YES@mywebsite.com delivers to both the mailbox and the script. So, only messages sent to "YES@mywebsite.com" activate the script, and emails sent to "NO@mywebsite.com" are stored in the mailbox but otherwise ignored.

A variant on this theme is to do something like:

  • YES@mywebsite.com --> binary, /home/username/print_yes.py
  • NO@mywebsite.com --> binary, /home/username/print_no.py

Which delivers all messages to the mailbox, and then activates the "yes" or "no" scripts depending on the destination address.


Solution 2:

A more general solution is to deliver all of the new messages to the script, and then filter by content or headers:

  • YES@mywebsite.com --> binary, /home/username/analyze_email.py
  • NO@mywebsite.com --> binary, /home/username/analyze_email.py

Then, inside of analyze_email.py, do something like this:

#!/usr/bin/env python
import sys
import re

output = open('/home/username/example-script.out', 'a')
email_lines = sys.stdin.readlines()
for line in email_lines:
    if re.search(r'To:', line) and re.search(r'YES\@mywebsite\.com', line):
        output.write('YES\n')
        break
output.close()

Note that I have explicitly used regular expressions here (instead of just using line.find() to search for a raw substring) since regular expressions offer a lot more in the way of extensible searching capabilities.

Hope that helps!

permanent link

answered 17 Mar '11, 00:43

ryans ♦♦
5.0k93360
accept rate: 43%

edited 17 Mar '11, 00:47

I'm trying to make use of your solution2 and failing.

I created: 11@mywebsite.com with target --> /home/username/email2script.py

email2script.py looks like this:

#!/username/bin/python2.5

import sys

import re

output = open('/home/username/example-script.out', 'a')

output.write("okay")

output.close()

...so it's not even triggering the python script, because example-script.out is never created. I read somethings about using chmod to make sure it's executable, and/or setting DJANGO_SETTINGS_MODULE...but I'm not really sure how or why I would do this.

(19 Mar '11, 03:08) benjamin

Hi Ben,

If you use the full script that Ryan has there does it work? The script you have isn't quite the same and I know Ryan's script works correctly.

(19 Mar '11, 03:32) klynton

Right, I tried his script first and it didn't work, so then I created the simplest version of it and still didn't work.

The problem is that it's not finding the target and I don't know why. The output scripts are fine, I think.

Thanks!

(19 Mar '11, 13:15) benjamin

Did you make the script executable as outlined in the docs?

http://docs.webfaction.com/user-guide/email.html#sending-mail-to-a-script

You can check if it applied with 'ls -la' or 'getfacl', the permissions will look like this,

-rwxr--r--

Notice the x, rwx stands for read, write, execute. The 9 slots are for rwx of user, group, and other. For the script to work user must have execute permission, which is what the chmod command in the above docs does.

(19 Mar '11, 13:36) johns

I did: $ chmod u+x email2script.py $ ls -la

and it shows this permission: -rwxrwx--x email2script.py

...still doesn't work

(19 Mar '11, 19:15) benjamin

Please submit a support ticket so we can look at the account.

(19 Mar '11, 20:01) johns

I submitted a ticket.

(19 Mar '11, 20:52) benjamin

Hi Benjamin,

The reason this isn't working is because you have multiple servers on your account so the script doesn't know which server to send it to or sends it to a different server each time. This is known issue and there is an internal ticket to fix it.

(19 Mar '11, 21:03) klynton

still not working. I'm guessing there's something wrong with my script...and I have no idea what it is.

(20 Mar '11, 13:14) benjamin

I have sent you an answer in the ticket, you might want to update the post once we get everything working.

(20 Mar '11, 13:46) johns
showing 5 of 10 show 5 more comments

Thanks!

My mistake was to blindly copy/paste your code.

My path for python2.5 is different from what is written. Mine looks like this:

#!/usr/local/bin/python2.5

Thanks for the help guys!

permanent link

answered 20 Mar '11, 15:19

benjamin
1146
accept rate: 0%

edited 20 Mar '11, 15:20

Okay, it was working. Now it's not working again, and I didn't change the script at all.

The only thing I did was add more email addresses that pointed to the same script. Could this cause it to fail? I submitted a ticket a day ago, if you'd like to look at the specifics of my account.

Thanks!

(22 Mar '11, 02:22) benjamin

We're looking at the ticket.

(22 Mar '11, 03:11) klynton
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×261
×8

question asked: 16 Mar '11, 20:54

question was seen: 3,878 times

last updated: 23 Mar '11, 19:06

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM