I am trying to make a email chatbot but it spams how could i fix this?

huangapple go评论63阅读模式
英文:

I am trying to make a email chatbot but it spams how could i fix this?

问题

我正在尝试构建一个电子邮件聊天机器人,但它存在一个问题,即在发送第一条消息后,然后收到回复后,它会不断地将答复发送给它收到的回复,直到收到另一条回复,然后它会再次重复。我在思考如何解决这个问题,我应该使用一个变量来检测电子邮件,然后在代码的后面使用一个条件来只在收到电子邮件时进行回应,有人有任何关于如何解决这个问题的想法吗?谢谢

def receive_email():
    try:
        mail = imaplib.IMAP4_SSL("smtp.gmail.com")
        mail.login(email_address, email_password)
        mail.select('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)')
        # 获取消息
        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("无法接收电子邮件")
        return None, None

希望这对你有所帮助。

英文:

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

答案1

得分: 0

这是电子邮件自动回复的常见问题,如果我理解你的意思正确,并且RFC 3834提供了很好的建议。

由于答案应该是自包含的,我提供一个摘要:

  1. 在你的发件消息上添加Auto-Submitted: auto-replied头字段。除了no之外的任何值都将阻止良好编写的自动回复程序回复你的发件消息。
  2. 在发送回复之前,立即将消息标记为\answered在这里了解更多
  3. 将搜索键从recent更改为unanswered not header "auto-submitted" ""unanswered表示搜索不会匹配你已经设置了\answered标记的消息,not header "auto-submitted" ""表示你不会匹配包含任何自动提交头字段的消息。
  4. 将回复发送到return-pathsender中的地址,而不是from中的地址。这是一种约定。自动提交的邮件通常会有一个特殊的return-path,指向一个绝对不会发送任何自动回复的地址。

你还可以根据RFC 3834的更多细节扩展搜索键。我建议的这个应该有效,但例如not header "precedence" "junk"将防止你的代码回复自动生成的邮件。Sendgrid和它的伙伴还会添加你可能想要查找和排除的头字段。

如果传入的消息具有以下标题(使用大多数邮件阅读器的“查看标题”功能查看):

From: example@example.com
Subject: Weekend
To: srtai22@gmail.com
Message-id: <56451182ae7a62978cd6f6ff06dd21e0@example.com>

那么你的回复应该具有以下标题:

Return-Path: <>
From: srtai22@gmail.com
To: example@example.com
Auto-Submitted: auto-replied
Subject: Auto: Weekend
References: <56451182ae7a62978cd6f6ff06dd21e0@example.com>

当然,两者都会有更多字段。你的回复的return-path表示不应该自动响应任何东西,From和To如预期的那样,auto-submitted指定了这是什么类型的响应,主题并不太重要,但这个是礼貌和有礼貌的,最后引用链接到原始消息。

英文:

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:

  1. Add the 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.
  2. Set the \answered flag on the message you reply to, immediately before you send the reply.
  3. Change the search key from recent to unanswered not header &quot;auto-submitted&quot; &quot;&quot;. unanswered means that the search won't match the messages on which you set the \answered flag, not header &quot;auto-submitted&quot; &quot;&quot; means that you'll not match messages that contain any auto-submitted header field.
  4. Direct your replies to the address in 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 &quot;precedence&quot; &quot;junk&quot; 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: &lt;56451182ae7a62978cd6f6ff06dd21e0@example.com&gt;

Then your reply should have headers like this:

Return-Path: &lt;&gt;
From: srtai22@gmail.com
To: example@example.com
Auto-Submitted: auto-replied
Subject: Auto: Weekend
References: &lt;56451182ae7a62978cd6f6ff06dd21e0@example.com&gt;

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.

huangapple
  • 本文由 发表于 2023年2月13日 23:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定