Skip to content

Commit

Permalink
Merge pull request #141 from dxw/v2
Browse files Browse the repository at this point in the history
Version 2
  • Loading branch information
mec committed Apr 22, 2024
2 parents d27bec2 + 77c32c1 commit 8f6709f
Show file tree
Hide file tree
Showing 30 changed files with 764 additions and 478 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog]

## [Unreleased]

## [2.0.0] - 2024-04-01

- Version 2.0.0 rewrites most of the gem, without altering the API
- The gem no longer prepends code in ActionMailer
- Previews are now 100% supported in Rails, but will require a Notify API key
- The preview layout has been updated and uses the Tudor Crown
- This version fixes a bug that meant not all Rails preview functionality would
work
- This version fixes a bug that meant no Rails preview functionality would work
from version 7.1.0 onwards

## [1.2.0] - 2023-04-28

- Requiring Ruby 2.7.8
Expand Down Expand Up @@ -71,7 +82,10 @@ The format is based on [Keep a Changelog]

- Initial release

[unreleased]: https://github.com/DFE-Digital/dfe-teachers-payment-service/compare/1.0.3...HEAD
[unreleased]: https://github.com/dxw/mail-notify/compare/2.0.0...HEAD
[2.0.0]: https://github.com/dxw/mail-notify/compare/1.2.0...2.0.0
[1.2.0]: https://github.com/dxw/mail-notify/compare/1.1.0...1.2.0
[1.1.0]: https://github.com/dxw/mail-notify/compare/1.0.5...1.1.0
[1.0.5]: https://github.com/dxw/mail-notify/compare/1.0.4...1.0.5
[1.0.4]: https://github.com/dxw/mail-notify/compare/1.0.3...1.0.4
[1.0.3]: https://github.com/dxw/mail-notify/compare/1.0.2...1.0.3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<%# Copied from GOV.UK Notify: https://raw.githubusercontent.com/alphagov/notifications-utils/d69de041d1be7b826e7d9cc56450e157ad361799/notifications_utils/jinja_templates/email_template.jinja2 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Template sourced from https://github.com/alphagov/notifications-utils/blob/main/notifications_utils/jinja_templates/email_template.jinja2 -->
<!DOCTYPE html>
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="telephone=no" name="format-detection" /> <!-- need to add formatting for real phone numbers -->
<meta name="viewport" content="width=device-width" />
<title>Page title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="telephone=no" name="format-detection"> <!-- need to add formatting for real phone numbers -->
<meta name="viewport" content="width=device-width">
<title>{{ subject }}</title>

