开发者

How to receive mail from gmail by using gmail gem n rails 3

开发者 https://www.devze.com 2023-03-11 22:59 出处:网络
I want to receive the mail from gmail gem into my rails app, I am able to receive the m开发者_高级运维ail by using following code in my controller

I want to receive the mail from gmail gem into my rails app, I am able to receive the m开发者_高级运维ail by using following code in my controller

home_controller.rb

class HomeController < ApplicationController
  def index
    @gmail = Gmail.connect("myemail.com","mypassword")
    @rec = @gmail.inbox.mails()     
  end
end

app/view/index.html

<h1>Mails</h1>

<table border="3" cellpadding="10">
<%= @rec.each do |mail| %>
    <tr><td><%= mail.message.body.raw_source.create %></td></tr>
<% end %>
</ul>

Here I do get the mail but alog with additional headers and information. I just want to get the email subject and body.

Is there any solution to this using "gmail gem" or will I need to use something else ?


I don't know anything about the GMail Gem but the Mail Gem itself will do the parsing of this information. You can use the Mail Gem to parse the contents of the email and pull out the subject and html information.

You may also want to take a look at my blog post on receiving email in rails using a test driven approach, it might not apply directly but it shows some thoughts on how you can separate out some of the logic away from using the controller directly.


Instead of <%= mail.message.body.raw_source.create %> you must do

<%= mail.subject %>
<%= mail.body %>
0

精彩评论

暂无评论...
验证码 换一张
取 消