# Script for sending welcome message in background

**URL:** <https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879>\
**Category:** Product Support\
**Created:** [January 24, 2025, 7:06pm UTC](https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879 "2025-01-24T19:06:19Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [January 24, 2025, 7:47pm UTC](https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879/2 "2025-01-24T19:47:23Z")

</div>

[ask.discourse.org](http://ask.discourse.org) output a good starting point, as a rails script:

```rb
MAX_MESSAGES = 1000 # Optional limit for testing
SECONDS_BETWEEN_MESSAGES = 3 # 3 second interval

Rails.logger.info "Starting bulk message script..."

# Define the content of your message
message_title = "Welcome to Our Community"
message_body = "Hello! We're so excited to have you here. Let us know if you have any questions!"

# Fetch users ordered by last seen
users = User.where(active: true)
            .where.not(staged: true) # Exclude staged users
            .order(last_seen_at: :desc)
            .limit(MAX_MESSAGES)

Rails.logger.info "Found #{users.count} active users to message."

users.each_with_index do |user, index|
  begin
    Rails.logger.info "Queuing message for #{user.username} (#{index + 1}/#{users.count})..."

    PostCreator.create!(
      Discourse.system_user,
      title: message_title,
      raw: message_body,
      archetype: Archetype.private_message,
      target_usernames: user.username,
      skip_validations: true
    )

    Rails.logger.info "Message successfully queued for #{user.username}."

    # Wait for the specified interval before processing the next user
    sleep(SECONDS_BETWEEN_MESSAGES)
  rescue => e
    Rails.logger.error "Failed to send message to #{user.username}: #{e.message}"
  end
end

Rails.logger.info "Bulk message script completed!"

```

I didn’t do any modification, and the user `system` sent the MP to all my users in my test instance:

 ![The image shows a messaging system with an inbox section containing a single email thread labeled "Welcome to Our Community" with multiple perspective icons and timestamps. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/0/5/5/055c475da65737f08cb36a423d8e17e3d074a96c.png)  
 ![The image is a screenshot of a community welcome message along with an option to add or remove a camera overlay filter, with 1 view and a timestamp of 3 minutes. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/4/7/1/47133e4ab62be7e5df4485ab787fdf1a4ea56081.png)

How many users do you have?

---

_[View the full topic](https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879)._
