正在开发 ruby on Rails3 应用程序,我正在向用户发送电子邮件,如果用户回复该电子邮件,则回复内容、日期应更新到数据库中。为此我有 ProductComment 模型。当管理员向用户发送评论时,它将存储在数据库中。如果用户回复,则数据库应相应更新。我正在尝试使用 postman 。我已经安装了 gem 。但我不知道如何获取评论 ID、我应该在回复地址中写什么、在哪里编写 postman 代码以及我应该从哪封邮件中读取。
我正在发送这样的电子邮件:
mail(:to => @user.email, :subject => "Edit Your Product", :reply_to=>"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c6d6e6f4c7f7576226f6361" rel="noreferrer noopener nofollow">[email protected]</a>)
我在产品 Controller 中处理它,如下所示:
require 'mailman'
Mailman::Application.run do
to '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0b08092a12131044090507" rel="noreferrer noopener nofollow">[email protected]</a>' do
ProductComment.create(message)
end
end
请帮助我解决这个问题
请告诉我如何在 ruby on Rails3 应用程序中使用 mailman gem
最佳答案
最近有一个关于在 Railscasts 上使用 postman 接收电子邮件的专业剧集:http://railscasts.com/episodes/313-receiving-email-with-mailman
chmod +x script/mailman_server
cat mailman_test.eml | script/mailman_server
script/mailman_server
-
# script/mailman_server
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "mailman"
Mailman.config.logger = Logger.new("log/mailman.log")
Mailman.config.pop3 = {
server: 'pop.gmail.com', port: 995, ssl: true,
username: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
Mailman::Application.run do
default do
begin
Ticket.receive_mail(message)
rescue Exception => e
Mailman.logger.error "Exception occurred while receiving message:\n#{message}"
Mailman.logger.error [e, *e.backtrace].join("\n")
end
end
end
-
def self.receive_mail(message)
ticket_id = message.subject[/^Update (\d+)$/, 1]
if ticket_id.present? && Ticket.exists?(ticket_id)
Ticket.update(ticket_id, body: message.body.decoded)
else
Ticket.create subject: message.subject, body: message.body.decoded, from: message.from.first
end
end
关于ruby-on-rails-3 - 如何使用mailman处理传入的邮件并将其更新到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094872/