Rails Contact Form Not Working

Rails contact form not sending email

Add following smtp setting on config/application.rb file:

  config.action_mailer.default_url_options = { :host => 'http://www.xxxx.com' }
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxxxxxxxx@gmail.com",
:password => "xxxxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}

Rails contact form not working

To trigger mails in dev mode, add this to your development.yml file:

config.action_mailer.perform_deliveries = true

Rails 5 contact Me form not sending email using sendgrid via heroku

It does not look to me like you are actually triggering the send event in your controller. If I am understanding your process correctly your create method should have the .deliver method on the mailer and message.

def create
@message = Message.new message_params

if @message.valid?
MessageMailer.contact_me(@message).deliver <-- This
redirect_to new_message_url, notice: "Message received, thanks!"
else
render :new
end
end

Rails form builder not working as expected

The line causing the problem was in app/views/papers/_form.

Replacing <%= form.fields_for :question do |builder| %>
with <%= form.fields_for :questions do |builder| %>

solved the issue.

MailForm is not sending email

First you should remove the warning, because now emails should be sent using the ActiveQueue.
Then you are not receiving your emails, probably, due a bad configuration. Since you haven't posted your configuration I would advise you to go through the guides and setup your development profile for mail.

I quick path is to use Gmail to send your emails:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }

Ruby on Rails: Cannot Move URL for Contact Form Without Getting NoMethodError

The problem is that the default path for a form_for @object when the object is not persisted (new record) is always objects_path. You'll have to explicitly state the url, something like...

<%= simple_form_for @contact, as: :contact, url: '/contact', html: { class: "new_contact", id: "new_contact" } do |f| %>

BUT this is going to mess up the form for your existing contacts when you try to edit them.

You'll have to do an <% if @contact.new_record? %> and <% else %> and <% end %> to handle the two different URLs required.

So the best recommendation is really follow the convention! Use plurals for the controller, set the route back to the way it was.

Unless you have a compelling reason to violate the "convention over configuration" rule, you shouldn't.

The docs explain the default URLs used. https://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

Show Rails Contact Form on main page

And easy and clean way would be to create a partial

_contact_form.html.erb (Partials always starts with an underscore)

.container
%h1 Contact
= simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f|
= f.input :name, :required => true
= f.input :email, :required => true
= f.input :message, :as => :text, :required => false, :input_html => {:rows => 10}

.hidden
= f.input :nickname, :hint => 'Leave this field blank!'
.form-actions
= f.button :submit, 'Send message', :class=> "btn btn-primary"

Then, in your index page:

<%= render "contacts/contact_form" %>

and in your controller index action( I don't know if 'welcome/index' is on project's controller or contacts controller)

def index
#your code
@contact = Contact.new
end

Finally, you seem quite new to rails, I'd like to recommend a free Ruby on Rails Tutorial

Add a contact form to Hartl's tutorial

4) app\controllers\static_pages_controller (or another location?)

This seems to be correct, if this is the github repo for said app.


def send_contact_form_email

You controller has an issue: this action will try to send the email, not matter if it's used in POST or GET. You should use two different actions, one for displaying the view (using GET), and one for sending the email (using the mailer class you created). (at this point, you might want to create another controller)


ContactFormMailer.send_contactform_email(visitor).deliver_now

Then, moving on: what you pass to your mailer is "visitor". There's no such variable.
You probably want to access something out of the params hash (which contains parameters for GET and POST requests), and use the same key as your form (form_for(:visitor ... => params[:visitor] (so you want to change that :static_pages)).

<p>On <$= ... %> <%= "#{@visitor.name} (#{@visitor.email} sent the following message:" %></p>

As this returns an object, and not a hash, @visitor.email needs to be @visitor[:email] inside the mailer.


One last thing: simply using params[:visitor] will mean people could leave the field blanks. You might want to look into strong parameters, that were added in Rails 4 (the book seems somewhat outdated?).

And lastly, you need to add routes to be able to reach these actions (one for the GET request - display the view - and one for the POST request - to submit the form).


PS:

mail( :to => myemail@example.com, :from => visitor.email, :subject => 'Contact form' )

Warning: here, you forgot to quote the email address. Also, you swapped the to/from parameters. You want to send TO your visitor email, not from it.


EDIT

params.require(:message).require(:name, :email, :content)

This will require said keys, but AFAIK on the same "level" as :message - the top one. You want to use permit:

params.require(:message) # require "namespace"
.permit(:name, :email, :content) # permit keys

    @message = message(message_params)

Where is the message function defined?

mail to: "myemail@example.com"
mail from: @message[:email]
mail subject: "Message from #{message[:name]}"

This sends 3 different emails, since you called the mail function 3 times.



Related Topics



Leave a reply



Submit