Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

RESEARCH: figure out email solution (2) #22

Open
maahern opened this issue Oct 19, 2017 · 2 comments
Open

RESEARCH: figure out email solution (2) #22

maahern opened this issue Oct 19, 2017 · 2 comments
Assignees

Comments

@maahern
Copy link

maahern commented Oct 19, 2017

--4 hours

@maahern maahern self-assigned this Oct 27, 2017
@maahern
Copy link
Author

maahern commented Nov 7, 2017

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=> "[email protected]",
:name=> "Recipient1" } ],
:html=>"

Body of email.

",
:from_email=>"[email protected]"
}

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: [email protected]

	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: '[email protected]'
	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
Copy link

tmloupe commented Nov 7, 2017

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants