Sending email from Rails application
It took me a while to configure sending email from Rails application. I went through many different tutorials, blogs, StackOverflow posts etc. Step, by step I found working configuration.
To send emails I use ActionMailer. First you need to generate mailer:
rails generate mailer UserMailer
It will create UserMailer class in app/mailers directory. In this class we need to define our method for sending emails:
Then in the directory app/views/user_mailer we need to create template for our email: send_email.html.erb (this name must match the name of action created in UserMailer class):
(*) If you do not want to send html you can create plain text and name file send_email.text.erb.
Now, the hardest part. Configuration of smtp. You need to add it to config/environments/development.rb (or test.rb or production.rb). I found this configuration working for gmail:
The last thing is call ActionMailer from our app:
Form for sending email can looks like that:
For that you need to configure action in controller and match route e.g.:
Method send_mail (from UserMailer class) can have as much parameters as you need. It can be also 0. In above example, the params are just rendered in the email template (send_email.html.erb file).