Search code examples
pythoncallableenviron

I am trying to delete some emails, but when I run the code I get the following error: TypeError: 'module' object is not callable (env)


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


Solution

  • See example in documentation

    env = environ.Env()
    

    And you forgot .Env.

    That's all.