# Scripting to trigger "Welcome" email to new users

**URL:** https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900
**Category:** Support
**Created:** [January 13, 2016, 5:43pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900 "2016-01-13T17:43:21Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![optimus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/optimus/32/39715_2.png) [@optimus](https://meta.discourse.org/u/optimus)
#### Post date: [January 13, 2016, 5:43pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/1 "2016-01-13T17:43:22Z")

</div>

I’ll be deploying Discourse across a fairly large user base across different departments soon. I will also set up groups in Discourse corresponding to the departments (i.e. IT, Business, etc.) What I’d like to be able to do is programmatically send out custom “welcome” emails based on the group and age of account (i.e. created in past 7 days), with some helpful links or information specific to the deparment… So example I want to send out something like:

"Hi IT User,

Welcome to Discourse!

Here are some helpful links to read…"

I see there’s an existing “Welcome User” email template under Admin\Customize\Email Templates. How do trigger it programmamtically just to the new accounts with a given age?

TIA,  
Chris

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [January 13, 2016, 5:50pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/2 "2016-01-13T17:50:46Z")

</div>

That’s not possible. Discourse stores only a password hash. The real password (clear text) is unknown.

---

<div class="post-metadata">

### Author: ![optimus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/optimus/32/39715_2.png) [@optimus](https://meta.discourse.org/u/optimus)
#### Post date: [January 13, 2016, 5:51pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/3 "2016-01-13T17:51:40Z")

</div>

Ok how about without password then ;)? I’ve updated my post.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [January 13, 2016, 5:53pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/4 "2016-01-13T17:53:43Z")

</div>

What’s the use case for sending the username?  
The user can use his email address to login if he doesn’t remember his username. 😉

---

<div class="post-metadata">

### Author: ![optimus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/optimus/32/39715_2.png) [@optimus](https://meta.discourse.org/u/optimus)
#### Post date: [January 13, 2016, 6:33pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/5 "2016-01-13T18:33:46Z")

</div>

let’s just say some of my user’s like to see things spelled out ;). the more pressing part is wanting to script this out so I can send the welcome emails to new users when I choose. If you have any input about that please share.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [January 13, 2016, 6:36pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/6 "2016-01-13T18:36:00Z")

</div>

If your company already have some authentication system in place, use the SSO feature, and they won’t even need to know username and password 👍.

[Discourse SSO](https://meta.discourse.org/t/13045?silent=true) is very flexible, I got mine working in under a week, with full integration, including avatars.

---

<div class="post-metadata">

### Author: ![optimus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/optimus/32/39715_2.png) [@optimus](https://meta.discourse.org/u/optimus)
#### Post date: [January 13, 2016, 6:38pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/7 "2016-01-13T18:38:14Z")

</div>

I am just going to remove the mention of the account info. I just want to script out to trigger the welcome email :)…

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [January 13, 2016, 6:50pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/8 "2016-01-13T18:50:15Z")

</div>

Log into your Docker container and start the Rails console:

```
cd /var/discourse
./launcher enter app
rails c

```

Then execute something like this (_this is untested_):

```ruby
User.find_each {|user| user.enqueue_welcome_message('welcome_user')}

```

---

<div class="post-metadata">

### Author: ![optimus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/optimus/32/39715_2.png) [@optimus](https://meta.discourse.org/u/optimus)
#### Post date: [January 13, 2016, 6:51pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/9 "2016-01-13T18:51:51Z")

</div>

Thanks @gerhard. What would I need to change to only send to users with accounts created past a certain date?

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [January 13, 2016, 7:05pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/10 "2016-01-13T19:05:49Z")

</div>

Something like this should work:

```ruby
User.where('created_at >= ?', 7.days.ago).find_each {|user| user.enqueue_welcome_message('welcome_user')}

```

It sends the welcome message to all users that where created within the last 7 days.

---

<div class="post-metadata">

### Author: ![optimus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/optimus/32/39715_2.png) [@optimus](https://meta.discourse.org/u/optimus)
#### Post date: [January 13, 2016, 8:44pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/11 "2016-01-13T20:44:43Z")

</div>

Awesome, thanks @gerhard! That worked!

---

<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: [June 8, 2024, 12:45pm UTC](https://meta.discourse.org/t/scripting-to-trigger-welcome-email-to-new-users/37900/12 "2024-06-08T12:45:53Z")

</div>

This topic was automatically closed after 3068 days. New replies are no longer allowed.
