Additional email address per user account support

Where did you get that create! would delete something? :wink:

Here’s how to delete all secondary emails for a user

UserEmail.where(user_id: 1234, primary: false).destroy_all

Thanks I’ll give it a shot!

Worked great. Thanks for the help Regis!

1 Like

@zogstrip could you please add secondary email address to the user export, like you did for staged? That was super handy! Feature request: add STAGED field to export user list to csv

It’ll be helpful for admins to be able to see what’s in this field and deal with it as needed. For my use case, I would like to see who has a secondary email set and remove it so I don’t run into trouble anymore with SSO and users with secondary emails.


This was very useful!
I needed to replace all staged users email addresses with another domain/tld, but keep the originals as secondary email.
This allows mbox importing script to recognize them from the secondary email but discourse sends notifications on the primary email.

Hope someone will find it useful.

UserEmail.where("email LIKE '%@domain.tld'").where(primary: true).find_each do |ue|
    new_mail =$/, '@newdomain.tld')
    next if UserEmail.exists?(email: new_mail)
    UserEmail.where(email: new_mail)
    UserEmail.create!(user_id: ue.user_id, email:

Sure thing and while in there I also fixed a bug with the escape_comma method


Much as I like the idea of the secondary email address, I am finding it to be a burden when managing my community and wishing there were a way to disable it.

am I right that to wipe out all secondary emails I would use the following?

UserEmail.where(primary: false).destroy_all

Why specifically is it a “burden”?


It is a burden for a few circumstances that keep coming up that take up too much time and brain space to figure out:

  • we invite people to join using one address, creating a staged user account, but then they actually end up joining using another email address. Or we find they already had another account using a different email.
  • we invite people with dormant accounts to come back, and instead of logging in with their existing account they create a new account with another email address

In these cases, we merge the accounts and then change the email address to the newer one that the people want to use. We use WordPress SSO and change the email address in WordPress, which then updates it in discourse. If the email address we want to change it to is already contained in the secondary email field in discourse for the user, then we have to delete it first otherwise this step fails silently.

We have also had cases of people coming to create a new account after we merged their user accounts, using the secondary email address. This does not work with our current SSO setup and fails silently.

So… there are some technical challenges to work out, and I’m not sure it’s a priority on your end and I know it’s a backburner priority on my end to improve how wordpress and discourse play with each other. I know I am also pushing the envelope in how staged users are intended to be used.

In the meantime I’d be happy if I could just disable the functionality. If it’s not possible, I will just get in the habit of also clearing the secondary email every time I merge users.


Sorry if I’m in the wrong place, but I’m trying to add a secondary e-mail address to my existing account and can’t find a way to do it in the web interface. The best thing I find is a forum with a bunch of Ruby code and what even is happening.

That’s right, the secondary email is only set if accounts are merged.

You’re going to need to resort to the rails console if you want to add a secondary email address.


Howdy Jon!

You are certainly in the right place. Is this for dotnetfoundation? Can you email, we will get it sorted for you!


Yes, fantastic. I have set this up and it is working just fine for me. Thank you!

Looking forward to see the web UI for this at some point :rocket:


Very happy this has been integrated and excited to try it out…

Sorry to revive an old post, but haven’t found anything else relevant.

I merged a user’s new account to their old account because their old account’s email address is no longer valid.
Their secondary email is now the valid email address but I need to change it to the primary so they can get email and reset password, etc.

I’ve tried deleting the secondary email and using the create command in rails c to re-add that valid email as their primary. I think I’m missing a step - do I need to save in rails c after I’m done?
After a successful delete/create command, I just put in exit, exit again and then launcher restart app.

What am I missing?

1 Like

I think we are missing some UX here, we have plans to improve it in the next release.

You are going to need to use the console to fix this up. Not exactly sure what commands you are going to need to run, maybe @LeoMcA can help.


I did the following:

./launcher enter app
rails c
UserEmail.where(user_id: 7, primary: false).destroy_all
UserEmail.create!(user: User.find_by_username("user7"), email: "")
./launcher restart app

I don’t know if this is what I did wrong, but I only ran the delete line this time. Restarted the app, and refreshed the website. The secondary email was gone! So I went ahead and did the rest on the web UI and changed the primary to secondary as it is no longer a block.

I take that back, changing the email via the web sends a confirmation to the valid email address, but retains the invalid email address once you land back to the user’s preference page or user admin page.
Now I’m not sure what to do :frowning:


At the risk of spamming this thread, I figured it out.

Once in rails c
I removed all the emails - I just didn’t know how to set an email as Primary, that’s why everytime I re-add the valid email, it just gets added as a secondary email.

# Remove all secondary emails
UserEmail.where(user_id: 1234, primary: false).destroy_all
# Remove primary email
UserEmail.where(user_id: 1234, primary: true).destroy_all

# Set primary email
UserEmail.create!(user_id: 1234, email: "", primary: true)

This goes without saying, it skips validation, but you’ve humanly validated it from the merged account previously so hopefully other folks can use this example. Only thing left to do is send the password reset email once you’ve restarted the app.


Hi Leon! This procedure is certainly not for the faint of heart and it looks like you’re on the right path. It’s definitely true that there’s some UX that would be handy here to manage the primary and secondary email addresses of users.This link might help you - it helped me!


This is complete as of the last release or two.