# The best way to send generated numbers to an email address

**URL:** <https://www.ruby-forum.com/t/the-best-way-to-send-generated-numbers-to-an-email-address/134649>\
**Category:** Rails\
**Created:** [April 13, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/the-best-way-to-send-generated-numbers-to-an-email-address/134649 "2008-04-13T12:48:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [April 13, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/the-best-way-to-send-generated-numbers-to-an-email-address/134649/1 "2008-04-13T12:48:33Z")

</div>

Hi,  
I’m writing an application that generate some sort of prepaid cards,  
each  
card with a PIN and a serial number. All serials and PINs are being  
hashed  
when saved to the database. For more security, I need the generated  
numbers  
to be sent via email to the Admin when they generated, there is no way  
for  
the admin to view the numbers from inside the application.  
The process is as the following: Admin chooses to generate 1000 new  
cards  
(for example) --\> Numbers are sent to the Admin by email before they get  
hashed --\> numbers are hashed and saved to the database.

The above scenario works for me without problems, right now the  
generated  
numbers are being sent as plain text in the body of the email message,  
my  
question is that good for large amount of numbers (thousands of cards)?  
Or  
should I export them as a CSV file and send it as attachment? I checked  
FasterCSV and the standard Ruby CSV library but as I understand they  
wouldn’t work for huge amount of data. Should I consider exporting to  
Excel  
file? Or do you think that it’s just fine to send them in the body of  
the  
email message? Or do you suggest some alternative way that I didn’t  
think  
about?

For reference: this is the generate method in the controller:

def generate  
@cards = {}  
params[:card][:number\_of\_cards].to\_i.times do  
pin = rand(999999999999).to\_s.center(12, rand(9).to\_s)  
serial = rand(999999999999).to\_s.center(12, rand(9).to\_s)  
@cards[serial] = pin  
Card.create({:pin =\> pin, :serial =\> serial})  
end  
PostOffice.deliver\_send\_cards(@cards)  
redirect\_to(cards\_path)  
end

In the model:

def before\_save  
self.pin = Card.encrypt(pin)  
self.serial = Card.encrypt(serial)  
end

In the Action Mailder:

def send\_cards(cards)

```
@recipients = "removed_email_address@domain.invalid"
@from = "someaddress"
headers "Reply-to" => "removed_email_address@domain.invalid"
@subject = "New generated cards"
@sent_on = Time.now

body[:cards] = cards

```

end

send\_cards.html.erb:

\<removed\_email\_address@domain.invalid do |a,b| %\>  
\<%= a +","+ b %\>  
\<%end%\>

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [April 13, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/the-best-way-to-send-generated-numbers-to-an-email-address/134649/2 "2008-04-13T16:01:33Z")

</div>

I don’t see any problem with sending an email that’s multiple  
thousands of lines long.

One thing you might consider, though: format the email so that it  
sends in a “textualized table”… i.e. instead of one number per line,  
have 4 or 5 with tab stops or even spacings, so it lays out like a  
table. Look at the Array function “in\_groups\_of” for grouping the  
cards array in chunks.

Also, shouldn’t you slap some public/private key encryption on that?  
Sending card numbers in the clear via email is probably not a wise  
idea. You could encrypt the body of the email with your public key,  
then decrypt it on the admin’s email app with the admin’s private key.

-Danimal

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [April 14, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/the-best-way-to-send-generated-numbers-to-an-email-address/134649/3 "2008-04-14T15:49:29Z")

</div>

Sure!

[http://www.ahgsoftware.com/articles/2007/03/18/how-to-encrypt-ruby-on-rails-mail-with-gnupg](http://www.ahgsoftware.com/articles/2007/03/18/how-to-encrypt-ruby-on-rails-mail-with-gnupg)  
[http://agilewebdevelopment.com/plugins/gnupg](http://agilewebdevelopment.com/plugins/gnupg)

-Danimal

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [April 14, 2008, 9:58am UTC](https://www.ruby-forum.com/t/the-best-way-to-send-generated-numbers-to-an-email-address/134649/4 "2008-04-14T09:58:35Z")

</div>

Hi Danimal,  
Thank you for your useful comments.  
About encryption with with my public key, can rails do this when sending  
the  
email?

Regards

On 13/04/2008, Danimal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’m writing an application that generate some sort of prepaid cards,  
> > (for example) → Numbers are sent to the Admin by email before they get  
> > wouldn’t work for huge amount of data. Should I consider exporting to  
> > @cards = {}  
> > In the model:
> 
> > send\_cards.html.erb:
> > 
> > \<removed\_email\_address@domain.invalid do |a,b| %\>  
> > \<%= a +“,”+ b %\>  
> > \<%end%\>
> > 
> > Thanks in advance
> 
> >

–  
Anas Marrawi  
Visit me at: [www.anasonline.net](http://www.anasonline.net)
