Ruby on Rails
This example shows how to send an email for user signups. You can also checkout this gem for more advanced features.
Setup ActionMailer
Let’s generate a Mailer class. Mailer classes function as our controllers for email views.
1
|
|
Now we open up the mailer we’ve just generated, app/mailers/user_notifier.rb
and add a mailer action that sends users a signup email.
1 2 3 4 5 6 7 8 9 10 |
|
Now we need a view that corresponds to our action and outputs HTML for our email. Create a file app/views/User_notifier/send_signup_email.html.erb
as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
If you don’t have a user model quite yet, generate one quickly.
1 2 |
|
Now in the controller for the user model app/controllers/users_controller.rb
, add a call to UserNotifier.send_signup_email when a user is saved.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Alright, now we’re cooking! Let’s get it all going through Aritic Mail.
Configure ActionMailer to Use Aritic Mail
In config/environment.rb
specify your ActionMailer settings to point to Aritic Mail’s servers.
1 2 3 4 5 6 7 8 9 |
|
That’s it! When a new user object is saved, an email will be sent to the user via Aritic Mail.
As a best practice, you should not store your credentials directly in the source but should instead store them in configuration files or environment variables. See this tutorial on environment variables in Rails.