Reputation: 957
I am trying to receive emails from a Contact Form using ActionMailer in Rails 5. But I am not sure on how send the params of the resource created on the form to the email on the receive method and send it to the email of the app.
controllers/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
mailers/default_mailer.rb:
class DefaultMailer < ApplicationMailer
default from: "[email protected]"
def receive(email)
mail.create(subject: email.subject, body: email.body)
end
end
It gives a 500 server error and says undefined method `humanize' for nil:NilClass in the receiver method.
Upvotes: 0
Views: 282
Reputation: 36860
It's a contact form, so you want the email to be sent to yourself, so you need the to:
key-value pair.
It's also useful to include the contact's name and email so you can know where to send any replies.
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
Upvotes: 1