# כיצד ניתן לאשר באופן אוטומטי כתובת דוא"ל משנית?

**URL:** https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354
**Category:** Support
**Created:** [21 במרץ,‏ 2024,‏ 1:25pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354 "2024-03-21T13:25:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [21 במרץ,‏ 2024,‏ 1:25pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/1 "2024-03-21T13:25:41Z")

</div>

I’ve seen a few topics touch on this issue, but nothing quite seems to address it.

Basically, our company was purchased. This means all users are moving from @old\_company.com to @new\_company.com

I have a map of old to new email addresses. What I would like to do is add the @new\_company.com email address for a user and automatically confirm it. Otherwise they need to go through the confirmation emails themselves, which realistically won’t happen.

Is there any way I can add a secondardy email as admin and automatically confirm it?

Simon mentions the following

> [@Change Users email address using the API without sending the confirmation Email](https://meta.discourse.org/t/change-users-email-address-using-the-api-without-sending-the-confirmation-email/77606/2):
>
> If you are using SSO you can update the email address through the `/admin/users/sync_sso` route. When it’s done that way a confirmation email isn’t sent.

But it’s not clear to me how this works. How can it sync an email address to an account, if the address is not yet confirmed? Does this mean I should/could

1. add secondary email for user via api
2. (maybe) disable outgoing email for 10 minutes so no confirmation email is sent
3. enable `saml sync email`
4. user accounts confirm and update when they sign in with saml

_Note, we are also changing saml provider in this example_

Even though the secondary email has not been confirmed, does the above setup mean that when they try to log in with the email for the first time, it will be automatically confirmed and tied to the account?

---

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [26 במרץ,‏ 2024,‏ 9:54am UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/4 "2024-03-26T09:54:46Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/8/7/d/87de8ec5b8cabab54769f401f4967b8bbd865050.png)

Just as a note, one issue I misunderstood was the confirmation email. As admin you get two emails, one to confirm your old email, then a second to confirm the change.

I assumed this applied to all users, but with the setting in the image above, you can set it to apply to only staff. This means users would only get the one email to confirm the change.

Still need to figure out how to automatically confirm the email for the user though 🤔

---

<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: [26 במרץ,‏ 2024,‏ 12:12pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/5 "2024-03-26T12:12:18Z")

</div>

I’m not sure there is a way to automatically confirm through the UI/API. 🤔 I think if there’s SSO then the email/identity confirmation is handled by them? In which case using the sso\_sync would pull in the already confirmed data/email and use that as ‘trustworthy’.

* * *

A little bit of fact-checking later… 🙂

The `/admin/users/sync_sso` is only for [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true). I think you already knew that, but I’ll say it out loud for anyone reading this later.

But there’s also the `auth_overrides_emails` admin setting which may be useful for this.

Essentially, if your SAML provider is sending a verified email, and `auth_overrides_emails` is set, Discourse will start using the new email without any confirmation being emailed out. 👍

---

<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: [26 במרץ,‏ 2024,‏ 5:25pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/9 "2024-03-26T17:25:52Z")

</div>

This is not a direct answer to your question, but: In the case of mass-renaming of email-addresses, you could do it via rails console:

```ruby
o = "@old_company.com" 
n = "@new_company.com"
UserEmail.where("email LIKE ?","%#{o}").each do |ue| 
  ue.email.sub! o,n 
  ue.save! 
end

```

If you favor adding a secondary email address:

```ruby
o = "@old_company.com" 
n = "@new_company.com"

UserEmail.where("email LIKE ?","%#{o}").each do |ue|
   sm = UserEmail.new
   sm.user_id = ue.id
   sm.email = ue.email.sub! o,n
   sm.save!
end

```

---

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [27 במרץ,‏ 2024,‏ 1:47pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/10 "2024-03-27T13:47:25Z")

</div>

Thanks for the suggestions. I gave them a try in the following way:

## 1. Add secondary email to user via api

The email is unconfirmed, but tied to the account

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/3/a/e3a5a79536bb3799fafe32e5ae5d54133101efa2.png)

## 2. Configure the following settings:

![image](https://global.discourse-cdn.com/meta/original/4X/1/5/3/153df70975ed8f9226e2259400cc4c6fc6aa814d.png)

![image](https://global.discourse-cdn.com/meta/original/4X/8/a/3/8a3494c150d189aadf34146d04c0d57629579e3e.png)

![image](https://global.discourse-cdn.com/meta/original/4X/0/b/4/0b4a9f13b84369b9a59797aa614ac870700517d7.png)

- I forgot to set the saml sync to true, however I don’t think that would have affected the outcome. The issue still seems to be conflict in terms of the email confirmation.

## 3. Result

The result was the a new user account was created based on the new company email. Worst case I could merge these, but that is a really nasty worst case.

I can still see the unconfirmed email in the old account. I try to resend the confirmation just to see what would happen but I get a 403 error(Forbidden).

It looks like the email needs to be confirmed _first_ before we can syncronise them 😕

Unless there’s something I’m missing, it looks like I need a way to confirm the second email.

> [@thoka](#):
>
> This is not a direct answer to your question, but: In the case of mass-renaming of email-addresses, you could do it via rails console:

I wonder if this still has the same issue in terms of confirmation, or are they assumed to be confirmed? An additional complexity is that the user part of [user@company.com](mailto:user@company.com) has also changed. But I suspect that would mean we run through a csv of mappings between the old and new emails.

---

<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: [27 במרץ,‏ 2024,‏ 1:50pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/11 "2024-03-27T13:50:33Z")

</div>

I was more thinking of changing the email in the SSO, enabling `auth overrides emails`, and then logging into your account via your SSO as normal.

I may be missing something.

---

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [27 במרץ,‏ 2024,‏ 1:55pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/12 "2024-03-27T13:55:42Z")

</div>

> [@JammyDodger](#):
>
> I was more thinking of changing the email in the SSO, enabling `auth overrides emails`, and then logging into your account via your SSO as normal.

I think it might be doing this, in that it is validating a SAML account based on the email address and SAML server used to log in. The problem is that it’s a completely different SAML IDP (New Company).

Consequently it correctly over-rides/creates the email to be the SAML email, but it does this for a brand new account because the Discourse account is not associated to the new email address.

However, if the existing account has the _new_ saml email addres already confirmed in Discourse, then the login is smooth and it becomes their new saml login email.

---

<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: [27 במרץ,‏ 2024,‏ 2:14pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/13 "2024-03-27T14:14:02Z")

</div>

Hmmm. 🤔 I think I was maybe working on the assumption that the move to the new IDP would have mapped them across based on an external id of some sort.

> [@Tris20](#):
>
> I wonder if this still has the same issue in terms of confirmation, or are they assumed to be confirmed?

I think it is possible to activate/confirm the email through the rails console by adding in the token info too. Something like:

```ruby
old_domain = "<insert_here_the_old_domain>"
new_domain = "<insert_here_the_new_domain>"

users = UserEmail.where("email like '%" + old_domain + "'")

users.each do |user_email|
    user = User.find_by_id(user_email.user_id)
    user.email = user.email.gsub(old_domain, new_domain)
    user.email_tokens.create(email: user.email)
    user.activate
    user.save!
    puts "."
end

```

---

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [27 במרץ,‏ 2024,‏ 2:31pm UTC](https://meta.discourse.org/t/how-can-i-automatically-confirm-a-secondary-email-address/300354/14 "2024-03-27T14:31:49Z")

</div>

> [@JammyDodger](#):
>
> I think I was maybe working on the assumption that the move to the new IDP would have mapped them across based on an external id of some sort.

To the best of my knowledge this hasn’t happened. For another platform we needed to get a list of the emails ourselves and do the mapping.

> [@JammyDodger](#):
>
> I think it is possible to activate/confirm the email through the rails console by adding in the token info too.

It looks more and more like this is the way to proceed. We’re hosted by you guys so I’ll pass this on to our CSM and follow up on this topic with any progress.
