如何使用 gmail gem n Rails 3 接收来自 gmail 的邮件
我想将来自 gmail gem 的邮件接收到我的 Rails 应用程序中,我可以通过在我的控制器
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>
中使用以下代码来接收邮件,这里我确实收到了邮件,但带有附加标头和信息。我只想获取电子邮件主题和正文。
使用“gmail gem”有什么解决方案吗?或者我需要使用其他东西吗?
I want to receive the mail from gmail gem into my rails app, I am able to receive the mail 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 GMail Gem 一无所知,但 Mail Gem 本身会解析此信息。您可以使用邮件宝石来解析邮件内容并提取出主题和html信息。
您可能还想查看我的博客文章 使用测试驱动的方法在 Rails 中接收电子邮件,它可能不会直接应用,但它显示了一些关于如何将某些逻辑与直接使用控制器分离的想法。
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.
而不是
<%= mail.message.body.raw_source.create %>
您必须这样做Instead of
<%= mail.message.body.raw_source.create %>
you must do