I am trying to build a email chatbot but it has this bug where after it sends the first message, and then gets a response it keeps spamming the answered to the response it got until it gets another response which then it repeats again I was thinking to solve this I should use a variable which detects emails and later down the code a condition that responds only if a email is received, does anyone have any idea on how I could fix this? Thanks
def receive_email():
try:
mail = imaplib.IMAP4_SSL("smtp.gmail.com")
mail.login(email_address, email_password)
mail.select('inbox')
#searches inbox
status, data = mail.search(None, 'Recent')
mail_ids = data[0].split()
latest_email_id = mail_ids[-1]
status, data = mail.fetch(latest_email_id, '(RFC822)')
#gets message
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
sender = msg['from']
subject = msg['subject']
if msg.is_multipart():
for part in msg.get_payload():
if part.get_content_type() == 'text/plain':
return part.get_payload()
message = msg.get_payload()
return message,
except Exception as e:
print("Error: ", e)
print("Could not receive email")
return None, None
This is the usual problem for an email autoresponder, if I understand you correctly, and RFC 3834 offers good advice.
Since answers should be self-contained I offer a summary:
Auto-Submitted: auto-replied
header field on your outgoing messages. Any value other than no
will prevent well-written autoresponders from replying to your outgoing messages.\answered
flag on the message you reply to, immediately before you send the reply.recent
to unanswered not header "auto-submitted" ""
. unanswered
means that the search won't match the messages on which you set the \answered
flag, not header "auto-submitted" ""
means that you'll not match messages that contain any auto-submitted header field.return-path
or sender
, not the one in from
. This is a matter of convention. Auto-submitted mail will often have a special return-path
that points to an address that never sends any autoreply.You may also extend the search key with more details from RFC 3834. The one I suggest should work, but not header "precedence" "junk"
will for example prevent your code from replying to a bit of autogenerated mail. Sendgrid and its friends also add header fields you may want to look for and exclude.
If the incoming message has headers like this (use the "view headers" function of most mail readers to see it):
From: example@example.com
Subject: Weekend
To: srtai22@gmail.com
Message-id: <56451182ae7a62978cd6f6ff06dd21e0@example.com>
Then your reply should have headers like this:
Return-Path: <>
From: srtai22@gmail.com
To: example@example.com
Auto-Submitted: auto-replied
Subject: Auto: Weekend
References: <56451182ae7a62978cd6f6ff06dd21e0@example.com>
There'll be many more fields in both, of course. Your reply's return-path says that nothing should respond automatically, From and To are as expected, auto-submitted specifies what sort of response this is, subject doesn't matter very much but this one's polite and well-behaved, and finally references links to the original message.