loyno-mathcs / workforwardnola

2016 NOLA Fellowship project | A mobile-first website that acts as a digital one-pager for the workforce system in New Orleans, reducing drop-off by setting proper expectations and preparing job seekers for their first visit.
http://careerpathnola.com
MIT License
2 stars 3 forks source link

RESEARCH: figure out email solution (2) #22

Open maahern opened 7 years ago

maahern commented 7 years ago

--4 hours

maahern commented 7 years ago

Documentation for email solution: The implementation and configuration of automated messages will depend on the service being used. I have provided some documentation for three different implementation options. I recommend that we use either Mandrill or Amazon SNS. Both options are highly configurable and relatively easy to implement. I have provided valuable resource links in this document for more information on Mandrill and Amazon SNS. Amazon SNS is suited for large-scale email solutions, whereas Mandrill may be more appropriate for our scope.

Using Mandrill:

Mandrill is a lightweight wrapper used for messages/notifications. The Mandrill gem uses an API stored as an environment variable called MANDRILL_APIKEY. A message is sent by creating a new instance of the class and passing the message as a parameter. Mandrill supports the following output formats: • Json • Xml • Yaml • php

A simple implementation:

require 'mandrill'
m = Mandrill::API.new message = {
:subject=> "Hello from the Mandrill API",
:from_name=> "Your name",
:text=>"Hi message, how are you?",
:to=>[
{
:email=> "recipient@theirdomain.com",
:name=> "Recipient1" } ],
:html=>"

Body of email.

",
:from_email=>"sender@yourdomain.com"
}

sending = m.messages.send message
puts sending

A template may be useful to create a more advanced design for the email:

require 'mandrill' m = Mandrill::API.new

rendered = m.templates.render 'MyTemplate', [{:name => 'main', :content => 'The main content block'}] puts rendered['html'] # print out the rendered HTML.

More information can be found at https://mandrillapp.com/api/docs/index.ruby.html

Using Amazon SNS/SES:

Amazon SNS (simple notification service) coordinates and manages the delivery or sending of messages to subscribing endpoints or clients. This implementation option has many customization and configuration options that that may be extremely useful.

A client will need to be constructed by defining :region and :credentials: sns = Aws::SNS::Client.new( region: region_name, credentials: credentials, )

Default credentials are loaded automatically from:

• ENV['AWS_ACCESS_KEY_ID'] and ENV['AWS_SECRET_ACCESS_KEY'] • Aws.config[:credentials]

Credentials can also be configured using :access_key_id and :secret_access_key.

creds = YAML.load(File.read('/path/to/secrets'))

Aws::SNS::Client.new( access_key_id: creds['access_key_id'], secret_access_key: creds['secret_access_key'] )

Using sns.publish(), you can set the subject and message as well as other attributes of the email as parameters. This method accepts file references for the message parameter.

More information can be found at: http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS.html

Using ActionMailer and Gmail:

This implementation will use a mailer class to set the user, receiver, and subject. While this implementation is relatively straightforward, using a service like Mandrill will reduce development time and possible maintenance. Below is sample code regarding the basic implantation of ActionMailer.

class mailer < ActionMailer::base

    default from: user@example.com

    def sample_email(user)
        @user = user;
        mail(to: @user.email, subject: 'Sample email')
    end
end

In order to customize the body of the email, an HTML file must be created.

<!DOCTYPE html>
<html>
    <head>
        <meta content = 'text/html; charset=UTF-8'http-equiv = 'Content-Type'/>
    </head>
    <body>
        <h1>Hi <%= @user.name %></h1>
        <p>
            This is the body of a sample email.
        </p>
    </body>
</html>

In order to send emails from gmail, an application and production file will be needed.

Within application.rb, the account’s username and password is set. 

    gmail_username: 'useremail@gmail.com'
    gmail_password: 'userPassword'

Within production.yml, the delivery method and smpt (simple mail transfer protocol) settings are configured. 

    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
        :address    =>"workforwardnola.gmail.com",
        :port           =>587,
        :user_name      =>ENV['gmail_username'],
        :password       =>ENV['gmail_password'],
        :authentification   =>"plain",
        :enable_starttls_auto   =>true
    }

Finally, this implementation option need a controller that will trigger the 'send event'.

    def create
        @user = User.new(user_params)

        respond_to do |format|

            if @user.save
                ExampleMailer.sample_email(@user).deliver

                format.html {redirect_to @user, notice: 'User was created.' }
                format.json {render :show, status: :created, location: @user }
            else
                format.html {render :new }
                format.json {render json: @user.errors, status: :unprocessable_entity}
            end
            end
    end
tmloupe commented 7 years ago

It looks pretty simple especially if we use Mandrill. The final option includes too many files and most likely needs manual update. Amazon SNS seems like it is more secure, but also requires more work. There would need to be some documentation (we can probably take some of the documentation here) within the project if we are to use any of these, so that future coders know how the email system is working. I would give it a 3 for Mandrill, and a 5 for ActionMailer and Amazon SNS.