2

私のアプリはユーザーごとに一意の電子メールを作成し、ユーザーは処理のためにそのアドレスに電子メールを送信します。Sendgrid を使用して、受信メールを自分のドメイン (Heroku でホストされている) のアドレスにパイプしました。

site.com/receive_email

電子メール アドレスはランダムに生成されるため、TO フィールドを使用してユーザーを特定します。

Mailmanなどの外部スクリプトを使用して実験しましたが、Heroku でホストされているため、このプロセスを継続するにはワーカーをフルタイムで実行する必要があります。このテストアプリでは、現時点ではそれを実際に探していません。

それは POST リクエストとして処理を残します。receive_emails で POST ハッシュ (params["subject"] など) にアクセスできます。

これは私が立ち往生するところです

POST パラメータから生データを処理することをお勧めしますか、それとも Mailman や ActionMailer などを使用してメールを処理できますか?

4

2 に答える 2

3

Sendgridを使用してメールを投稿リクエストに変換したことはありませんが、herokuアドオンであるcloudmailinでは正常に機能します。これは、誰かがあなたのアプリケーションにメールを送信し、それがcloudmailin / sendgridによって処理されて投稿に変換され、次にそれをコントローラーに送信し、次にコントローラーがメッセージパラメーターを調べてメールから送信者を見つける例です。アドレス、および送信者がまだ存在しない場合は、彼女のアカウントを作成します。

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

幸運を。

于 2013-01-07T23:27:57.060 に答える
0

ActionMailerすでにgemに依存しているMailため、それを使用して受信メールを解析し、必要な部分を抽出できます. マルチパート電子メールを扱うのに特に便利です。

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
于 2013-01-07T23:59:05.013 に答える