[英]Using ActionMailer in Rails to receive emails from forms
我正在嘗試使用Rails 5中的ActionMailer從聯系表單中接收電子郵件,但是我不確定如何將在表單上創建的資源的參數發送到receive方法中的電子郵件,然后將其發送到應用程序的電子郵件中。
控制器/ 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: "atendimento@lcasystems.com.br"
def receive(email)
mail.create(subject: email.subject, body: email.body)
end
end
它給出了500個服務器錯誤,並在接收方方法中為nil:NilClass說了未定義的方法'humanize'。
這是一個聯系表單,因此您希望將電子郵件發送給自己,因此需要“ to:
鍵-值”對。
包括聯系人的姓名和電子郵件也很有用,這樣您就可以知道將答復發送到哪里。
def receive(contact)
mail(to: "atendimento@lcasystems.com.br",
subject: contact.subject,
body: contact.body.to_s +
"\n (Sender is #{contact.name} and their email is #{contact.email})")
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.