Skip to content

Bug RocketChat Email Inbox issue with mutated blocks in db even upon failure #39442

@Kaustubh2k5

Description

@Kaustubh2k5

Description:

In the file located at apps/meteor/server/email-inbox/EmailInbox_Outgoing.ts . Before the email is confirmed sent. If SMTP fails, the message in the DB already shows the To: / Subject: context blocks as if the email was sent , but it was never delivered. So the message record in the database is updated to reflect a sent state regardless of whether the email was actually delivered.

  • Bug Report
  • Refactor Required

Steps to reproduce:

Prerequisites:

  1. A running RocketChat instance (tested on 8.3.0-develop)
  2. An SMTP test server running on localhost:1025 (e.g. maildev)
  3. Dovecot or equivalent IMAP server on localhost:143
  4. An Email Inbox configured in Admin then Email Inboxes with:
  •                 SMTP server: localhost, port: 1025, secure: false
    
  •                 IMAP server: localhost, port: 143, secure: false
    
  •                 Inbox active: true
    

reproduction:

Drop a raw email into the IMAP mailbox to trigger onEmailReceived:

sudo bash -c 'cat >> /var/mail/imaptest << EOF
From visitor@test.com Sat Mar 07 18:00:00 2026
From: visitor@test.com
To: test@example.com
Subject: Bug Test Room
Message-ID: <test-bug-001@test.com>
MIME-Version: 1.0
Content-Type: text/plain


This is the bug test email
EOF'
  1. Wait for RC to poll IMAP and create the room. Confirm in agent view
  2. room info must show Channel: Email not Channel: Livechat. If it shows Livechat, the room is not email-sourced and the bug will not trigger.
  3. Accept the conversation as an agent.
  4. Confirm SMTP is working by sending a test reply , verify if it arrives in MailDev UI at http://localhost:1080.
  5. Stop the SMTP server to simulate failure:
pkill -f maildev
  1. In the agent view, hover over the incoming email message then click Reply then type any text then hit Send.

  2. Observe in the RC server logs:

ERROR (EmailInbox): Error sending Email reply
  message: "connect ECONNREFUSED ::1:1025"
  code: "ESOCKET"
  1. Also observe the rocket.cat error message appearing in the room:
@agent something went wrong when replying email, sorry. Error: connect ECONNREFUSED
// Using Node.js mongo client
const msgs = await db.collection('rocketchat_message')
  .find({ rid: '<roomId>' },{ projection: { msg: 1, blocks: 1, _updatedAt: 1 } }).toArray();

Expected behavior:

since the email was never delivered, the message state in DB should remain unchanged from its pre-send state. The agent should be able to safely retry.

Actual behavior:

The replied message has blocks written with To: / Subject: context despite SMTP failure. The DB record reflects a "sent" state that never happened:

Server Setup Information:

  • Version of Rocket.Chat Server: 8.3.0-develop
  • License Type: Community (dev build)
  • Number of Users: 1 (local dev)
  • Operating System: Linux Debian bookworm (x86_64)
  • Deployment Method: Source / yarn dev (Meteor development server)
  • Number of Running Instances: 1
  • NodeJS Version: 22.16.0
  • MongoDB Version: 7.0.16 (wiredTiger engine)

Client Setup Information

  • Desktop App or Browser Version: Chrome 145.0.0.0
  • Operating System: Linux

Additional Issues Found :

Dead Code Double if (!inbox) Check

File: EmailInbox_Outgoing.ts afterSaveMessage callback
The second if (!inbox) block (line 165) is unreachable the first block at line 155 already returns if inbox is false.

if (!inbox) {
  await sendErrorReplyMessage(...);
  return message;         // always returns here
}

if (!inbox) {             // DEAD CODE, never reached
  return message;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions