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

feat: allow sending via SES #34

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions app/services/ses/email_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class SES::EmailService < BaseAwsService
# Send an email using Amazon SES
#
# @param [Hash] params Email parameters
# @option params [Array<String>] :to Recipient email addresses
# @option params [String] :from Sender email address
# @option params [String] :reply_to Reply-to email address
# @option params [String] :subject Email subject line
# @option params [String] :html HTML email content
# @option params [String] :text Plain text email content
# @option params [Hash<String, String>] :headers Additional email headers
#
# @return [AWS::SES::Types::SendEmailResponse] Response from SES API
#
# @example Basic usage
# email_service.send(
# to: ['user@example.com'],
# from: 'sender@example.com',
# reply_to: 'reply@example.com',
# subject: 'Hello',
# html: '<p>HTML content</p>',
# text: 'Text content',
# headers: {'List-Unsubscribe' => '<url>'}
# )
def send(params)
payload = build_email_payload(params)

@ses_client.send_email(payload)
end

private

def build_email_payload(params)
parsed_headers = params.fetch(:headers, {}).map { |key, value| { name: key, value: value } }

{
from_email_address: params[:from],
destination: { to_addresses: params[:to] },
reply_to_addresses: [ params[:reply_to] ],
content: {
simple: {
subject: { data: params[:subject] },
body: {
text: { data: params[:text] },
html: { data: params[:html] }
},
headers: parsed_headers
}
},
configuration_set_name: configuration_set
}
end

def configuration_set
@configuration_set ||= AppConfig.get("AWS_SES_CONFIGURATION_SET")
end
end