Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to SparkPost #1332

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ export GITHUB_TEST_APP_CLIENT_SECRET=
export GITHUB_TEST_APP_ID=
export GITHUB_TEST_APP_PEM=
export INTERCOM_IDENTITY_SECRET_KEY=
export POSTMARK_API_KEY=
export S3_BUCKET=
export SCOUT_APP_KEY=
export SCOUT_APP_NAME=
export SEGMENT_WRITE_KEY=
export SENTRY_DSN=
export STRIPE_SECRET_KEY=
export STRIPE_PLATFORM_CLIENT_ID=
export SPARKPOST_API_KEY=
export SPARKPOST_FORGOT_PASSWORD_TEMPLATE=
export SPARKPOST_MESSAGE_INITIATED_BY_PROJECT_TEMPLATE=
export SPARKPOST_ORGANIZATION_INVITE_TEMPLATE=
export SPARKPOST_PROJECT_APPROVAL_REQUEST_TEMPLATE=
export SPARKPOST_PROJECT_APPROVED_TEMPLATE=
export SPARKPOST_PROJECT_USER_ACCEPTANCE_TEMPLATE=
export SPARKPOST_PROJECT_USER_REQUEST_TEMPLATE=
export SPARKPOST_RECEIPT_TEMPLATE=
export SPARKPOST_REPLY_TO_CONVERSATION_TEMPLATE=
20 changes: 18 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ config :code_corps, CodeCorpsWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "eMl0+Byu0Zv7q48thBu23ChBVFO1+sdLqoMI8yZoxEviF1K3C5uIohbDfvM9felL",
render_errors: [view: CodeCorpsWeb.ErrorView, accepts: ~w(html json json-api)],
pubsub: [name: CodeCorps.PubSub,
adapter: Phoenix.PubSub.PG2]
pubsub: [name: CodeCorps.PubSub, adapter: Phoenix.PubSub.PG2]

# Configures Elixir's Logger
config :logger, :console,
Expand All @@ -31,6 +30,17 @@ config :mime, :types, %{
"application/vnd.api+json" => ["json-api"]
}

config :code_corps,
sparkpost_forgot_password_template: System.get_env("SPARKPOST_FORGOT_PASSWORD_TEMPLATE") || "forgot-password",
sparkpost_message_initiated_by_project_template: System.get_env("SPARKPOST_MESSAGE_INITIATED_BY_PROJECT_TEMPLATE") || "message-initiated-by-project",
sparkpost_organization_invite_template: System.get_env("SPARKPOST_ORGANIZATION_INVITE_TEMPLATE") || "organization-invite",
sparkpost_project_approval_request_template: System.get_env("SPARKPOST_PROJECT_APPROVAL_REQUEST_TEMPLATE") || "project-approval-request",
sparkpost_project_approved_template: System.get_env("SPARKPOST_PROJECT_APPROVED_TEMPLATE") || "project-approved",
sparkpost_project_user_acceptance_template: System.get_env("SPARKPOST_PROJECT_USER_ACCEPTANCE_TEMPLATE") || "project-user-acceptance",
sparkpost_project_user_request_template: System.get_env("SPARKPOST_PROJECT_USER_REQUEST_TEMPLATE") || "project-user-request",
sparkpost_receipt_template: System.get_env("SPARKPOST_RECEIPT_TEMPLATE") || "receipt",
sparkpost_reply_to_conversation_template: System.get_env("SPARKPOST_REPLY_TO_CONVERSATION_TEMPLATE") || "reply-to-conversation"

config :code_corps, CodeCorps.Guardian,
issuer: "CodeCorps",
ttl: { 30, :days },
Expand Down Expand Up @@ -72,6 +82,9 @@ config :code_corps,
github_app_client_secret: System.get_env("GITHUB_APP_CLIENT_SECRET"),
github_app_pem: pem

config :code_corps,
sparkpost: CodeCorps.Emails.ExtendedAPI

