# IMAP sync stages new user if Group.incoming\_email is not set to Group.email\_username

**URL:** https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674
**Category:** Bug
**Created:** [February 6, 2021, 6:18pm UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674 "2021-02-06T18:18:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [February 6, 2021, 6:18pm UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674/1 "2021-02-06T18:18:29Z")

</div>

While [playing around with IMAP sync](https://meta.discourse.org/t/imap-support-for-group-inboxes/160588/18) a staged user was created for `Group.email_username` during import from the IMAP account.  
Trying to understand what happened I noticed that `Group.find_by_email` does not take `Group.email_username` into account requiring to set `Group.incoming_email` to include this value at least.

Repro steps:

- enable IMAP sync
- configure Email sync options on a group but leave `Group.incoming_email` unconfigured
- send a mail to `Group.email_username`

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 7, 2021, 8:08pm UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674/2 "2021-02-07T20:08:41Z")

</div>

I believe IMAP is still a bit of a work in progress @sam?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [February 8, 2021, 1:00am UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674/3 "2021-02-08T01:00:42Z")

</div>

It should be reasonable supported we do use it in production on meta.

@martin can you have a quick look at this report?

---

<div class="post-metadata">

### Author: ![martin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/martin/32/491371_2.png) [@martin](https://meta.discourse.org/u/martin)
#### Post date: [February 8, 2021, 3:44am UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674/5 "2021-02-08T03:44:36Z")

</div>

Thanks @thoka , I had a look into this. I think you are right in that `Group.find_by_email` needs to have the `email_username` added to it, but it is not the cause of the group email user getting staged in this case. This actually happens later on in the receiver where we add other addresses to the newly created topic. The important part here is `should_invite?`

> <https://github.com/discourse/discourse/blob/e9364856f5fb42d8a0fbc0862753f47e76287627/lib/email/receiver.rb#L1244-L1272>

> <https://github.com/discourse/discourse/blob/e9364856f5fb42d8a0fbc0862753f47e76287627/lib/email/receiver.rb#L1274-L1278>

Which in turn compares the email against `group_incoming_emails_regex`:

> <https://github.com/discourse/discourse/blob/e9364856f5fb42d8a0fbc0862753f47e76287627/lib/email/receiver.rb#L948-L950>

I think we will need to make it so in this regex, or another, that we compare against the `email_username` as well. So far we have not found it a problem that the staged user for the group is made, though I can see that it looks a little messy/unnecessary. I will need to do some testing when I change this to make sure the staged user for the group is not being made for some important currently unknown (to me) reason.

---

<div class="post-metadata">

### Author: ![martin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/martin/32/491371_2.png) [@martin](https://meta.discourse.org/u/martin)
#### Post date: [June 11, 2021, 1:30am UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674/6 "2021-06-11T01:30:10Z")

</div>

This was fixed by [FEATURE: Use group SMTP settings for sending user notification emails (initial) by martin-brennan · Pull Request #13220 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/13220), and the group email config UI has gotten a revamp recently. IMAP is still very much in an alpha state though, so use that feature at your own risk.

---

<div class="post-metadata">

### Author: ![martin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/martin/32/491371_2.png) [@martin](https://meta.discourse.org/u/martin)
#### Post date: [June 11, 2021, 1:30am UTC](https://meta.discourse.org/t/imap-sync-stages-new-user-if-group-incoming-email-is-not-set-to-group-email-username/178674/7 "2021-06-11T01:30:13Z")

</div>


