# Sending email to multiple recipients

**URL:** <https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498>\
**Category:** Rails\
**Created:** [December 29, 2007, 6:41am UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498 "2007-12-29T06:41:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 6:41am UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/1 "2007-12-29T06:41:51Z")

</div>

Hello,  
I have to send mail to multiple recipients. My table is named people  
and has  
first\_name,last\_name, email1 , email2, email3 for each person. i have  
put people in various mailinglist .For example first mailinglist where  
first\_name is john or say the second mailing list contains people of  
british origin.etc

the relation between mailinglist and people is many to many.  
params[:ml] gives me the id of the mailing list to which mail is to be  
sent.  
In the controller i have

def sendmail  
@mailinglists=Mailinglist.find\_all\_by\_id(params[:ml])  
@mailinglists.each do |ml|  
@people =  
Person.find(:all,:include=\>:mailinglists,:conditions=\>[‘mailinglist\_id=?’,"#{params[:ml]}"])  
UserMailer.deliver\_send\_mail(@people)  
end  
end

In the mailer model which i have named as user\_mailer i have

class UserMailer \< ActionMailer::Base  
def send\_mail(people)  
#----------------------------------------  
recipients people.email1

# this generates error undefined method email1

#---------------------------------------------------

```
from "removed_email_address@domain.invalid"
subject "Thank you This is a test"
body "This is a test"

```

end  
end

The recipients people.email1 generates an error undefined method email1  
.  
If i use the same code for person instead of people i.e if i send mail  
to only one person and pass person as a parameter as

def send\_mail(person)  
recipients person.email1  
…  
…  
then it works fine but i am able to send an email to only one person.  
Please tell me what am i doing wrong and how do i proceed.

Thank you.  
Regards,  
Ank

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 9:24am UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/2 "2007-12-29T09:24:21Z")

</div>

Am 29.12.2007 um 06:41 schrieb Ank Ag:

> params[:ml] gives me the id of the mailing list to which mail is to be  
> ,:include  
> =\>:mailinglists,:conditions=\>[‘mailinglist\_id=?’,“#{params[:ml]}”])  
> UserMailer.deliver\_send\_mail(@people)

@people.each do |p|  
UserMailer.deliver\_send\_mail(p)  
end

Perhaps this works.

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/3 "2007-12-29T13:03:42Z")

</div>

If you have thousands of email addresses then you must use ar\_mailer. It  
is  
asynchronous and will not impact the performance of your Rails app.  
Check  
out the Rails Way book for a good discussion on this issue.

