← Blog • Published: Monday, 23 Oct 2023
ActionMailer CC and BCC multiple recipients
This is a short article on using CC and BCC in your ActionMailer mailers, to send emails to multiple recipients. Rails makes this really easy, but this article also walks you through some more complex examples of using these methods.
When you're building a Rails app, there'll probably come a time when you want to send emails to multiple people.
This could be for notifications, alerts, or even newsletters. ActionMailer comes with a straightforward way to do this, specifically through the use of the cc:
(carbon copy) and bcc:
(blind carbon copy) parameters.
In this short article I'll show you how to use these methods to send emails to multiple recipients in your Rails apps. Let's go!
Basic Usage
ActionMailer mailers accept cc:
and bcc:
parameters in their #mail
method call.
This gives us a straightforward way to carbon copy (CC) or blind carbon copy (BCC) multiple recipients when sending emails, like this —
# app/mailers/welcome_mailer.rb
#
def welcome
mail(
to: "[email protected]",
cc: ["[email protected]", "[email protected]"]
bcc: ["[email protected]", "[email protected]"]
)
end
For a more real-world example, consider a Rails app that's a forum site. You might want to send out emails to a primary administrator, and CC in all the other secondary administrators.
You might do that like —
# app/mailers/admin_mailer.rb
#
def admin_update_email
secondary_admin_emails = Admin.where(role: "secondary").pluck(:email)
mail(
to: "[email protected]",
cc: secondary_admin_emails
)
end
For more information, you can also check out the official Ruby on Rails ActionMailer docs. I've linked directly to the information on the #mail
method, which includes information on all the headers you can pass it. This includes to:
, cc:
and bcc:
, and a couple of others.
More advanced example
Here's a slightly more advanced example — Suppose you have a multi-tenant application where each tenant has its own set of administrators.
You want to send an email to the primary admin of each tenant, CC the secondary admins for each tenant, and BCC the super admin of the entire app.
Here's how you might structure your #mail
method —
# app/mailers/admin_mailer.rb
#
def multi_tenant_admin_update
Tenant.all.each do |tenant|
primary_admin_email = tenant.admins.primary.first.email
secondary_admin_emails = tenant.admins.secondary.pluck(:email)
super_admin_email = "[email protected]"
mail(
to: primary_admin_email,
cc: secondary_admin_emails,
bcc: super_admin_email
)
end
end
In this example, we iterate through each tenant, gather the necessary email addresses, and then send out an email.
We've combined the cc:
and bcc:
parameters inside a single, more complex, mailer method.
Conclusion
Using the cc:
and bcc:
parameters with ActionMailer's #mail
method is an easy way to manage email recipients in a Ruby on Rails application.
They're a handy way to send emails to multiple recipients in your Ruby on Rails apps and are a handy feature of ActionMailer.