All you need to do is add the following method to your action mailer model:
def receive(email)
...
end
"email" argument is an object of type TMail::Mail. You can use it to read email metadata:
@to = email.to
@from = email.from
@body = email.body
The nice part is accessing the email attachments (if exist) through simple code. The following code checks if the email have attachments and read each attachment file name, content type and its content:
def receive(email)
if email.has_attachments?
email.attachments.each do |attachment|
@file_name = attachment.original_filename
@content_type = attachment.content_type
@content = attachment.read
end
end
end
Using ActionMailer for receiving emails contains two magical parts:
- Some email bodys encoded using Base64 encoding, also the attachments. The magical part in TMail is that it handles decoding the attachment content using Base64 decoder for you. So, you access the attachment content directly without decoding it.
- If you read the incoming email from a system file and need to feed it to your ActionMailer.receive method, all you need to do is reading the file content and give it directly to your ActionMailer.receive method without converting it into TMail object and ActionMailer will do the rest:
file = open("my_email")
email_content = file.read
file.close
MailerAgent.receive(email_content)
2 comments:
Salam Wael, nice article, I read it in your blog before :)
However I have a question: what email server you used to receive your emails? And how did u configure it?
Thanks
Salam Hossam,
Thanks a lot :)
As I remember, the email server was exim.
We used it to receive the emails, put them inside some system folder as files and then I read the files through my RoR and pass them to ActionMailer.
Of course this is not the optimal solution, but we did it like that just for saving time :)
Post a Comment