Class: SendLayer::Emails
- Inherits:
-
Object
- Object
- SendLayer::Emails
- Defined in:
- lib/sendlayer/emails.rb
Instance Method Summary collapse
-
#initialize(client) ⇒ Emails
constructor
A new instance of Emails.
- #send(from:, to:, subject:, text: nil, html: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil, tags: nil) ⇒ Object
Constructor Details
#initialize(client) ⇒ Emails
Returns a new instance of Emails.
16 17 18 |
# File 'lib/sendlayer/emails.rb', line 16 def initialize(client) @client = client end |
Instance Method Details
#send(from:, to:, subject:, text: nil, html: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil, tags: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sendlayer/emails.rb', line 20 def send(from:, to:, subject:, text: nil, html: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil, tags: nil) # Empty strings are treated as absent, so a caller passing text: '' gets a # validation error rather than an email with no body. has_html = !html.nil? && html != '' has_text = !text.nil? && text != '' # Validate required parameters raise SendLayerValidationError.new("Either 'text' or 'html' content must be provided") unless has_html || has_text # Prepare email data email_data = { From: normalize_recipient(from, 'sender'), To: normalize_recipients(to, 'recipient'), Subject: subject } # Both parts are sent when both are supplied. The previous if/else could # only ever emit one of them, which silently dropped the plain-text part. # HTML wins for the declared content type whenever an HTML body is present. email_data[:ContentType] = has_html ? 'HTML' : 'Text' email_data[:HTMLContent] = html if has_html email_data[:PlainContent] = text if has_text email_data[:CC] = normalize_recipients(cc) if cc email_data[:BCC] = normalize_recipients(bcc) if bcc email_data[:ReplyTo] = normalize_recipients(reply_to, 'reply_to') if reply_to email_data[:Headers] = headers if headers if unless .is_a?(Array) && .all? { |t| t.is_a?(String) } raise SendLayerValidationError.new('Tags must be a list of strings') end email_data[:Tags] = end # Handle attachments if && !.empty? email_data[:Attachments] = () end @client.make_request('POST', 'email', email_data) end |