sreenivas
sreenivas

Reputation: 395

Send mail to all users

    class UserMailer < ActionMailer::Base
  default from: '[email protected]'

  def welcome_email(user)
    @user = user
    @url  = 'http://www.fixrnix.in'
    mail(to: @user.email , subject: 'Welcome to FixNix Audit Management')
  end
end

How can i send email to all users instead of single user mail(to: @user.email , subject: 'Welcome to FixNix Audit Management')

Upvotes: 0

Views: 1731

Answers (2)

BroiSatse
BroiSatse

Reputation: 44685

You can pass array of addresses as well:

def welcome_email(users)
  @url  = 'http://www.fixrnix.in'
  mail(to: users.pluck[:email] , subject: 'Welcome to FixNix Audit Management')
end

However, if your email body depends on the user or you don't want mailed users to see other users's addresses you can't do this. You need to create a new message for each user and send it separately.

users.each do |user|
  YourMailer.welcome_email(user).deliver
end

Upvotes: 8

LHH
LHH

Reputation: 3323

You can simply do this in your mailer

emails = users.collect(&:email).join(", ")
mail(to: emails, subject: 'Welcome to FixNix Audit Management')

Hope this helps!

Upvotes: 0

Related Questions