-1

I am trying to delete some emails through Python, but when I run the code(through git bash) I get the following error: I am trying to delete some emails through Python, but when I run the code(through git bash) I get the following error:

$ python imap_email.py
Traceback (most recent call last):
  File "imap_email.py", line 6, in <module>
    env = environ()
TypeError: 'module' object is not callable
(env)

Here is my code

import imaplib
import email
from email.parser import HeaderParser
import environ

env = environ()

environ.Env.read_env()

server ='outlook.office365.com'
user = env('EMAIL_USER')
password = env('EMAIL_PASSWORD')

def connect(server, user, password):
    imap_conn = imaplib.IMAP4_SSL(server)
    imap_conn.login(user=user, password=password)
    return imap_conn

def delete_email(instance, email_id) :
    typ, delete_response = instance.fetch(email_id, '(FLAGS)')
    typ, response = instance.store(email_id, '+FLAGS', r'(\Deleted)')
    print(delete_response)
    print(response)
    
keywords_list = ['You won', 'Bitcoin']

for keyword in keywords_list:
    conn = connect(server, user, password)
    conn.select( "Spam")
    typ, msg = conn.search(None, '(BODY"' + keyword + '")')
    print(msg)
    msg = msg[0].split()
    for email_id in msg:
        print(email_id)
        delete_email(conn, email_id)
        
    print('Complete !')

Thanks in advance

loai
  • 3
  • 1
  • 1
    Welcome to Stack Overflow. Please explain what you're trying to do with ```env = environ()```. – ewokx May 27 '22 at 10:01
  • I'm not sure what environ is for, but when I looked up how to remove mails via Python it said to use environ. As an addition I can tell you that I have made a file which has been saved as .env, where the user name and the code for the mail is saved. The file's code looks like this: EMAIL_USER="xxx" EMAIL_PASSWORD="xxx" Where xxx is replaced with my real email user and password – loai May 27 '22 at 10:29
  • maybe `env = environ.Env()` or `env = environ.environ()`. OR even `env = environ.Env.read_env()` – furas May 27 '22 at 13:05
  • 1
    this [documentation](https://pypi.org/project/django-environ-2/) shows `env = environ.Env(...)` – furas May 27 '22 at 13:07

1 Answers1

0

See example in documentation

env = environ.Env()

And you forgot .Env.

That's all.

furas
  • 134,197
  • 12
  • 106
  • 148
  • I have just one more question. My keyword list contains words with Danish letters æ, ø, å. When running the code I get this error: UnicodeEncodeError: 'ascii' codec can't encode character '\xe6' in position 9: ordinal not in range(128). When I look up the error, most people say to use encode('utf-8') but I still get the same error. I hope you can help – loai May 29 '22 at 19:40
  • this char doesn't have to be in `UTF-8` but in other encoding. For `Latin1` `b'\xe6'.decode('latin1')` it gives `æ`. And for `CP1250` it gives `ć` - `b'\xe6'.decode('cp1250')`. But `CP1252` also gives `æ` - `b'\xe6'.decode('cp1252')`You may have to use loop to check with different encoding - and it will need `try/except` to catch wrong encodings. But sometimes email may have some information what encoding it use and you may have to search this information. – furas May 29 '22 at 20:01
  • you can also use external modules for IMAP - Maybe they will have functions to detect encoding. ie. [IMAPClient](https://imapclient.readthedocs.io/en/2.2.0/), [imap-tools](https://github.com/ikvk/imap_tools) – furas May 29 '22 at 20:10
  • Thanks for the reply. But it still doesn't seem to work. I have tried entering the suggestions you gave and tried others out from this page: https://docs.python.org/3/library/codecs.html The error occurs at codeline: type, msg = conn.search(None, '(BODY "' + keyword + '")') As I understand it, this code searches for words. i.e. it shouldn't depend on what the email looks like? Since the error occurs by just searching for words containing e.g. the letter æ. It seems that it can't handle the letter itself. Or am I completely wrong? – loai May 29 '22 at 21:04
  • maybe don't search chars - text may not have `æ` but other chars which can make problem. Simply try encoding with `UTF-8` - and do it in `try/except`. And inside `except` try encoding with `Latin1` - and do it also with `try/except`. And inside this `except` try encoding with other codec, etc. You could reduce it with loop like `for encoding in ["utf-8', 'latin1', 'cp1250']:` - and it will need `break` to exit look when it encode it without error. Similar code was in some answers to similar questions. – furas May 29 '22 at 21:13