config :stripity_stripe,
api_key: System.get_env("STRIPE_SECRET_KEY"),
connect_client_id: System.get_env("STRIPE_PLATFORM_CLIENT_ID")
Expand All @@ -81,6 +94,9 @@ config :sentry,
enable_source_code_context: true,
included_environments: ~w(prod staging)a

config :sparkpost,
api_key: System.get_env("SPARKPOST_API_KEY")

config :code_corps, :sentry, CodeCorps.Sentry.Async

config :code_corps, :processor, CodeCorps.Processor.Async
Expand Down
13 changes: 0 additions & 13 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ config :code_corps, :stripe_env, :dev

config :sentry, environment_name: Mix.env() || :dev

config :code_corps, CodeCorps.Mailer, adapter: Bamboo.LocalAdapter

config :code_corps,
postmark_forgot_password_template: "123",
postmark_message_initiated_by_project_template: "123",
postmark_organization_invite_email_template: "123",
postmark_project_approval_request_template: "123",
postmark_project_approved_template: "123",
postmark_project_user_acceptance_template: "123",
postmark_project_user_request_template: "123",
postmark_receipt_template: "123",
postmark_reply_to_conversation_template: "123"

# If the dev environment has no CLOUDEX_API_KEY set, we want the app
# to still run, with cloudex in test API mode
if System.get_env("CLOUDEX_API_KEY") == nil do
Expand Down
15 changes: 0 additions & 15 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ config :code_corps, :stripe_env, :prod
config :sentry,
environment_name: Mix.env || :prod

config :code_corps, CodeCorps.Mailer,
adapter: Bamboo.PostmarkAdapter,
api_key: System.get_env("POSTMARK_API_KEY")

config :code_corps,
postmark_forgot_password_template: "1989483",
postmark_message_initiated_by_project_template: "4324242",
postmark_organization_invite_email_template: "3441863",
postmark_project_approval_request_template: "4105824",
postmark_project_approved_template: "4105822",
postmark_project_user_acceptance_template: "1447041",
postmark_project_user_request_template: "4017262",
postmark_receipt_template: "1255222",
postmark_reply_to_conversation_template: "4324082"

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
14 changes: 0 additions & 14 deletions config/remote-development.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ config :logger, level: :info
config :code_corps, :stripe, Stripe
config :code_corps, :stripe_env, :remote_dev

config :code_corps, CodeCorps.Mailer,
adapter: Bamboo.LocalAdapter

config :code_corps,
postmark_forgot_password_template: "123",
postmark_message_initiated_by_project_template: "123",
postmark_organization_invite_email_template: "123",
postmark_project_approval_request_template: "123",
postmark_project_approved_template: "123",
postmark_project_user_acceptance_template: "123",
postmark_project_user_request_template: "123",
postmark_receipt_template: "123",
postmark_reply_to_conversation_template: "123"

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
15 changes: 0 additions & 15 deletions config/staging.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,6 @@ config :sentry,
config :code_corps, :stripe, Stripe
config :code_corps, :stripe_env, :staging

config :code_corps, CodeCorps.Mailer,
adapter: Bamboo.PostmarkAdapter,
api_key: System.get_env("POSTMARK_API_KEY")

config :code_corps,
postmark_forgot_password_template: "1989481",
postmark_message_initiated_by_project_template: "4324241",
postmark_organization_invite_email_template: "3442401",
postmark_project_approval_request_template: "4105823",
postmark_project_approved_template: "4105744",
postmark_project_user_acceptance_template: "1447022",
postmark_project_user_request_template: "4017261",
postmark_receipt_template: "1252361",
postmark_reply_to_conversation_template: "4324243"

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
17 changes: 3 additions & 14 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,12 @@ config :code_corps,
config :sentry,
environment_name: Mix.env || :test

config :code_corps,
sparkpost: CodeCorps.SparkPostTesting.SuccessAPI

