# How to Create Records in Rails App Database using Inbound Em

**URL:** <https://www.ruby-forum.com/t/how-to-create-records-in-rails-app-database-using-inbound-em/90646>\
**Category:** Rails\
**Created:** [March 30, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/how-to-create-records-in-rails-app-database-using-inbound-em/90646 "2007-03-30T17:39:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 30, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/how-to-create-records-in-rails-app-database-using-inbound-em/90646/1 "2007-03-30T17:39:56Z")

</div>

I am trying to create a feature within my application to receive  
product data including an image attachment via inbound email.

Can someone point me to a tutorial or other document online that will  
help out with this. I have started hammering at Google, but was  
hoping someone could provide a link.

Thanks,  
Nathan

---

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 30, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/how-to-create-records-in-rails-app-database-using-inbound-em/90646/2 "2007-03-30T18:12:15Z")

</div>

On 3/30/07, Nathan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to create a feature within my application to receive  
> product data including an image attachment via inbound email.
> 
> Can someone point me to a tutorial or other document online that will  
> help out with this. I have started hammering at Google, but was  
> hoping someone could provide a link.

Nathan,  
I dug this out of a feed recently:  
[http://wiki.rubyonrails.com/rails/pages/HowToReceiveEmailsWithActionMailer](http://wiki.rubyonrails.com/rails/pages/HowToReceiveEmailsWithActionMailer)

Is this what you want?

---

<div class="post-metadata">

**Author:** ![nathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c5a1d2/32.png) [@nathan](https://www.ruby-forum.com/u/nathan)\
**Post date:** [March 30, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-create-records-in-rails-app-database-using-inbound-em/90646/3 "2007-03-30T17:54:09Z")

</div>

Nathan wrote:

> I am trying to create a feature within my application to receive  
> product data including an image attachment via inbound email.
> 
> Can someone point me to a tutorial or other document online that will  
> help out with this. I have started hammering at Google, but was  
> hoping someone could provide a link.

Here’s some code I use in one of my apps. Stole most of it from some  
examples I found somewhere, don’t remember where:

class IncomingCSVFileHandler \< ActionMailer::Base  
def receive(email)  
return unless email.has\_attachments?

```
email.attachments.each do |attachment|
  next unless attachment.original_filename[-4..-1] == '.csv'

  CSV::Reader.parse(attachment.read) do |rec|
    next if rec.last.blank?
    server = Server.find_by_name(rec.last.split('-',

```

2).first.strip.upcase)  
next if server.blank?

```
    a = Answer.new
    a.server = server
    a.ticket_no = rec.first
    a.question = rec[2]
    a.answer = rec.last.split(' - ', 2).last
    first_name, last_name = rec[3].split
    a.created_by = User.find_by_name("#{last_name},

```

#{first\_name}.titleize")  
a.last\_updated = Time.now  
a.updated\_by = a.created\_by  
a.save!  
RAILS\_DEFAULT\_LOGGER.info(“Added answer record for  
#{server.name}”)  
end  
end  
end

def self.check\_mail  
begin  
imap = Net::IMAP.new(‘130.102.202.121’)  
imap.login(‘userid’, ‘password’)  
imap.select(‘remedycsvfiles’)  
imap.search([“SINCE”, Date.today.strftime("%d-%b-%Y")]  
).each do |message\_id|  
msg = imap.fetch(message\_id,‘RFC822’)[0].attr[‘RFC822’]

```
    IncomingCSVFileHandler.receive(msg)

    #Mark message as deleted and it will be removed from storage

```

when user  
#session closed

# imap.store(message\_id, “+FLAGS”, [:Deleted])

```
  end

```

# imap.expunge

```
  imap.logout
rescue
  RAILS_DEFAULT_LOGGER.error("Remedy CSV File Import Error: " + $!)
end

```

end  
end

Not pretty, but it works!

HTH,

Jamey C.
