chore: Handle invalid email address in IMAP channel (#9450)
This commit is contained in:
@@ -12,6 +12,9 @@ class Imap::ImapMailbox
|
|||||||
# prevent loop from chatwoot notification emails
|
# prevent loop from chatwoot notification emails
|
||||||
return if notification_email_from_chatwoot?
|
return if notification_email_from_chatwoot?
|
||||||
|
|
||||||
|
# Stop processing if email format doesn't match Chatwoot supported mail format
|
||||||
|
return unless email_from_valid_email?
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
find_or_create_contact
|
find_or_create_contact
|
||||||
find_or_create_conversation
|
find_or_create_conversation
|
||||||
@@ -34,6 +37,18 @@ class Imap::ImapMailbox
|
|||||||
@processed_mail = MailPresenter.new(@inbound_mail, @account)
|
@processed_mail = MailPresenter.new(@inbound_mail, @account)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def email_from_valid_email?
|
||||||
|
Rails.logger.info("Processing Email from: #{@processed_mail.original_sender} : inbox #{@inbox.id}")
|
||||||
|
|
||||||
|
# validate email with Devise.email_regexp
|
||||||
|
if Devise.email_regexp.match?(@processed_mail.original_sender)
|
||||||
|
true
|
||||||
|
else
|
||||||
|
Rails.logger.error("Email from: #{@processed_mail.original_sender} : inbox #{@inbox.id} is invalid")
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def find_conversation_by_in_reply_to
|
def find_conversation_by_in_reply_to
|
||||||
return if in_reply_to.blank?
|
return if in_reply_to.blank?
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,16 @@ RSpec.describe Imap::ImapMailbox do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when a new email with invalid from' do
|
||||||
|
let(:inbound_mail) { create_inbound_email_from_mail(from: 'invalidemail', to: 'imap@gmail.com', subject: 'Hello!') }
|
||||||
|
|
||||||
|
it 'does not create a new conversation' do
|
||||||
|
allow(Rails.logger).to receive(:error)
|
||||||
|
class_instance.process(inbound_mail.mail, channel)
|
||||||
|
expect(Rails.logger).to have_received(:error).with("Email from: invalidemail : inbox #{inbox.id} is invalid")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when a reply for existing email conversation' do
|
context 'when a reply for existing email conversation' do
|
||||||
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
|
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
|
||||||
let(:reply_mail) do
|
let(:reply_mail) do
|
||||||
|
|||||||
Reference in New Issue
Block a user