How can I bulk remap/update user email addresses?

(harvey) #1

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

The problem is I tried to change my domain from to (all staff members are on email address). The forum migrated fine. But all my staff’s (admin/moderator) login email addresses became !

(I was fortunately spared, as my login was, so it migrated to, 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 email address and change their email, but I realized as part of security measure, a confirmation email is actually sent to to confirm the change. But obviously any email to 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.

(Jeff Atwood) #2

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

(harvey) #3

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

My email went from to

The domain migration impacts login emails. And since we were trying to migrate from to 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.

(harvey) #4

@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.

(Jeff Atwood) #5

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

(Arpit Jalan) #6

Can you please let me know the exact command you ran to remap to ?

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

(harvey) #7

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:

I may have typed in

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.

(Jeff Atwood) #8

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.

(Arpit Jalan) #9

That is… very risky.

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

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
    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')

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

    if new_email != = new_email
      putc "."
      i += 1

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

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['','']

  • remove the code appended in step 4

  • :tada:

(harvey) #10

To be clear, you meant to write:

Correct? Just wanted to double check to make sure I’m not missing anything. I guess restoring it to is fine as well. We have email access to both domains.

(Arpit Jalan) #11

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

(harvey) #12

That worked like a charm, you are a life saver.:confetti_ball:

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 email address, rather than from (the newly migrated)

I don’t mind if the emails come from, 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.

(Arpit Jalan) #13

Awesome! :thumbsup:

I think you are looking for notification email site setting. :slight_smile:

(harvey) #14

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.

I’ve specified notification email to be ` But I only have (and NOT 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.