# Bug OP-8116 - Inbound Emails - Email headers not handled correctly

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-8116

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | High |
| Module |  |
| Target versions | 11.2.1 |
| Observed in versions |  |
| Sprint |  |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers |  |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### **Environment**:

Your OpenProject Version: 11.1.1

Linux Debian 10 / Google Chrome / OpenProject language: English

### **Logs**

There are no logs for this functionality (or at least I haven't seen them). 

### **Steps to reproduce:**

  - Add a comment to a work package and include a mention of a user, and save the comment.
  - When the user receives the mail, he checks the headers:

<!-- end list -->

``` text
Message-ID: <openproject.aggregated_journal-9-203.20210106165930@localhost.com>>
References: <openproject.work_package-9-49.20210106150218@localhost.com>>
...
X-OpenProject-Project: test-project
X-OpenProject-Issue-Id: 49
X-OpenProject-Issue-Author: bentey
X-OpenProject-Type: WorkPackage
X-OpenProject-Issue-Assignee: bentey
X-Mailer: OpenProject
X-OpenProject-Host: localhost
X-OpenProject-Site: OpenProject
```

  - The user replies to the email, and modifies the subject (remove \# and the number) to not include the ID, and make sure that OpenProject will not include the comment by the subject ID but by the header. Then send the answer with a comment.
  - We check the sent mail and see these headers:

<!-- end list -->

``` text
References: <openproject.work_package-9-49.20210106150218@localhost.com>
 <openproject.aggregated_journal-9-203.20210106165930@localhost.com>>
In-Reply-To: <openproject.aggregated_journal-9-203.20210106165930@localhost.com>>
```

  - OpenProject processes the email but creating a new WP.

#### Source of problem after testing

The current R\_egExp\_ is checking a different text format. While the header contains a text string with this format: `openproject.###_###-#-##` the current expression expects to find this other format `openproject.###_###-##`. For this reason the `if` statement in line 137 never returns true.

If we correct this with the change proposed in *Listing-1* the next problem is on line 136. As the process `flatten.compact` starts with `in_reply_to` and then with `references`... the variable `headers` ends with these values (in this order):

`openproject.aggregated_journal-9-203.20210106165930@localhost.com`  
`openproject.work_package-9-49.20210106150218@localhost.com`  
`openproject.aggregated_journal-9-203.20210106165930@localhost.com`

The `if headers.detect` starts with the first string and *(now, after apply the change proposed in Listing-1)* the format matches, so it returns **true**. But then there is no `receive_aggregated_journal_reply` method. To correct this I have changed the order of the process `flatten.compact` starting with `references`.

Now **1)** the text string `openproject.work_package-9-49.20210106150218@localhost.com` returns **true** in `if headers.detect`, and **2)** there is a method called `receive_work_package_reply` which finishes processing the email correctly adding the response to the existing WP.

**Listing 1.** RegExp to check the headers - app/models/mail\_handler.rb (131)

``` ruby
(Current) MESSAGE_ID_RE = %r{^<?openproject\.([a-z0-9_]+)\-(\d+)\.\d+@}
...
(New)     MESSAGE_ID_RE = %r{^<?openproject\.([a-z0-9_]+)\-(\d+)-(\d+)\.\d+@}
```

<figure class="image op-uc-figure" style="width:49.19%;"><div class="op-uc-figure--content"><img class="op-uc-image" src="/api/v3/attachments/20222/content"></div><figcaption class="op-uc-figure--description">Image 1. Proposed RegExp to match the current headers</figcaption></figure>

<figure class="image op-uc-figure" style="width:33.66%;"><div class="op-uc-figure--content"><img class="op-uc-image" src="/api/v3/attachments/20223/content"></div><figcaption class="op-uc-figure--description">Image 2. String groups that match with the proposed RegExp</figcaption></figure>

**Listing 2.** Order when saving headers - app/models/mail\_handler.rb (136)

``` ruby
(Current) headers = [email.in_reply_to, email.references].flatten.compact
...
(New)     headers = [email.references, email.in_reply_to].flatten.compact
```

**Listing 3.** Obtaining the ID of the destination WP - app/models/mail\_handler.rb (139)

``` ruby
(Current) object_id = $2.to_i
...
(New)     object_id = $3.to_i
```

### **Actual Behavior**

Create a new WP.

### **Expected Behavior**

Update the specified WP adding the new comment.
