Distributing the certificates

As a course administrator,
So that the right certificate is delivered to a student,
I would like certificates to be emailed to a students registered email address.

Let's start with modifying the certificate_generation.feature by adding the following step:

# features/certificate_generation.feature

...
  Then 3 instances of "Certificate" should be created
  And 3 certificates should be generated
  And 3 images of certificates should be created
  And 3 emails with certificates attached should be sent
  And I should see "Generated 3 certificates"
...

Run that scenario by pointing cucumber to the line of code where it starts:

$ cucumber features/certificate_generation.feature:7

We want to install a gem that helps us to send emails from the application. As a first step we will do the simplest implementation we possibly can.

Let's add Mail to our Gemfile and run bundle

# Gemfile

gem 'mail'

In our controller, we add a configuration block:

And, of course, you need to update your .env file with those variables:

I can not stress this enough - MAKE SURE THAT .env IS OUTSIDE YOUR VERSION CONTROL

Okay, moving on.

In certificate_generator.rb add the key :email to the details hash:

Also, add a method called send_email to that module. That method needs to have access to the details hash, so we are going to allow it to take two arguments:

Create a file named body.txt in pdf/templates folder:

Add some content to that file:

We could personalize this message, but for now some static content will do.

Another update we need to make is again in the certificate_generator.rb We need to call the send_email method from the generate method in order to actually send the message with the certificate attached to it:

Last updated