Skip to content

Commit

Permalink
chore: Handle invalid email address in IMAP channel
Browse files Browse the repository at this point in the history
  • Loading branch information
sojan-official committed May 10, 2024
1 parent 05f0398 commit 09259f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/mailboxes/imap/imap_mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def process(mail, channel)
# prevent loop from chatwoot notification emails
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
find_or_create_contact
find_or_create_conversation
Expand All @@ -34,6 +37,18 @@ def decorate_mail
@processed_mail = MailPresenter.new(@inbound_mail, @account)
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
return if in_reply_to.blank?

Expand Down
10 changes: 10 additions & 0 deletions spec/mailboxes/imap/imap_mailbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
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
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
let(:reply_mail) do
Expand Down

0 comments on commit 09259f9

Please sign in to comment.