coddingtonbear/django-mailbox

filter emails with command `python manage.py getmail`

Closed this issue · 5 comments

I would like to be able to use the python manage.py getmail command while also taking advantage of the ability to specified a condition with the get_new_mail(condition=None) function specified here: https://django-mailbox.readthedocs.io/en/latest/topics/appendix/instance-documentation.html#django_mailbox.models.Mailbox.get_new_mail

how can i do that?

ad-m commented

What do you want to achieve?

I would like to be able to run each message through the following function before actually saving it.

def removePhotoEmails(message):
  if "Subject: [WEBSITE PHOTOS]" in str(message):
    print("email is for the photo gallery")
    return False
  else:
    print("email is for the announcements")
    return True

from inside my view.py file, I was previously doing this to determine if the function does work [which it does]

def index(request):
  print("announcements index")
  mailboxes = Mailbox.objects.all()
  for mailbox in mailboxes:
    mailbox.get_new_mail(condition=removePhotoEmails)

but that seems a very bad way to filter cause it relies on calling the get_new_email everytime the index page is loaded which can lead to problems with duplicate mail. So I would like to be able to pass the removePhotoEmails function to the get_new_mail function with the python manage.py getmail so i can do periodic polling via the crontab but not entirely sure how to do that.

ad-m commented

Since you have very specific needs which are hard to generalize, and you can see that you can not put your code in the view, why not write your own management command implementation and put there your code?

However, I suggest to process all messages and then use only some of them by implementing own logic in custom signal handler.

@ad-m I am wondering however, is there any way to use python manage.py getmail while also taking advantage of the condition for the email processing? or can those 2 things not be used in conjunction?

closing as no longer a needed functionality