<style type="text/css">
<style>
@media only screen and (min-device-width: 581px) {
.content {
width: 580px !important;
Expand All @@ -19,7 +19,7 @@
</style>

<!--[if gte mso 9]>
<style type="text/css">
<style>
li {
margin-left: 4px !important;
}
Expand All @@ -33,8 +33,7 @@
</head>

<body style="font-family: Helvetica, Arial, sans-serif;font-size: 16px;margin: 0;color:#0b0c0c;">

<span style="display: none;font-size: 1px;color: #fff; max-height: 0;"></span>
<span style="display: none;font-size: 1px;color: #fff; max-height: 0;" hidden>{{ preheader }}…</span>
<table role="presentation" width="100%" style="border-collapse: collapse;min-width: 100%;width: 100% !important;" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="100%" height="53" bgcolor="#0b0c0c">
Expand All @@ -51,20 +50,20 @@
<tr>
<td style="padding-left: 10px">
<img
src="https://static.notifications.service.gov.uk/images/gov.uk_logotype_crown.png"
alt=" "
src="https://static.notifications.service.gov.uk/images/govuk-logotype-tudor-crown.png"
alt=""
height="32"
border="0"
style="Margin-top: 4px;"
/>
style="Margin-top: 2px;"
>
</td>
<td style="font-size: 28px; line-height: 1.315789474; Margin-top: 4px; padding-left: 10px;">
<td style="font-size: 28px; line-height: 1.315789474; Margin-top: 4px; padding-left: 8px;">
<span style="
font-family: Helvetica, Arial, sans-serif;
font-weight: 700;
color: #ffffff;
text-decoration: none;
vertical-align:top;
vertical-align:middle;
display: inline-block;
">GOV.UK</span>
</td>
Expand Down Expand Up @@ -102,7 +101,7 @@
<![endif]-->
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse;">
<tr>
<td bgcolor="#005EA5" width="100%" height="10"></td>
<td bgcolor="#1D70B8" width="100%" height="10"></td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
Expand All @@ -114,7 +113,6 @@
<td width="10" valign="middle" height="10"></td>
</tr>
</table>

<table
role="presentation"
class="content"
Expand All @@ -126,27 +124,27 @@
width="100%"
>
<tr>
<td height="30"><br /></td>
<td height="30"><br></td>
</tr>
<tr>
<td width="10" valign="middle"><br /></td>
<td width="10" valign="middle"><br></td>
<td style="font-family: Helvetica, Arial, sans-serif; font-size: 19px; line-height: 1.315789474; max-width: 560px;">
<!--[if (gte mso 9)|(IE)]>
<table role="presentation" width="560" align="center" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse;width: 560px;">
<tr>
<td style="font-family: Helvetica, Arial, sans-serif; font-size: 19px; line-height: 1.315789474;">
<![endif]-->
<%= yield %>
<![endif]-->
<%= yield %>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
<td width="10" valign="middle"><br /></td>
<td width="10" valign="middle"><br></td>
</tr>
<tr>
<td height="30"><br /></td>
<td height="30"><br></td>
</tr>
</table>
</body>
Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require 'bundler/setup'
require 'action_mailer'
require 'action_controller'
require 'mail/notify'
require 'pry'

Expand Down
6 changes: 3 additions & 3 deletions lib/mail/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
require "notifications/client"

require "mail/notify/version"
require "mail/notify/railtie" if defined? Rails
require "mail/notify/engine" if defined? Rails
require "mail/notify/delivery_method"
require "mail/notify/personalisation"
require "mail/notify/mailer"
require "mail/notify/message"
require "mail/notify/mailers_controller"
require "mail/notify/mail_notify_previews_controller"
require "mail/notify/mail_notify_preview_interceptor"

Mail::Message.include Mail::Notify::Message

Expand Down
42 changes: 16 additions & 26 deletions lib/mail/notify/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ class DeliveryMethod
attr_accessor :settings, :response

def initialize(settings)
raise ArgumentError, "You must specify an API key" if settings[:api_key].blank?
raise ArgumentError, "You must specify a Notify API key" if settings[:api_key].blank?

@settings = settings
end

def deliver!(mail)
@mail = mail
@personalisation = Personalisation.new(mail)
send_email
def deliver!(message)
params = {
template_id: message.template_id,
email_address: message.to.first,
personalisation: message.personalisation,
email_reply_to_id: message.reply_to_id,
reference: message.reference
}

client.send_email(params.compact)
end

def preview(mail)
personalisation = Personalisation.new(mail).to_h
template_id = mail[:template_id].to_s
def preview(message)
template_id = message.template_id
personalisation = message.personalisation

Rails.logger.info("Getting Notify preview for template id #{template_id}")
client.generate_template_preview(template_id, personalisation: personalisation)
end

Expand All @@ -28,24 +36,6 @@ def preview(mail)
def client
@client ||= Notifications::Client.new(@settings[:api_key], @settings[:base_url])
end

def email_params
{
email_address: @mail.to.first,
template_id: @mail[:template_id].to_s,
personalisation: @personalisation.to_h,
email_reply_to_id: optional_param(:reply_to_id),
reference: optional_param(:reference)
}
end

def optional_param(name)
@mail[name].presence&.to_s
end

def send_email
@response = client.send_email(email_params.compact)
end
end
end
end
13 changes: 13 additions & 0 deletions lib/mail/notify/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Mail
module Notify
class Engine < Rails::Engine
initializer "mail-notify.add_delivery_method", before: "action_mailer.set_configs" do
ActionMailer::Base.add_delivery_method(:notify, Mail::Notify::DeliveryMethod)

config.action_mailer.preview_interceptors = [:mail_notify_preview_interceptor]
end
end
end
end
51 changes: 51 additions & 0 deletions lib/mail/notify/mail_notify_preview_interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

##
# This is a ActionMailer interceptor class for previews from the Notifications API
#
# See ActionMailer::Base:
#
# https://github.com/rails/rails/blob/v5.2.8.1/actionmailer/lib/action_mailer/base.rb#L367

class MailNotifyPreviewInterceptor
##
# ActionMailer call back when a preview is being generated.
#
# Transforms the content of the passed in message.

def self.previewing_email(message)
new(message).transform!
end

##
# Creates a new MailNotifyPreviewInterceptor ready for use.

def initialize(message)
@message = message
end

##
# Transforms the content of the message to that from the Notifications API preview.
#
# The html is wrapped in a layout and rendered by the MailNotifyPreviewsControllers renderer.

def transform!
# the messages delivery method will be our Mail::Notify::DeliveryMethod and have the `preview` method.
preview = @message.delivery_method.preview(@message)

@message.subject = preview.subject
@message.html_part.body = renderer.render html: preview.html.html_safe, layout: "govuk_notify_layout"
@message.text_part.body = preview.body

@message
end

private

def renderer
# rendering in Rails without a controller gets far too complicated, instead
# we rely on this empty controller to do it for us.

MailNotifyPreviewsController.renderer
end
end
6 changes: 6 additions & 0 deletions lib/mail/notify/mail_notify_previews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

##
# Empty controller used by MailNotifyPreviewInterceptor to render html.

class MailNotifyPreviewsController < ActionController::Base; end

0 comments on commit 8f6709f

Please sign in to comment.