config :code_corps, :sentry, CodeCorps.Sentry.Sync

config :code_corps, :processor, CodeCorps.Processor.Sync

config :code_corps, CodeCorps.Mailer,
adapter: Bamboo.TestAdapter

config :code_corps,
postmark_forgot_password_template: "123",
postmark_message_initiated_by_project_template: "123",
postmark_organization_invite_email_template: "123",
postmark_project_approval_request_template: "123",
postmark_project_approved_template: "123",
postmark_project_user_acceptance_template: "123",
postmark_project_user_request_template: "123",
postmark_receipt_template: "123",
postmark_reply_to_conversation_template: "123"

config :code_corps, :cloudex, CloudexTest
config :cloudex, api_key: "test_key", secret: "test_secret", cloud_name: "test_cloud_name"
3 changes: 2 additions & 1 deletion emails/forgot_password.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Here's the link to reset your password.</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/message_initiated_by_project.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>You have a new message from {{project_title}}.</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/organization_invite.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Create your first project on Code Corps.</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/project_approval_request.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{{project_title}} is asking to be approved</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/project_approved.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{{project_title}} is approved!</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/project_user_acceptance.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{{project_title}} just added you as a contributor</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/project_user_request.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{{user_first_name}} wants to join {{project_title}}</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/receipt.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Your monthly donation on Code Corps</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
3 changes: 2 additions & 1 deletion emails/reply_to_conversation.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{{author_name}} replied to your conversation in {{project_title}}.</title>
<!--
Make sure you copy the styles from styles.css into the email template in Postmark before saving there.
If saving manually and not using mix.templates.update, make sure to copy
contents of style.scss into the style tag here.

<style type="text/css" rel="stylesheet" media="all">
</style>
Expand Down
46 changes: 46 additions & 0 deletions lib/code_corps/emails/api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule CodeCorps.Emails.API do
@moduledoc ~S"""
A wrapper for the SparkPost API.

All API requests should go through functions in this module.
"""
require Logger

@type transmission_result :: {:ok, %SparkPost.Transmission.Response{}} |
{:error, %SparkPost.Endpoint.Error{}}

defp api, do: Application.get_env(:code_corps, :sparkpost)

def create_template(body \\ %{}, headers \\ [], options \\ []) do
body |> api().create_template(headers, options) |> marshall_response()
end

def update_template(id, body \\ %{}, headers \\ [], options \\ []) do
id |> api().update_template(body, headers, options) |> marshall_response()
end

@doc ~S"""
Sends a transmission using the provided email map
"""
@spec send_transmission(%SparkPost.Transmission{}) :: transmission_result
def send_transmission(%SparkPost.Transmission{} = content) do
content |> api().send_transmission() |> marshall_response()
end

defp marshall_response(%SparkPost.Transmission.Response{} = response), do: {:ok, response}
defp marshall_response(%SparkPost.Content.Inline{} = response), do: {:ok, response}
defp marshall_response(%SparkPost.Endpoint.Error{} = error), do: {:error, error}
# Extended API responses
defp marshall_response({:ok, %HTTPoison.Response{status_code: code} = response})
when code in 200..399, do: {:ok, response}
defp marshall_response({:ok, %HTTPoison.Response{status_code: code} = response})
when code in 400..599, do: {:error, response |> build_error()}
defp marshall_response({:error, %HTTPoison.Error{} = error}), do: {:error, error |> build_error()}

defp build_error(%HTTPoison.Response{status_code: code, body: %{"errors" => errors}}) do
%SparkPost.Endpoint.Error{status_code: code, errors: errors}
end
defp build_error(%HTTPoison.Error{id: id, reason: reason}) do
%SparkPost.Endpoint.Error{status_code: nil, errors: [{id, reason}]}
end
end
14 changes: 0 additions & 14 deletions lib/code_corps/emails/base_email.ex

This file was deleted.

Loading