diff --git a/app/jobs/inboxes/fetch_imap_emails_job.rb b/app/jobs/inboxes/fetch_imap_emails_job.rb index bf9972778..80308f1e1 100644 --- a/app/jobs/inboxes/fetch_imap_emails_job.rb +++ b/app/jobs/inboxes/fetch_imap_emails_job.rb @@ -9,7 +9,7 @@ class Inboxes::FetchImapEmailsJob < ApplicationJob process_email_for_channel(channel) rescue *ExceptionList::IMAP_EXCEPTIONS channel.authorization_error! - rescue EOFError => e + rescue EOFError, OpenSSL::SSL::SSLError => e Rails.logger.error e rescue StandardError => e ChatwootExceptionTracker.new(e, account: channel.account).capture_exception @@ -87,8 +87,12 @@ class Inboxes::FetchImapEmailsJob < ApplicationJob end def last_email_time(channel) - time = 1.hour.ago.to_s - time = channel.inbox.messages.incoming.last.content_attributes['email']['date'] if channel.inbox.messages.any? + if channel.inbox.messages.any? + time = channel.inbox.messages.incoming.last.content_attributes['email']['date'] + time ||= channel.inbox.messages.incoming.last.created_at.to_s + end + time ||= 1.hour.ago.to_s + DateTime.parse(time) end diff --git a/spec/fixtures/files/mail_with_no_date.eml b/spec/fixtures/files/mail_with_no_date.eml new file mode 100644 index 000000000..745d5782d --- /dev/null +++ b/spec/fixtures/files/mail_with_no_date.eml @@ -0,0 +1,6 @@ +Date: +From: testemail@gmail.com +To: imap@outlook.com +Subject: test text only mail + +text only mail diff --git a/spec/jobs/inboxes/fetch_imap_emails_job_spec.rb b/spec/jobs/inboxes/fetch_imap_emails_job_spec.rb index 3a3b5f577..20c3880e8 100644 --- a/spec/jobs/inboxes/fetch_imap_emails_job_spec.rb +++ b/spec/jobs/inboxes/fetch_imap_emails_job_spec.rb @@ -16,6 +16,7 @@ RSpec.describe Inboxes::FetchImapEmailsJob, type: :job do let(:ms_email_inbox) { create(:inbox, channel: microsoft_imap_email_channel, account: account) } let!(:conversation) { create(:conversation, inbox: imap_email_channel.inbox, account: account) } let(:inbound_mail) { create_inbound_email_from_mail(from: 'testemail@gmail.com', to: 'imap@outlook.com', subject: 'Hello!') } + let(:inbound_mail_with_no_date) { create_inbound_email_from_fixture('mail_with_no_date.eml') } let(:inbound_mail_with_attachments) { create_inbound_email_from_fixture('multiple_attachments.eml') } it 'enqueues the job' do @@ -53,6 +54,35 @@ RSpec.describe Inboxes::FetchImapEmailsJob, type: :job do described_class.perform_now(imap_email_channel) end + + it 'process the email with no date' do + email = Mail.new do + to 'test@outlook.com' + from 'test@gmail.com' + subject :test.to_s + body 'hello' + end + + imap_fetch_mail = Net::IMAP::FetchData.new + imap_fetch_mail.attr = { seqno: 1, RFC822: email }.with_indifferent_access + + imap = double + + allow(Net::IMAP).to receive(:new).and_return(imap) + allow(imap).to receive(:authenticate) + allow(imap).to receive(:select) + allow(imap).to receive(:search).and_return([1]) + allow(imap).to receive(:fetch).and_return([imap_fetch_mail]) + + allow(Mail).to receive(:read_from_string).and_return(inbound_mail_with_no_date.mail) + + imap_mailbox = double + + allow(Imap::ImapMailbox).to receive(:new).and_return(imap_mailbox) + expect(imap_mailbox).to receive(:process).with(inbound_mail_with_no_date.mail, imap_email_channel).once + + described_class.perform_now(imap_email_channel) + end end context 'when imap fetch new emails with more than 15 attachments' do