2012-12-04 1 views
2

내 응용 프로그램은 각 사용자에 대해 고유 한 전자 메일을 만들고 사용자는 해당 주소로 전자 메일을 보내 처리합니다. site.com/receive_email 전자 메일 수신 및 처리 : Heroku, Sendgrid 및 Mailman

내가 이메일 주소가 무작위로 생성되기 때문에, 사용자를 결정하기 위해 필드에를 사용하여 (아마 : Sendgrid를 사용하여, 나는 주소 (Heroku가에서 호스팅) 내 도메인으로 들어오는 이메일을 파이프했습니다 그곳에 보안 문제가있다.)

Mailman과 같은 외부 스크립트를 사용해 본 적이 있지만 Heroku에서 호스팅되었으므로이 프로세스를 진행하기 위해 풀 타임으로 작업자를 실행해야합니다. 이 테스트 앱을 위해 지금은 그 순간을 찾지 않습니다.

그러면 POST 요청으로 처리됩니다. receive_emails에서 POST 해시 (params [ "subject"] 등)에 액세스 할 수 있습니다. 나는 문제가 발생할 경우

이것은

는 POST의 PARAMS 원시 데이터를 처리하는 것이 좋습니다겠습니까, 아니면 나를 위해 이메일을 처리하는 우편 집배원 또는 ActionMailer 같은 것을 사용할 수있다?

답변

3

전자 메일을 게시 요청으로 변환하는 데 Sendgrid를 사용하지는 않았지만 heroku addon 인 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 

행운을 빕니다.

0

ActionMailer은 이미 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 
+0

그리고 이것이 더 참고를 들어,'Mail' 보석 저장소입니다 : https://github.com/mikel/mail –

+0

이 질문 만 붙어에서 언급 안녕 @Jorge 또한 같은 작업을하고 있어요 내가 [여기] (http://stackoverflow.com/questions/35382172/receiving-emails-with-sendgrid-and-cloudmailin)를 게시 한 단계에서. 도와 줄수있으세요 –