On Dec 28, 2007 9:41 PM, Ank Ag [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,  
> I have to send mail to multiple recipients. My table is named people  
> and has  
> first\_name,last\_name, email1 , email2, email3 for each person. i have  
> put people in various mailinglist .For example first mailinglist where  
> first\_name is john or say the second mailing list contains people of  
> british origin.etc

–

> **[Collage | Collaborative real world learning](https://rubyplus.org/)**
>
> Collage is a community for collaborative, real world learning. Find a class in the San Francisco Bay Area, or start a learning circle in your neighborhood.

Free Ruby and Rails Screencasts

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/4 "2007-12-29T15:18:54Z")

</div>

On Dec 29, 2007 12:41 AM, Ank Ag [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In the mailer model which i have named as user\_mailer i have
> 
> class UserMailer \< ActionMailer::Base  
> def send\_mail(people)  
> #----------------------------------------  
> recipients people.email1
> 
> # this generates error undefined method email1
> 
> #---------------------------------------------------

> The recipients people.email1 generates an error undefined method email1

A little ruby should fix this.

Instead of

recipients people.email1

try

recipients people.map {|person| person.email1}

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/5 "2007-12-29T14:13:22Z")

</div>

On Dec 28, 2007, at 11:41 PM, Ank Ag wrote:

> Hello,  
> I have to send mail to multiple recipients. My table is named  
> people  
> and has  
> first\_name,last\_name, email1 , email2, email3 for each person. i have  
> put people in various mailinglist .For example first mailinglist where  
> first\_name is john or say the second mailing list contains people of  
> british origin.etc

Just a matter of opinion, but you might consider moving your email  
addresses to another table so you don’t have to limit your users to  
three. I like to use a construct called “contact number” which can  
be a phone number (landline or cell), fax, email, page, url, im,  
whatever. Then you can have as many as you want and iterate through  
them as a collection. But that’s just a matter of style and need.

> [‘mailinglist\_id=?’,“#{params[:ml]}”])
> 
> # this generates error undefined method email1
> 
> #---------------------------------------------------

recipients is an array, so you need to build all of the addresses as  
an array. The collection people does not have a method called  
“email1”, that is an attribute of each person object. So you’d need  
to do something like this

people.each do |person|

# this is more personal than just the email address, but that would

work too  
recipients \<\< “#{person.first\_name} #{person.last\_name} \<#  
{person.email1}\>”  
end

I don’t know about the performance of ActionMailer with thousands of  
recipients, so Bala’s advice might be your best bet. And I would not  
use Jochen’s advice (absolutely no offense intended, Jochen!) and put  
each delivery in a loop. In that case, you’d be sending a single  
message for every recipient instead of sending one message to your  
outbound server and letting it handle the multiple recipients.

Oh, one other thing: use bcc instead of recipients. It’s generally  
considered bad etiquette to send out a mass email and put everyone’s  
address in the to line.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/6 "2007-12-29T17:43:26Z")

</div>

Jochen K. wrote:

> Am 29.12.2007 um 06:41 schrieb Ank Ag:
> 
> > params[:ml] gives me the id of the mailing list to which mail is to be  
> > ,:include  
> > =\>:mailinglists,:conditions=\>[‘mailinglist\_id=?’,“#{params[:ml]}”])  
> > UserMailer.deliver\_send\_mail(@people)
> 
> @people.each do |p|  
> UserMailer.deliver\_send\_mail(p)  
> end
> 
> Perhaps this works.  
> Hi thanks it works…Do you have a better idea so that I collect all  
> the email address at once and call UserMailer.deliver only once with  
> different address seperated by comma.

Thank you.  
Regards,  
Anks

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/7 "2007-12-29T17:58:42Z")

</div>

Phillip K. wrote:

> On Dec 28, 2007, at 11:41 PM, Ank Ag wrote:
> 
> > Hello,  
> > I have to send mail to multiple recipients. My table is named  
> > people  
> > and has  
> > first\_name,last\_name, email1 , email2, email3 for each person. i have  
> > put people in various mailinglist .For example first mailinglist where  
> > first\_name is john or say the second mailing list contains people of  
> > british origin.etc
> 
> Just a matter of opinion, but you might consider moving your email  
> addresses to another table so you don’t have to limit your users to  
> three. I like to use a construct called “contact number” which can  
> be a phone number (landline or cell), fax, email, page, url, im,  
> whatever. Then you can have as many as you want and iterate through  
> them as a collection. But that’s just a matter of style and need.
> 
> > [‘mailinglist\_id=?’,“#{params[:ml]}”])
> > 
> > # this generates error undefined method email1
> > 
> > #---------------------------------------------------
> 
> recipients is an array, so you need to build all of the addresses as  
> an array. The collection people does not have a method called  
> “email1”, that is an attribute of each person object. So you’d need  
> to do something like this
> 
> people.each do |person|
> 
> # this is more personal than just the email address, but that would
> 
> work too  
> recipients \<\< “#{person.first\_name} #{person.last\_name} \<#  
> {person.email1}\>”  
> end
> 
> I don’t know about the performance of ActionMailer with thousands of  
> recipients, so Bala’s advice might be your best bet. And I would not  
> use Jochen’s advice (absolutely no offense intended, Jochen!) and put  
> each delivery in a loop. In that case, you’d be sending a single  
> message for every recipient instead of sending one message to your  
> outbound server and letting it handle the multiple recipients.
> 
> Oh, one other thing: use bcc instead of recipients. It’s generally  
> considered bad etiquette to send out a mass email and put everyone’s  
> address in the to line.
> 
> Peace,  
> Phillip

Thanks a ton Philip…One final question regarding mailing. The use  
wants to create templates and that will be in html format…The user  
will not know anything about HTML code so he will copy and paste the  
text from WORD to the textbox provided in the email interface of rails.  
Any suggestions how do I convert that code to html while pasting or it  
would be even better if it is possible to create a text box gmail style  
i.e. which has colors and fontsize options… Sorry for so many  
questions. I am somewhat new to ruby and rails…and have a project to  
complete in rails.

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/8 "2007-12-29T18:30:02Z")

</div>

> 

hi there  
What I did was to use the fckeditor a js script editor which is easily  
integrated with rails.  
This editor has an option PASTE FROM WORD that keeps “some” of the  
formatting (tables and other  
stuff) it has worked wonderully for me

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/9 "2007-12-29T21:14:46Z")

</div>

Jorge Mario G. Mazo wrote:

> > 
> 
> hi there  
> What I did was to use the fckeditor a js script editor which is easily  
> integrated with rails.  
> This editor has an option PASTE FROM WORD that keeps “some” of the  
> formatting (tables and other  
> stuff) it has worked wonderully for me

Yes, Thx Its really good…

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 29, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/10 "2007-12-29T18:20:05Z")

</div>

On Dec 29, 2007, at 10:58 AM, Ank Ag wrote:

> questions. I am somewhat new to ruby and rails…and have a project to  
> complete in rails.

Sorry, I can’t be of much help here. I have not yet had a need such  
as this and therefore have no experience working it out. I’m sure  
that someone else on here will have some great ideas, though.

Phillip

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [May 4, 2014, 8:25pm UTC](https://www.ruby-forum.com/t/sending-email-to-multiple-recipients/124498/11 "2014-05-04T20:25:33Z")

</div>

thank you
