Question

am developing ruby on rails3 application where i am sending an email to user and if user replies that email then that reply content, date should be updated to the database. For this i have ProductComment model. when admin sends comment to the user it will be stored in the database. if user replies to that then database should be updated accordingly. I am trying to use mailman. I have installed the gem. But am not getting how to get the comment id, what should i write in replyto address, where to write the mailman code and from which mail i should read.

Am sending email like this:

mail(:to => @user.email, :subject => "Edit Your Product", :reply_to=>"abc@syz.com)

I am handling it in products controller like this:

require 'mailman'
Mailman::Application.run do
to 'abc@xyz.com' do
ProductComment.create(message)
end
end

Please help me to come out from this problem

Please tell me how to use mailman gem in ruby on rails3 application

Was it helpful?

Solution

there is a recent pro-episode on receiving emails with mailman on railscasts: 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

OTHER TIPS

Postmark Inbound is a good choice. Setup like so:

  1. Sign up for Postmark, they will give you an email which Postmark will assign to your account.
  2. Sign up for Google Apps branded Gmail for your domain. Set up forwarding from an account to the Postmark email address. People can now email reply@yourdamin.com, and it will be forwarded to Postmark.
  3. Create a callback URL. When Postmark receives an email it will package it up and post it to your callback. You can then access the email attributes via the params hash.

To implement replying to messages, simply add a reply to field to your outgoing message which contains a unique hash for the message, e.g.

reply+uniquehash@yourdomain.com.

This is a legal email address, and will be sent to reply@yourdomain.com. You can then parse out the hash in your callback and use it to match the reply to the original message.

Simple :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top