# How can I bulk remap/update user email addresses?

**URL:** https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519
**Category:** Support
**Created:** [1월 28, 2017, 3:56오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519 "2017-01-28T15:56:29Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 28, 2017, 3:56오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/1 "2017-01-28T15:56:29Z")

</div>

I followed the instructions here to change domain (it worked fine):

> [@Change the domain name or rename your Discourse](https://meta.discourse.org/t/change-the-domain-name-or-rename-my-discourse/16098):
>
> bookmark This guide explains how-to change the domain name of your Discourse site, and includes information about setting up DNS changes, editing configuration files, updating site settings, and correcting social logins and post content to reflect your new domain name. warning This guide is for self-hosted Discourse instances that were installed using our [installation guide on GitHub](https://github.com/discourse/discourse/blob/main/docs/INSTALL-cloud.md). If you are [hosted by Discourse](https://www.discourse.org/pricing), please see [Configure your domain name for hosted Discourse](https://meta.discourse.org/t/configure-your-domain-name-for-hosted-discourse/21827) for specific …

The problem is I tried to change my domain from [forum.mysite.co](http://forum.mysite.co) to forum.mysite.com.au (all staff members are on user@mysite.com.au email address). The forum migrated fine. But all my staff’s (admin/moderator) login email addresses became user@mysite. **com.aum.au**!

(I was fortunately spared, as my login was @mysite.co, so it migrated to @mysite.com.au, which also happens to be my email anyway).

What do I do now?

I tried to change everyone’s email directly in admin\>user, but the system won’t let me change other people’s email. I was going to ask everyone to try to login with the .com.aum.au email address and change their email, but I realized as part of security measure, a confirmation email is actually sent to .com.aum.au to confirm the change. But obviously any email to .com.aum.au doesn’t get to anybody.

Any suggestions?

EDIT: the affected users can login with username/password. Problem is no way to change the affected email. Since a confirmation email is sent to original (now wrong) email address, which doesn’t get to anyone.

---

<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: [1월 28, 2017, 10:21오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/2 "2017-01-28T22:21:04Z")

</div>

> [@harveywun](#):
>
> user@mysite.com.aum.au

Are you sure this is the actual email or is this a typo? Can we see a screenshot of this in a user profile?

---

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 28, 2017, 11:30오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/3 "2017-01-28T23:30:31Z")

</div>

![](https://global.discourse-cdn.com/meta/original/3X/0/d/0dbeb1dbc6e30a52b09b8ee63e34c61a7f28c368.png)

@codinghorror It is certainly not a typo. My staffs’ (more than one) email went from user@site.com.au to user@site.com.aum.au

My email went from [myemail@site.co](mailto:myemail@site.co) to myemail@site.com.au

The domain migration impacts login emails. And since we were trying to migrate from [site.co](http://site.co) to site.com.au. It ‘replaced’ the string pattern in our login emails.

I can show you screenshots of my other colleague’s profile, but the issue is the same.

---

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 28, 2017, 11:37오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/4 "2017-01-28T23:37:02Z")

</div>

@codinghorror the affected users can login with their username/password, the issue is they cannot change the email, as by security design, a confirmation email is sent to the original (now wrong) email address, which is not getting to anyone.

---

<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: [1월 29, 2017, 1:03오전 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/5 "2017-01-29T01:03:08Z")

</div>

I see then this is an error on our end can you assist @techapj?

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [1월 29, 2017, 6:44오전 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/6 "2017-01-29T06:44:35Z")

</div>

> [@harveywun](#):
>
> The problem is I tried to change my domain from [forum.mysite.co](http://forum.mysite.co) to forum.mysite.com.au (all staff members are on user@mysite.com.au email address).

> [@harveywun](#):
>
> since we were trying to migrate from [site.co](http://site.co) to site.com.au. It ‘replaced’ the string pattern in our login emails.

Can you please let me know the _exact command_ you ran to remap `forum.mysite.co` to `forum.mysite.com.au` ?

I can easily create a new rake task which will batch remap/update user email address from `user@mysite.com.aum.au` to `user@mysite.com.au` but first I want to know the root cause of how this happened, to prevent it from happening in future.

---

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 29, 2017, 7:15오전 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/7 "2017-01-29T07:15:44Z")

</div>

Our discourse is hosted on digitalocean. We don’t use CDN, so I basically followed the instructions _exactly_.

Reading through the instructions again, the only part I could have screwed up is:

> [@Change the domain name or rename your Discourse](https://meta.discourse.org/t/change-the-domain-name-or-rename-your-discourse/16098/1):
>
> ./launcher enter app  
> discourse remap [talk.foo.com](http://talk.foo.com) [talk.bar.com](http://talk.bar.com)  
> rake posts:rebake

I may have typed in

> [@](#):
>
> discourse remap [foo.com](http://foo.com) [bar.com](http://bar.com)

Without the “talk” sub-domain (or in our case “forum” sub-domain). But then again, not everyone puts discourse in a sub-domain, so I would think this is still a bug that could happen to your other customers (and why remap a user’s email?).

Is there a command in ubuntu where I can get command history for that particular command line (after ./launcher enter app)? Then I can confirm this more definitely with you.

---

<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: [1월 29, 2017, 11:22오전 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/8 "2017-01-29T11:22:17Z")

</div>

Oh, I thought you were a customer, but you aren’t. Yeah, you’ll need to fix the bad mapping command you issued. We can’t help you directly, as you are not on our servers.

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [1월 29, 2017, 11:49오전 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/9 "2017-01-29T11:49:38Z")

</div>

> [@harveywun](#):
>
> Without the “talk” sub-domain (or in our case “forum” sub-domain).

That is.. very risky.

Here is the rake task you can run to fix this issue.

```ruby
desc 'Batch remap user email address'
task 'users:batch_remap_email', [:find, :replace] => [:environment] do |_,args|
  require 'highline/import'

  find = args[:find]
  replace = args[:replace]
  if !find || !replace
    puts "ERROR: Expecting rake users:batch_remap_email['find','replace']"
    exit 1
  else
    confirm_replace = ask("Are you sure you want to remap all email addresses matching '#{find}' with '#{replace}'? (Y/n) ")
    exit 1 unless (confirm_replace == "" || confirm_replace.downcase == 'y')
  end

  puts "Remapping email addresses"
  i = 0
  User.where("email LIKE ?", "%#{find}%").each do |u|
    new_email = u.email.dup
    new_email = new_email.gsub!(/#{Regexp.escape(find)}/, replace) || new_email

    if new_email != u.email
      u.email = new_email
      u.email_tokens.create(email: u.email)
      u.activate
      u.save
      putc "."
      i += 1
    end
  end
  i

  puts "", "#{i} email addresses remapped!", ""
end

```

If you are unfamiliar with how to use above rake code, here is the rough outline:

- login on your server

- `cd /var/discourse`

- `./launcher enter app`

- append the above code in `/lib/tasks/users.rake` file

- `rake users:batch_remap_email['mysite.com.aum.au','mysite.co']`

- remove the code appended in step 4

- 🎉

---

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 29, 2017, 1:22오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/10 "2017-01-29T13:22:07Z")

</div>

> [@techAPJ](#):
>
> rake users:batch\_remap\_email[‘mysite.com.aum.au’,‘[mysite.co](http://mysite.co)’]

To be clear, you meant to write:

> [@](#):
>
> rake users:batch\_remap\_email[‘mysite.com.aum.au’,‘ **mysite.com.au** ’]

Correct? Just wanted to double check to make sure I’m not missing anything. I guess restoring it to [mysite.co](http://mysite.co) is fine as well. We have email access to both domains.

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [1월 29, 2017, 1:27오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/11 "2017-01-29T13:27:36Z")

</div>

Yeah, that was just an example. What you wrote looks good and will fix the issue.

---

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 29, 2017, 1:43오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/12 "2017-01-29T13:43:43Z")

</div>

That worked like a charm, you are a life saver.🎊

While I have you, quick question - is there any configuration I have to do with mailgun (or mail server equivalent)? Right now the mails are still sent from mailgun with [mysite.co](http://mysite.co) email address, rather than from (the newly migrated) mysite.com.au.

I don’t mind if the emails come from [mysite.co](http://mysite.co), I’m more concerned as to whether there are any risk/unintended consequences if the domain names don’t match up between the forum and mailgun.

Thanks for the help, once again.

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [1월 29, 2017, 1:51오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/13 "2017-01-29T13:51:54Z")

</div>

> [@harveywun](#):
>
> That worked like a charm, you are a life saver. 🎊

Awesome! 👍

> [@harveywun](#):
>
> Right now the mails are still sent from mailgun with [mysite.co](http://mysite.co) email address, rather than from (the newly migrated) mysite.com.au

I think you are looking for `notification email` site setting. 🙂

---

<div class="post-metadata">

### Author: ![harveywun](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harveywun](https://meta.discourse.org/u/harveywun)
#### Post date: [1월 29, 2017, 2:13오후 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/14 "2017-01-29T14:13:38Z")

</div>

Yeah, i guess that’s the question. Under `notification email` :

[quote]The from: email address used when sending all essential system emails. The domain specified here must have SPF, DKIM and reverse PTR records set correctly for email to arrive.  
[/quote]

I’ve specified notification email to be `no-reply@mysite.com.au. But I only have [mysite.co](http://mysite.co) (and NOT mysite.com.au) with SPF, DKIM and reverse PTR records set correctly with mailgun.

I’m getting notification emails just fine. I just want to double check and make sure it is ok to leave it as is.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [1월 24, 2024, 12:47오전 UTC](https://meta.discourse.org/t/how-can-i-bulk-remap-update-user-email-addresses/56519/15 "2024-01-24T00:47:29Z")

</div>


