views:

703

answers:

1

Hi I am having trouble figuring out how to send email to a few different set groups with multiple recipients.

I have many users, each user has an 'Inbox' found with their login name so something like: /users/c230in_cc/messages. The user can then go to "New Message" to send a message. Emailing one person is not a problem, I have that working fine.

But now I need for the user to be able to pick to send an email to one of three set groups. "Primaries Only", "Spouses Only", "All Friends and Family."

I know the multiple recipients needs to be in an array. For example the first one "Primaries Only" would be user.company.primaries. (I need to know what company the user is in and then grab the primaries associated with that company.) I can access this through the message_controller but that doesn't help me too much.

So I need to: A. Have an option in the view to choose which group. B. Pass along the array of people in that group to the message_mailer.

Also one group needs to pull from several different models... is this possible?

I have tried a lot of different things, but I am new to rails and am generally just confused on how to accomplish this.

I would REALLLLYYYY appreciate any guidance. My code is below.

user.rb

class User < ActiveRecord::Base  
  belongs_to :company
  has_many :messages

message.rb

class Message < ActiveRecord::Base
  belongs_to :users

end

message_controller

def new
  @user = User.find_by_login(params[:user_id])
  @primary= @user.company.primaries
  @message = Message.new 
end

def create
  @user = User.find_by_login(params[:user_id])
  @message = Message.new(params[:message])
  @user = User.find_by_login(params[:user_id])
  @primary= @user.company.primaries

respond_to do |format|
  if @message.save
        flash[:notice] = 'Message was sent.'
        format.html { redirect_to user_messages_path(current_user)}

      else
        format.html { render :action => "new" }

      end
    end
  end

end

message_mailer.rb

def send_email(message)
   to 
   from message.from 
   subject message.subject  
   sent_on Time.now 
   body :message => message
end

message_observer.rb

def after_create(message)  
  MessageMailer.deliver_send_email(message) 
end
A: 

It's easiest if you just outsource this task to a service like Campaign Monitor

Jen
I love Campaign Monitor and I wish that were an option, but it is not. Thanks for the suggestion.