Create a contact us form in rails that saves to the database and sends email alerts

Posted over 5 years ago by Emma
Posted over 5 years ago by Emma

Also dont know if its worth mentioning but i am still trying to do this in my localhost as i havent yet deployed

0
Posted over 5 years ago by Emma

Thanks i have managed to sort the contact form but i am having issues still with the email notifications. The link below i think is for rails 3 is there anything very different that i should be aware of? I have only ever used rails 4.1.1

0
Posted over 5 years ago by Alex Yang

Emma, have you already tried following the instructions from the article? Should be the same for Rails 4. Basically, you'll need to do the following 4 things:
1) Set your email configuration
2) Generate a mailer model
3) Deliver the email notification from your controller file
4) Create your mailer views

Here's a high-level outline on how to do each of these:

1) You should add a section to your config/environments/development.rb and config/environments/production.rb files. To use Gmail, you can use this:

config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'gmail.com',
authentication: "plain",
enable_starttls_auto: true,
user_name: "YOUR_USER_NAME", # just use the part before the @
password: ENV["YOUR_PASSWORD"] # assuming you've stored your Gmail password using Figaro
}

2) Run 'rails generate mailer NotificationsMailer'. This will create a file for you under '/app/mailers/notifications_mailer.rb'. Should look like this:

class NotificationsMailer < ActionMailer::Base
default from: "YOUR_NAME <USER_NAME@gmail.com>"
default to: "YOUR_NAME <USER_NAME@gmail.com>"

def contact_form_email(name, email, message)
@name = name
@email = email
@message = message

mail subject: "Contact form submission"
end
end

3) Then from your controller file, you should call this 'contact_form_email' method whenever you want the email to be sent. In your case, you'll want to do this from the 'create' action in your Contacts controller. Use code like the following, where you make sure to give the name, email, and message inputs that you want to be used:

NotificationsMailer.contact_form_email(@contact.name, @contact.email, @contact.message).deliver

4) Finally, you'll need to decide what to display in the email itself. You'll be creating a view page similar to the view pages you create for your web app. Under app/views/notifications_mailer, create a file called contact_form_email.html.erb and write the code for the email you want to send. A simple one could look like this:

Name: <%= @name %>
Email: <%= @email %>
Message: <%= @message %>

0
Posted over 5 years ago by Alex Yang

Emma, this is really two features in one: 1) a 'Contact Us' form that saves to a database and 2) email notifications. Rather than trying to tackle it all at once, it's better to do it one feature at a time. (A good starting point that will have some of the pieces you need is this one: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/).

To create a 'Contact Us' form that saves to a database, you'll need a few things: 1) a Contact database to store all the information, 2) a new URL to hold your 'Contact us' form, 3) new controller actions to process the submission of the form, and 4) a new view page to display your form.

Here's an outline for each of those steps - it should be enough to get you started :-)

1) Create your Contact database with a new migration. This depends on what information you want users to send, but it should be something like this: 'rails generate migration CreateContactTable name:string email:string message:text'. Run it with 'rake db:migrate' and don't forget to restart your Rails server.

2) Create your new URLs in config/routes.rb. Should be similar to the lines used in the article:
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
Remember that the 'new' action is used for displaying the form, while 'create' is used for processing the form submission.

If you run into an 'invalid route name' error, it's because you already have a '/contact' URL defined. Just name it something else instead.

3) Generate the controller file with your 'new' and 'create' actions with 'rails generate controller Contact new create'. Then write in the code for each of these actions. You can look at the corresponding code for any standard scaffolded controller (like the ones we use in the BaseRails course) as an example. Make sure to include the redirect after the form is submitted as well as the form parameters that can be safely submitted (not shown in the article, but look at the bottom section of another controller file to see what I mean).

4) Create a new.html.erb file in your views/contact folder and fill it with the form code with the fields you need.

Once you get that working, let me know if you still need help with the email notifications.

0
Posted almost 5 years ago by Oxana

I'm trying to set my gmail configuration. I don't need contact-us form, but I want users to be able to reset their forgotten passwords and get notifications when they sign up for the first time for the website.
I followed this article and add the code from step 1 to the development.rb and to the production.rb. I also noticed there is a line in development.rb required by Device gem:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Same line in production and it says to change host to the actual app host. Do we need to change something in this line?
I tried to reset password with just first step from this article and I got gmail message Google Account: sign-in attempt blocked. Any thoughts or what do I do wrong?

1
Posted almost 5 years ago by Alex Yang

For config.action_mailer.default_url_options, you should leave the value as { host: 'localhost', port: 3000 } in development.rb. In production.rb, you should use your website host instead (e.g. { :host => 'baserails.com' }).

Then just make sure you've set your config.action_mailer.smtp_settings as shown above. If you're still having trouble, you may need to add the following two lines as well:

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true

Try it out and let me know if it works!

1
Posted almost 5 years ago by Oxana

I set it up and it works in my development environment. It sends email reset link. However it does not work in production, heroku does not give any errors, notification comes up that you'll receive an email in a few minutes but email never comes. I checked gmail and from development I can see in sent emails but from production nothing come. I read in some reference it can happens in heroku and I need to add Rails.application.routes.default_url_options[:host] = 'yoursite.herokuapp.com' into production. I did but nothing changed. No errors and no emails :)

1
Posted almost 5 years ago by Alex Yang

You should be fine if you've set your config/production.rb settings correctly. In particular, did you set config.action_mailer.default_url_options = { :host => 'yoursite.herokuapp.com' }?

Make sure you've pushed your changes up to Heroku and try checking your heroku logs as well.

1
Posted almost 5 years ago by Oxana

I think my settings are correct and I did push changes to heroku. I don't get any errors and when I check heroku logs it says that email was sent.But it never arrives and I never can see it in gmail sent box. As opposite I can see them when I reset password from development. I don't know where else it can be. I checked bunch of articles and they all have same settings.

1
Posted almost 5 years ago by Alex Yang

If that's the case, you might want to try using an email service like Mailgun or Sendgrid instead. The setup should be relatively similar and you should be able to quickly confirm that at least it works.

1
Posted almost 5 years ago by Oxana

That's what I read as an advice too, people are saying that gmail and heroku have problems. I looked carefully at log and I found after email was sent there is a Completed 302 found. May be this causing problems? I can post a log here if it would help

1
Posted almost 5 years ago by Alex Yang

It may be a small typo somewhere, either in your configuration or filename or somewhere else, that's causing the issue. Paste a link to a CloudApp screenshot here if you like, but I'd just recommend going through the Mailgun setup since it's straightforward and may solve your problem directly.

1
Posted almost 5 years ago by Oxana

I setup Mailgun finally for pruduction and it works. I left gmail settings for development. I wonder if heroku did some settings that prevent sending via gmail?!

1