2017-12-30 20 views
0

레일스 5의 ActionMailer를 사용하여 문의 양식에서 전자 메일을 수신하려고합니다. 양식에 작성된 리소스의 매개 변수를 전자 메일로 보내는 방법에 대해서는 잘 모르겠습니다. 메소드를 수신하여 앱 이메일로 보냅니다.레일스에서 ​​ActionMailer를 사용하여 양식에서 전자 메일 수신

컨트롤러/contacts_controller.rb가이 :

class ContactsController < ApplicationController 

    def index 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(contact_params) 
    if @contact.save 
     DefaultMailer.receive(@contact).deliver 
     redirect_to root_path 
    else 
     render :new 
    end 
    end 

    private 
    def contact_params 
    params.require(:contact).permit(:name, :email, :subject, :body) 
    end 
end 

우편물/default_mailer.rb는 :

class DefaultMailer < ApplicationMailer 
    default from: "[email protected]" 

    def receive(email) 
    mail.create(subject: email.subject, body: email.body) 
    end 
end 

그것은 500 서버 오류를 제공하고 정의되지 않은 방법은`전무에 대한 '인간화 말한다 : NilClass를 수신기에 방법.

답변

1

연락처 양식이므로 이메일을 직접 보내려고하므로 to: 키 - 값 쌍이 필요합니다.

답장을 보낼 곳을 알 수 있도록 연락처의 이름과 전자 메일을 포함하는 것도 유용합니다.

def receive(contact) 
    mail(to: "[email protected]", 
     subject: contact.subject, 
     body: contact.body.to_s + 
      "\n (Sender is #{contact.name} and their email is #{contact.email})") 
end