# Action mailer

**URL:** <https://www.ruby-forum.com/t/action-mailer/138958>\
**Category:** Rails\
**Created:** [May 29, 2008, 11:05am UTC](https://www.ruby-forum.com/t/action-mailer/138958 "2008-05-29T11:05:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [May 29, 2008, 11:05am UTC](https://www.ruby-forum.com/t/action-mailer/138958/1 "2008-05-29T11:05:18Z")

</div>

class Mailman \< ActionMailer::Base  
def receive(email)  
page = Page.find\_by\_address(email.to.first)  
page.emails.create(  
:subject =\> email.subject, :body =\> email.body  
)

```
  if email.has_attachments?
    for attachment in email.attachments
      page.attachments.create({
        :file => attachment, :description => email.subject
      })
    end
  end
end

```

end

what does this line do…

def receive(email)  
page = Page.find\_by\_address(email.to.first)  
page.emails.create(  
:subject =\> email.subject, :body =\> email.body  
)

Look like it can’t work at all

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [May 29, 2008, 11:10am UTC](https://www.ruby-forum.com/t/action-mailer/138958/2 "2008-05-29T11:10:53Z")

</div>

On 29 May 2008, at 10:05, Emmie W. wrote:

> what does this line do…
> 
> def receive(email)  
> page = Page.find\_by\_address(email.to.first)  
> page.emails.create(  
> :subject =\> email.subject, :body =\> email.body  
> )

Which part isn’t working? if Page. find\_by\_address is returning nil,  
have you compared what’s in the database with what’s in the email  
(bearing in the mind that the email will probably have something like  
“Fred blogs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)” whereas your database might only have  
removed\_email\_address@domain.invalid  
.

Fred

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [May 29, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/action-mailer/138958/3 "2008-05-29T12:32:01Z")

</div>

receive is a class method in ActionMailer::Base that processes  
incoming mail. What you usually do is pipe contents of the email, as a  
string, from your mailing process to the receive method which  
automatically converts it to a Tmail object.

This is the example for Rails Way (modified slightly to match your  
class name) to listen for incoming mail:

./script/runner ‘Mailman.receive(STDIN.read)’

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [May 30, 2008, 3:57am UTC](https://www.ruby-forum.com/t/action-mailer/138958/4 "2008-05-30T03:57:10Z")

</div>

Actually these code is not working

if email.has\_attachments?  
for attachment in email.attachments  
page.attachments.create({  
:file =\> attachment, :description =\> email.subject  
})  
end

So i change the code to

if email.has\_attachments?  
email.attachments.each do |attachment|  
# save original file  
File.open(“C:/rubydev1/mail7/log/” +  
base\_part\_of(attachment.original\_filename)  
,File::CREAT|File::TRUNC|File::WRONLY,0666){ |f|  
f.write(attachment.read)  
f.close()  
}  
end  
end

And these code work fine, it did help me read the attachment file and  
save it into the ruby directory. Then now i want to retrieve hte email  
header such as the receiver, the senter, subject and date. It is  
possible. Thanks
