Last weekend I was working on a mini hackathon and launched a group emailing service. EmailList.io. The task was to setup an email server, how to fetch and parse emails in ruby on rails forward it to respective group members. Last part was pretty simple. In this post, I am just talking about “How to fetch and parse emails in ruby on rails.”
To fetch email using POP/IMAP you check out a gem ie mailman.
Here is the sample code for the same:
Install the gem
Create a script file in scripts folder ie mailman_server and just paste the code below,
which itself is pretty clear. Check inline comments.
So far it’s pretty easy, but the question is how to parse the email body as HTML, text and parse attachments. Now take a look on this. Pretty simple.
Get attachments, HTML and Text Body
Once you have parsed all the attributes from the email ie to, from, email attachments now you can easily pass it to delayed job or something like creating a ticket or whatever you want to do as per you needs ;)
If you are facing any issues. Check out the source code files at github. Still, if you have any doubts contact me or you can comment on the blog post itself.