Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to pull multiple email recipients in ActionMailer - Rails

Posted on 2009-12-21
6
Medium Priority
?
798 Views
Last Modified: 2013-11-13
trying to pull email addresses from database:

#subscriptions_controller
def deliver
    @subscription = Subscription.find(params[:id])
    @users = @subscription.users
    @recipients = @users.find(:all).collect { |user| user.email }
    @subscription.deliver
    flash[:notice] = "Delivered Subscription"
    redirect_to subscriptions_url
  end

#subscription.rb
def deliver
  UserMailer.deliver_lotto_saturday_subscription(@user, @subscription)
  update_attribute(:schedule_delivery, Time.now)
end

#user_mailer.rb
class UserMailer < ActionMailer ::Base

def lotto_saturday_subscription(user, subscription)
  recipients  ????????
  from        "lottomail.net"
  subject     "Your Lotto Saturday Reminder"
  body       :user => user
end

0
Comment
Question by:depassion
  • 3
  • 3
6 Comments
 
LVL 14

Accepted Solution

by:
wesgarrison earned 2000 total points
ID: 26096666
You need to pass the variables to the deliver method.  Instance variables aren't passed to models like they are to views.

I didn't see that you were creating @user anywhere, so I took it out.  If you need it in the template, just pass it along like that the recipient addresses.
#subscriptions_controller
def deliver
    @subscription = Subscription.find(params[:id])
    @users = @subscription.users
    @recipients = @users.find(:all).collect { |user| user.email }
    @subscription.deliver(@recipients)
    flash[:notice] = "Delivered Subscription"
    redirect_to subscriptions_url
  end

#subscription.rb
def deliver
  UserMailer.deliver_lotto_saturday_subscription(recipients)
  update_attribute(:schedule_delivery, Time.now)
end

#user_mailer.rb
class UserMailer < ActionMailer ::Base

def lotto_saturday_subscription(to_addresses)
  recipients  to_addresses
  # bcc         to_addresses   # if you want to hide the email addresses
  from        "no.reply@lottomail.net"
  subject     "Your Lotto Saturday Reminder"
  body       
end

Open in new window

0
 

Author Comment

by:depassion
ID: 26097686
Hi Wes,thanks for answering. I get the following error:


Processing SubscriptionsController#deliver (for 127.0.0.1 at 2009-12-22 01:21:49) [POST]
  Parameters: {"authenticity_token"=>"VqXdHq+XSM0LAjjkuxau3g/rwUz6aGUyk9xzEk906Gs=", "id"=>"1"}
  Subscription Columns (1.6ms)   SHOW FIELDS FROM `subscriptions`
  Subscription Load (0.2ms)   SELECT * FROM `subscriptions` WHERE (`subscriptions`.`id` = 1)
  subscriptions_users Columns (1.2ms)   SHOW FIELDS FROM `subscriptions_users`
  User Load (1.0ms)   SELECT * FROM `users` INNER JOIN `subscriptions_users` ON `users`.id = `subscriptions_users`.user_id WHERE (`subscriptions_users`.subscription_id = 1 )
  User Columns (0.9ms)   SHOW FIELDS FROM `users`

ArgumentError (wrong number of arguments (1 for 0)):
  app/controllers/subscriptions_controller.rb:12:in `deliver'
  app/controllers/subscriptions_controller.rb:12:in `deliver'

Rendered rescues/_trace (56.5ms)
Rendered rescues/_request_and_response (0.5ms)
Rendering rescues/layout (internal_server_error)
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 26097704
Whoops:
#subscription.rb
def deliver(recipients)
  ....

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:depassion
ID: 26097715
0
 

Author Comment

by:depassion
ID: 26097742
Thats it, thanks!
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 26097758
Sorry, I was just typing off the cuff, not actually running it, and I changed that method.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question