Catch emails to all [email protected], and execute code

124 views Asked by At

In my webapp I have instances where I want to let users send an email to their local representative. But I don't want to expose that user's email so I'd like to have the email sent from [email protected], where a_user_name is different depending on who sends it.

Where I'm lost is how to handle the situation where somebody replies to that email, and I need to deliver it to the original author. I'd like to be able to catch that email, look up the user in our database, and send them an email notifying them of the reply. How do I complete this last part?

Using Rails + Amazon SES

Thanks!

2

There are 2 answers

0
andyleesuk On BEST ANSWER

You can integrate with a transactional email provider like Mandrill (or others). There's various mandrill rails gems to help.

See: https://mandrill.zendesk.com/hc/en-us/articles/205583197-Inbound-Email-Processing-Overview

The inbound email gets picked up in a controller like:

class InboxController < ApplicationController
  include Mandrill::Rails::WebHookProcessor

  def handle_inbound(event_payload)
   #parse event_payload and take appropriate action
  end
end

You can then parse that email to get the sender validate it against your user table and pass the message on or whatever.

You probably want to set a subdomain of your domain or a totally separate domain so you can route your normal email traffic to your mydomain.com mailservers and pick up the emails to be parsed by rails / processed by the webhook from mail.mydomain.com or whatever.

1
Milind On

you can use Base64.encode64

2.0.0-p481 :029 >   encrypted_email=Base64.encode64('[email protected]')
 => "bWlrZUBnbWFpbC5jb20=\n" 
2.0.0-p481 :030 > Base64.decode64(encrypted_email)
 => "[email protected]" 

OR MessageEncryptor

2.0.0-p481 :037 >   crypt = ActiveSupport::MessageEncryptor.new(Rails.configuration.secret_token)
 => #<ActiveSupport::MessageEncryptor:0xcd4d6fc @secret="c07692942cde247c96ea3da23a4d6406ebdad7c37f63c13e100731ce03ce24088dbe419da154fb7e504e777c60c7f6f8850d27f8cb8b7968602244ce5c21bfb3", @cipher="aes-256-cbc", @verifier=#<ActiveSupport::MessageVerifier:0xcd4d4e0 @secret="c07692942cde247c96ea3da23a4d6406ebdad7c37f63c13e100731ce03ce24088dbe419da154fb7e504e777c60c7f6f8850d27f8cb8b7968602244ce5c21bfb3", @digest="SHA1", @serializer=ActiveSupport::MessageEncryptor::NullSerializer>, @serializer=Marshal> 
2.0.0-p481 :038 > encrypted_data = crypt.encrypt_and_sign('[email protected]')
 => "UjF4VFRnRnF1RVJnZXhUUm1KakJZcDFMN1ZoZXVzSmFvLzUwdFkydXNjYz0tLTlsbFI3Vlo4RUNVK2pMZVEzS2tSOWc9PQ==--710d4fcdc202e1b1143e12661d8b40831525f158" 
2.0.0-p481 :039 > decrypted_back = crypt.decrypt_and_verify(encrypted_data)
 => "[email protected]" 

So.you may use a dedicated table to store both encrypted and original emails and then use it in the FROM section of mail as well as to refer it for future use .So you must use a Generic email id that users can use to reply and then you may send this encrypted hidden field which will identify which email id was intended to send and again send to that email.

i know it would be a two way process like:-

  1. Encrypt email in From part and use Generic email where users can reply back
  2. Use hidden field containing encrypted mail
  3. So,if the user replies back to the Generic mail,check the encrypted mail from hidden field and then again resend it to the decrypted email.

HOPE THIS HELPS.