54 Commits

Author SHA1 Message Date
Pranav
ffa124d0d5 fix: Use .find_by instead .where().first (#12402)
While investigating a customer-reported issue, I found that some emails
were appearing late in Chatwoot. The root cause was query timeouts.

It only happened for emails with an in_reply_to header. In these cases,
Chatwoot first checks if a message exists with message_id = in_reply_to.
If not, it falls back to checking conversations where
additional_attributes->>'in_reply_to' = ?.

We were using:
```rb
@inbox.conversations.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first
```
This looked harmless, but .first caused timeouts. Without .first, the
query ran fine. The issue was the generated SQL:
```sql
SELECT * 
FROM conversations 
WHERE inbox_id = $1 
  AND (additional_attributes->>'in_reply_to' = '<in-reply-to-id>') 
ORDER BY id ASC 
LIMIT $2;
```
The ORDER BY id forced a full scan, even with <10k records.

The fix was to replace .first with .find_by:
```rb
@inbox.conversations.find_by("additional_attributes->>'in_reply_to' = ?", in_reply_to)
```
This generates:

```sql
SELECT * 
FROM conversations 
WHERE inbox_id = $1 
  AND (additional_attributes->>'in_reply_to' = '<in-reply-to-id>') 
LIMIT $2;
```
This avoids the scan and runs quickly without needing an index.

By the way, Cursor and Claude failed
[here](https://github.com/chatwoot/chatwoot/pull/12401), it just kept on
adding the index without figuring out the root cause.

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2025-09-10 10:08:37 +05:30
Pranav
7e70f7a68a fix: Disable automations on auto-reply emails (#12101)
The term "sorcerer’s apprentice mode" is defined as a bug in a protocol
where, under some circumstances, the receipt of a message causes
multiple messages to be sent, each of which, when received, triggers the
same bug. - RFC3834

Reference: https://github.com/chatwoot/chatwoot/pull/9606

This PR:
- Adds an auto_reply attribute to message.
- Adds an auto_reply attribute to conversation. 
- Disable conversation_created / conversation_opened event if auto_reply
is set.
- Disable message_created event if auto_reply is set.

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2025-08-05 13:17:06 +05:30
Pranav
5ab913f7b5 chore: Add a condition to handle bounced email (#11873)
Add bounced emails to the conversation thread.
Fix Gmail bounce detection by checking the X-Failed-Recipients header.

Currently, bounced emails are rejected as auto-replies, which causes
support agents to miss important delivery failure context. This PR
ensures bounced messages are correctly added to the thread, preserving
visibility for the support team.
2025-08-01 14:43:46 +05:30
Sojan Jose
bc42aec68e chore: upgrade ruby version to 3.4.4 (#11524)
- Chore upgrade ruby version to 3.4.4 before we migrate to rails 7.2
over #11037
2025-05-21 19:40:07 +05:30
Pranav
24f49b9b5a fix: Process non-image inline attachments as regular attachments (#10998)
In Chatwoot, we rely on the Content-ID for inline attachments to replace
the link with the uploaded attachment URL. Our expectation was that only
images would be inline, while other attachments would not. However,
email clients like Apple Mail (sigh) allow users to send inline
attachments that are not images, and these attachments often lack a
Content-ID. This creates significant issues in rendering.
 
I investigated how other email clients handle this scenario. When
viewing the same email (sent from Apple Mail) in Gmail, only one image
appears—and it’s treated as an attachment, not inline. This happens
because both attachments are the same image, and Apple Mail only sends
one copy. See the screenshot below.

| Apple Mail | Gmail | 
| -- | -- | 
| <img width="646" alt="Screenshot 2025-02-27 at 8 20 17 PM"
src="https://github.com/user-attachments/assets/e0d1cd2d-e47c-4081-a53b-7a67106341b3"
/> | <img width="360" alt="Screenshot 2025-02-27 at 8 20 51 PM"
src="https://github.com/user-attachments/assets/b206e56e-8f86-43e9-867b-d895c36aff78"
/> |

A good fix for this would be to check if the Content-ID is missing and
then upload the file as a regular attachment. However, the Mail gem (for
some reason) automatically adds a default Content-ID to inline parts. I
need to dig into the source code to understand why this happens.

For now, I’ve implemented a check to treat non-image attachments as
regular attachments. Inline image attachments are already handled by
appending an image tag at the end if the content-id is not found in the
body. A sample conversation to test this behavior is
[here](https://app.chatwoot.com/app/accounts/1/conversations/46732).
2025-02-28 13:33:48 -08:00
Sojan Jose
515778eabb chore: Disable throwing error for malformed to address (#10464)
We don't need to raise error on sentry for malformed to address as it is already logged.

Fixes: https://linear.app/chatwoot/issue/CW-3151/standarderror-invalid-email-to-address-header-standarderror
2024-11-20 18:58:54 -08:00
Pranav
8cdbdaaa07 fix: Process attachments as regular attachments if the text/plain or text/html part is empty (#10379)
Some email clients automatically set Content-Disposition to inline for
specific content types, such as images. In cases where the email body is
empty, inline attachments may not display correctly due to our previous
implementation. Our assumption was that these attachments are referenced
within text/plain or text/html parts.

Customer-reported issues, especially with Apple Mail, show emails with
attachments marked as inline but without any corresponding text parts.
This leads to missing attachments even though would have processed the
attachment.

This update introduces a check for the presence of a text part. If none
exists, inline attachments are treated as regular attachments and added
to the external attachments array, ensuring that all attachments display
properly.

<details>
<summary><b>Script to update the existing emails that are already
available in the system</b></summary>

```rb
def update_content id
  message = Message.find id
  conversation = message.conversation
  message_id = message.source_id

  channel = message.inbox.channel

  authentication_type = 'XOAUTH2'
  imap_password = Google::RefreshOauthTokenService.new(channel: channel).access_token
  imap = Net::IMAP.new(channel.imap_address, port: channel.imap_port, ssl: true)
  imap.authenticate(authentication_type, channel.imap_login, imap_password)
  imap.select('INBOX')

  results = imap.search(['HEADER', 'MESSAGE-ID', message_id])
  message_content = imap.fetch(results.first, 'RFC822').first.attr['RFC822']
  mail = MailPresenter.new(Mail.read_from_string(message_content))

  mail_content = if mail.text_content.present?
                   mail.text_content[:reply]
                 elsif mail.html_content.present?
                   mail.html_content[:reply]
                 end

  attachments = mail.attachments.last(Message::NUMBER_OF_PERMITTED_ATTACHMENTS)
  inline_attachments = attachments.select { |attachment| attachment[:original].inline? && mail_content.present? }
  regular_attachments = attachments - inline_attachments

  regular_attachments.each do |mail_attachment|
    attachment = message.attachments.new(
      account_id: conversation.account_id,
      file_type: 'file'
    )
    attachment.file.attach(mail_attachment[:blob])
  end

  message.save!
end
```
</details>
2024-11-04 10:25:01 +01:00
Sojan Jose
7968e98529 chore: Stop processing auto-response emails (#9606)
Stop processing auto-response emails
https://www.notion.so/chatwoot/Avoid-Auto-Replies-sorcerer-s-apprentice-mode-55ffb09efbd7451994f1ff852de4c168?pvs=4
2024-06-13 14:19:11 -07:00
Sojan Jose
4284c123a6 chore: Handle invalid email address in IMAP channel (#9450) 2024-05-10 08:55:26 +05:30
Shivam Mishra
c62c512ec4 fix: inline attachments not handled if tag was missing (#9068) 2024-03-06 17:15:29 +05:30
Shivam Mishra
c031cb19d2 fix: downcase email before finding (#8921)
* fix: downcase email when finding

* feat: add `from_email` class

* refactor: use `from_email`

* feat: add rule to disallow find_by email directly

* chore:  remove redundant test

Since the previous imlpmentation didn't do a case-insentive search, a new user would be created, and the error would be raised at the DB layer. With the new changes, this test case is redundant

* refactor: use from_email
2024-02-21 18:51:00 +05:30
Vishnu Narayanan
d53097f77d fix: Raise error if email to_header is invalid (#8688) 2024-02-20 17:03:39 -08:00
Shivam Mishra
33a6ad9f7e chore: add more logging to mailbox helpers [CW-3071] (#8909)
* chore: add more logging to mailbox helpers

* fix: deleted entries

* fix: log order

* refactor: log using `processed_mail.message_id`
2024-02-13 08:51:50 +05:30
Vishnu Narayanan
3c952e6a4a fix: change email conversation not found exception to log (#8785)
* fix: change email conversation not found exception to log

* chore: refactor reply_mailbox methods

---------

Co-authored-by: Sojan Jose <sojan@pepalo.com>
2024-01-25 22:36:02 +05:30
Vishnu Narayanan
1c44445088 chore: Update the error message to the correct one (#8646)
-  Update the error message in reply_mailbox to more appropriate one.
2024-01-11 17:38:38 -08:00
Sojan Jose
aaf4fee9a6 chore: Fix sentry errors in email processing for bounce notifications (#8677)
This case occurs for bounce notification emails where the from address is From: "" (Mail Delivery System) . We will be discarding these emails for now.

Fixes: https://linear.app/chatwoot/issue/CW-2793/activerecordrecordinvalid-validation-failed-email-invalid-email
2024-01-10 14:30:23 -08:00
Shivam Mishra
e529e1206e fix: Update inline image processing logic to fix missing images when multiple inline images present (#7861) 2023-09-06 14:35:19 +05:30
Pranav Raj S
6ddc99d066 fix: Update the in_reply_to to logic to use processed_mail (#7793) 2023-08-24 16:19:17 +05:30
Tejaswini Chile
c99d9f9557 fix: search for nil in-reply-to messages (#7286) 2023-06-12 16:01:56 +05:30
Tejaswini Chile
f64f2138db fix: Update mail check for html_part (#7273) 2023-06-09 16:08:40 +05:30
Tejaswini Chile
aae6081d73 fix: check content disposition, for inline messages in mail (#7231) 2023-06-05 20:28:32 +05:30
Tejaswini Chile
09971fd613 fix: Wrap references string into array (#7243) 2023-06-03 07:33:36 +05:30
Tejaswini Chile
abc27fa791 fix: find mail message by references (#7220) 2023-05-31 19:23:29 +05:30
Tejaswini Chile
354010a6e1 chore: fetch mails with multiple attachments (#7030) 2023-05-14 10:02:36 +05:30
Sojan Jose
022383d942 chore: Upgrade to Rails 7 (#6719)
fixes: #6736
2023-05-06 10:44:52 +05:30
Jordan Brough
66cb0ee865 chore: Refactor code in ApplicationMailbox (#5857)
Refactor code in ApplicationMailbox

* short-circuiting as soon as we get a "true" value in some cases
* using ".exists?" instead of instantiating an ActiveRecord object
* using "match?" instead of "match"
2023-01-17 17:30:03 +05:30
Sojan Jose
12cd15b6ad chore: Disable email processing for suspended accounts (#5762)
- Disable email processing for suspended accounts
2022-10-26 03:40:47 -07:00
Sojan Jose
e310230f62 chore: Refactor Contact Inbox Builders (#5617)
- Remove duplicate code and move everything to builders
- fixes: #4680
2022-10-13 15:12:04 -07:00
Jordan Brough
1bdd59f025 Find by downcased email in SupportMailbox (#5211) 2022-10-12 13:38:18 +05:30
Jordan Brough
59b31615ed chore: Use "create!" and "save!" bang methods when not checking the result (#5358)
* Use "create!" when not checking for errors on the result
* Use "save!" when not checking the result
2022-09-13 17:40:06 +05:30
Aswin Dev P.S
19c637eb33 chore: Fix sentry issues (#4940)
Fixes: #4810
2022-07-08 15:09:06 +05:30
Aswin Dev P.S
9015d83679 chore: Fix sentry issues (#4863)
Fix sentry issues.

Fixes #4815, #4814, #4811, #4809
2022-06-15 16:20:19 +05:30
Tejaswini Chile
57359be37e Fix: Find mailbox with cc email (#4372) 2022-04-08 11:20:19 +05:30
Sojan Jose
467f3b9191 chore: Disable fetching new emails after mailbox error (#4176)
- Disabled email fetch job if credentials for the channel isn't working
- notify customers when the email channel isn't working

fixes: https://github.com/chatwoot/chatwoot/issues/4174
2022-03-22 12:14:17 +05:30
Tejaswini Chile
1467a8fa33 Fix: parse verification mail (#3864)
Email parsing logic was stripping of HTML tables which was causing the issue in this case.

Fixes: #3731
2022-01-27 15:45:26 -08:00
Tejaswini Chile
c57c975a0d bug: NoMethodError: undefined method `match' for in_reply_to (#3641)
Fixes #3615
2021-12-22 22:33:18 +05:30
Tejaswini Chile
c2519ea1ea Fix: fixing mail to and in_reply_to issues (#3451) 2021-12-10 19:42:26 +05:30
Aswin Dev P.S
24e6a92297 feat: IMAP Email Channel (#3298)
This change allows the user to configure both IMAP and SMTP for an email inbox. IMAP enables the user to see emails in Chatwoot. And user can use SMTP to reply to an email conversation.

Users can use the default settings to send and receive emails for email inboxes if both IMAP and SMTP are disabled.

Fixes #2520
2021-11-19 11:52:27 +05:30
Sojan Jose
acb06e7df6 chore: Prevent notification email loop (#3386)
Configuring an agent email also as a support email inbox leads to conversations getting created in a loop if notifications were also configured to the same email.
2021-11-15 19:15:51 +05:30
Pranav Raj S
99abbb8158 feat: Display sent status of emails in email channel (#3125) 2021-10-14 12:55:46 +05:30
Tejaswini Chile
6998e9aa2d fix: Update email message_id parsing order (#3073)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
2021-10-12 20:46:00 +05:30
Tejaswini Chile
6ad5a7452c fix: Emails not delivered when case does not match
Fixes #2504
2021-09-17 22:14:39 +05:30
Tejaswini Chile
a0ffefad71 chore: Use the name of the sender from the mail object
if the sender email is Sony Mathew <Sony@chatwoot.com> Contact should be built with Sony Mathew

Fixes #2911
2021-09-16 13:26:52 +05:30
Tejaswini Chile
09e3413d10 chore: consider X-original-sender to create contact in case of group mail (#2841)
For emails forwarded from google groups, Google rewrites the FROM address to the group email and the original email will be available under X-Original-Sender. This PR enables chatwoot to handle this case.

Fixes: #2715
2021-08-24 14:18:08 +05:30
Sojan Jose
ab54d9c629 chore: Upgrade rails and ruby versions (#2400)
ruby version: 3.0.2
rails version: 6.1.4
2021-08-03 20:11:52 +05:30
Pranav Raj S
5aac2acf56 fix: Improve mail content parsing (#2638) 2021-07-15 22:50:32 +05:30
Sojan Jose
c453455ad1 fix: Handle application errors in Sentry (#1982)
- Handle notification errors for attachment messages
- Fix empty identifiers being passed
- Fix 404 when invalid source id
- Handle webhook exceptions
2021-03-27 12:27:48 +05:30
Sojan Jose
ca4a766b82 chore: Enable email channel (#1851) 2021-03-04 13:59:59 +05:30
Pranav Raj S
d758df8807 fix: Add correct thread message_id to the email message (#1659)
Co-authored-by: Sojan <sojan@pepalo.com>
2021-01-17 22:43:32 +05:30
Sony Mathew
f9b0427751 feat: email channel backend (#140) (#1255)
* feat: added support mailbox to handle email channel (#140)

Added a new mailbox called 'SupportMailbox' to handle all the
incoming emails other than reply emails.

An email channel will have a support email and forward email
associated with it. So we filter for the right email inbox based on
the support email of that inbox and route this to this mailbox.

This mailbox finds the account, inbox, contact (create a new one
if it does not exist) and creates a conversation and adds the
email content as the first message in the conversation.

Other minor things handled in this commit:

* renamed the procs for routing emails in application mailbox
* renamed ConversationMailbox to ReplyMailbox
* Added a fallback content in MailPresenter
* Added a record saving (bang) versions of enabling and disabling
features in Featurable module
* added new factory for the email channel

refs: #140
2020-09-21 22:44:22 +05:30