Setting up a mailer is a relatively straightforward task. It’s very similar to building a new controller and views. Once you’ve made a couple, it should come naturally.
You’ll be dusting off your Flight Booker project and having it send out a “You have booked your ticket” confirmation email to all Passengers when they are created as part of the booking process. Make sure to send out one email to each Passenger in the Booking, not just one for the whole Booking. (Alternatively, you can use one of your other projects, as long as it has users registering.)
- Locate and load up the project file.
- Do some pushups and jumping jacks. You’ve been spending a bit too much time at the computer lately.
- Generate your new mailer with
$ rails generate mailer PassengerMailer.
- Install the
letter_openergem (see docs here) to open your emails in the browser instead of sending them in the development environment.
- Follow through the steps listed in the Rails Guide to create the action to send the confirmation email.
- Build both an
.text.erbversion of your ticket confirmation email.
- Test that the email sends by creating a new flight booking (
letter_openershould open it in the browser for you if you’ve set it up properly).
Try out one other trick – call the mailer directly from the Rails Console using something like:
- Extra Credit: Deploy it to Heroku and try it out. If you deploy to Heroku, there will be a bit of additional setup to get the SendGrid add-on (see docs) and make sure your configuration is set up properly. The docs describe how to get that up and going.
(One heads up: in order to use SendGrid you will probably have to give your credit card information to Heroku, if you haven’t already. You don’t have to pay for the service, but you do have to give your information.)
This section contains helpful links to other content. It isn’t required, so consider it supplemental.