Receiving and Processing Email: Heroku, Sendgrid, and possibly Mailman

匆匆过客 提交于 2019-12-06 06:16:17

I haven't used Sendgrid to turn emails into post requests, but it works fine with cloudmailin, a heroku addon. Here is an example where someone sends an email to your application, it is processed by cloudmailin/sendgrid and turned into a post, and then sends it to your controller, and then the controller looks through the message params, finds the sender from the email address, and if the sender doesn't already exist, creates an account for her:

class CreateUserFromIncomingEmailController < ApplicationController

  require 'mail'

  skip_before_filter :verify_authenticity_token

  parse_message(params[:message])

  def create
    User.find_or_create_by_email(@sender)
  end

private

  def parse_message(message_params)
    @message    = Mail.new(message_params)
    @recipients = @message.to
    @sender     = @message.from.first
  end

end

Good luck.

ActionMailer already depends on the Mail gem, you could use it to parse the incoming email and extract the parts that you want. It is specially useful to deal with multipart emails.

require 'mail'

class IncomingEmails < ApplicationController
  skip_before_filter :verify_authenticity_token

  def receive_email
    comment = Comment.new(find_user, message_body)
    comment.save

  rescue
    # Reject the message
    logger.error { "Incoming email with invalid data." }
  end

  private

  def email_message
    @email_message ||= Mail.new(params[:message])
    # Alternatively, if you don't have all the info wrapped in a
    # params[:message] parameter:
    #
    # Mail.new do
    #   to      params[:to]
    #   from    params[:from]
    #   subject params[:subject]
    #   body    params[:body]
    # end
  end

  def find_user
    # Find the user by the randomly generated secret email address, using
    # the email found in the TO header of the email.
    User.find_by_secret_email(email_message.to.first) or raise "Unknown User"
  end

  def message_body
    # The message body could contain more than one part, for example when
    # the user sends an html and a text version of the message. In that case
    # the text version will come in the `#text_part` of the mail object.
    text_part = email_message.multipart? ? email_message.text_part : email_message.body
    text_part.decoded
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!