POP3 email polling stopped working

We have a strange problem with our Discourse server. For several weeks the email polling from our internal POP3 server worked without any problems. On February 16th the reception of email suddenly stopped. Strangely, we see activity from Discourse in the log files of our POP3 server:

Feb 25 13:54:49 mailout popa3d[12458]: Authentication passed for discourse
Feb 25 13:54:49 mailout popa3d[12458]: 1545 messages (12060237 bytes) loaded
Feb 25 13:54:49 mailout popa3d[12458]: 0 (0) deleted, 1545 (12060237) left

However, no new emails are processed.

We are using the newest Discourse version 2.7.0.beta4 (since today 2.7.0.beta3 before). These are our email settings:

Do you have any idea what could be wrong? What can we do to investigate the error further?

Are these old emails? What are the dates on these emails?

I have just about the same situation.

POP3 was working for years, then starting from Feb 23, emails stopped arriving.

I checked the email server, and Discourse diligently polled the server every few minutes. However, nothing is received.

The emails remain in the email server.

Note: I did an update to version 2.7.0.beta4 on Feb 22, so I believe this probably has to do with it…

The emails stuck in the email server are all dated AFTER the POP3 stopped working, i.e. after Feb 23 in my case.

OK, try this:

Go into the email server, delete all the emails there. Obviously, there needs to be some way to keep the ones that you want to keep…

Then wait for Discourse to POP3 again.

This now works for me and emails start showing up again.

I have a suspicion that some internal email ID counter is messed up during the latest version update. @codinghorror Maybe it is set to a very large number so all emails get treated as “already read” or something…

If I don’t delete all existing emails, new emails just pile up.

1 Like

The most recent change in POP3 polling happened in January.

That alone shouldn’t stop Discourse from polling, but it’s possible that an incoming email raises an exception. There’s no error handling in the new code…

@jzedlitz Are you still seeing the problem and want to help get to the bottom of this? Are there any related errors in /logs? Do you see an error in /sidekiq/scheduler when you search for Jobs::PollMailbox? Do you see an error when you manually poll in the rails console?

./launcher enter app
rails c

Jobs::PollMailbox.new.execute({})

Usually, at least for me, the situation is that the POP3 poll succeeds.

I can see Discourse polling from the email server.

However, Discourse simply disconnects without downloading any mail.

At least for me there are no errors whatsoever.

We also added a safety check to ignore very old mail a bit ago, perhaps @martin remembers.

Yeah that was the PR @gerhard linked in POP3 email polling stopped working - #6 by gerhard. That should not break anything I don’t think. I would need the full original email message to test with. This is the main code change:

def mail_too_old?(mail_string)
  mail = Mail.new(mail_string)
  date_header = mail.header['Date']
  return false if date_header.blank?

  date = Time.parse(date_header.to_s)
  date < 1.week.ago
end

So I guess maybe there could be a silent error if Time.parse fails? Since we don’t even attempt to do anything if the Date header is missing.

2 Likes

Sorry for not responding sooner. We have more or less solved the problem by emptying the POP3 mailbox for Discourse. Immediately Discourse was able to pick up messages again. I transferd some of the newer emails into the now-empty POP3 mailbox. These could also be processed. However, as soon as I put some of the 1,400 older email into the mailbox Discourse won’t process any messages.

3 Likes

I’ve just stumbled on the same issue. I noticed something was off when emails should have created messages. The POP3 mailbox had a few emails in it (I run a small instance).
Putting messages one by one seems to have done the trick, I think it works better now but I have to wait a bit to confirm this.

1 Like

Unfortunately, the problem has occurred again. Using formail I have split the mbox into the single messages and fed them one by one into the POP3 account. Some (even older) messages were processed by Discourse, other not.

Is there a way to trigger more log messages from Jobs::PollMailbox.new.execute({}) to see why a message is ignored?

Have you been checking /logs when a email that refuses to be processed is found?

The posts above certainly sound like it’s particular messages that are failing.

1 Like

Which logfile exactly? I tried tail -f /var/discourse/shared/standalone/log/rails/*.log but no messages show up while Discourse is trying to fetch the emails.

As an admin, visit https://discourse.example.com/logs in your web browser.

Notably, the folder is named /log/, and it wasn’t what i was referring to :smirk:

1 Like

Thanks for the explanation. Unfortunately, /logs/ stays empty while fetching emails. Is there a way to increase the log level?

1 Like

There might be a problem in the error handling in pop3 polling then. Someone needs to check out the code and see if it’s swallowing errors.

Could you apply the following changes to app/jobs/scheduled/poll_mailbox.rb inside the docker container before running Jobs::PollMailbox.new.execute({}) in the rails console?

       pop3.start(SiteSetting.pop3_polling_username, SiteSetting.pop3_polling_password) do |pop|
+        mail_string = nil
         pop.each_mail do |p|
+          mail_string = nil
           mail_string = p.pop
           break if mail_too_old?(mail_string)
           process_popmail(mail_string)
           p.delete if SiteSetting.pop3_polling_delete_from_server?
+        rescue => e
+          puts e.message
+          puts e.backtrace.join("\n\t")
+          puts "", "MESSAGE:", mail_string if mail_string
         end
       end
1 Like

Now that I know which file to look in, I was able to do some experimenting myself. The problem is the mail_too_old check. After I removed it the stuck messages were processed.

1 Like

Yeah, I suspected that, but it would be interesting to know which email prevented mail_too_old? from working. Was it really too old? Did it fail to parse the email? Something else? You can PM me the email that was stopping POP3 from working if you want.