Class: PostmarkClient::Email
- Inherits:
-
Object
- Object
- PostmarkClient::Email
- Defined in:
- lib/postmark_client/models/email.rb
Overview
Represents an email message for the Postmark API. Provides a clean Ruby interface for building email payloads.
Constant Summary collapse
- TRACK_LINKS_OPTIONS =
Valid link tracking options
%w[None HtmlAndText HtmlOnly TextOnly].freeze
Instance Attribute Summary collapse
-
#attachments ⇒ Array<Attachment>
Email attachments.
-
#bcc ⇒ String, ...
BCC recipient email address(es).
-
#cc ⇒ String, ...
CC recipient email address(es).
-
#from ⇒ String
Sender email address.
-
#headers ⇒ Array<Hash>
Custom email headers.
-
#html_body ⇒ String?
HTML email body.
-
#message_stream ⇒ String
Message stream identifier.
-
#metadata ⇒ Hash
Custom metadata key-value pairs.
-
#reply_to ⇒ String?
Reply-to email address.
-
#subject ⇒ String
Email subject.
-
#tag ⇒ String?
Email tag for categorization.
-
#text_body ⇒ String?
Plain text email body.
-
#to ⇒ String+
Recipient email address(es).
-
#track_links ⇒ String
Link tracking setting (“None”, “HtmlAndText”, “HtmlOnly”, “TextOnly”).
-
#track_opens ⇒ Boolean
Whether to track email opens.
Instance Method Summary collapse
-
#add_attachment(attachment = nil, **kwargs) ⇒ self
Add an attachment to the email.
-
#add_header(name:, value:) ⇒ self
Add a custom header to the email.
-
#add_metadata(key, value) ⇒ self
Add metadata to the email.
-
#attach_file(file_path, content_type: nil, content_id: nil) ⇒ self
Add a file as an attachment.
-
#initialize(from: nil, to: nil, cc: nil, bcc: nil, subject: nil, html_body: nil, text_body: nil, reply_to: nil, tag: nil, headers: nil, track_opens: nil, track_links: nil, attachments: nil, metadata: nil, message_stream: nil) ⇒ Email
constructor
Initialize a new email.
-
#to_h ⇒ Hash
(also: #to_api_hash)
Convert the email to a hash for API requests.
-
#valid? ⇒ Boolean
Check if the email is valid.
-
#validate! ⇒ Boolean
Validate the email before sending.
Constructor Details
#initialize(from: nil, to: nil, cc: nil, bcc: nil, subject: nil, html_body: nil, text_body: nil, reply_to: nil, tag: nil, headers: nil, track_opens: nil, track_links: nil, attachments: nil, metadata: nil, message_stream: nil) ⇒ Email
Initialize a new email
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/postmark_client/models/email.rb', line 99 def initialize( from: nil, to: nil, cc: nil, bcc: nil, subject: nil, html_body: nil, text_body: nil, reply_to: nil, tag: nil, headers: nil, track_opens: nil, track_links: nil, attachments: nil, metadata: nil, message_stream: nil ) @from = from @to = to @cc = cc @bcc = bcc @subject = subject @html_body = html_body @text_body = text_body @reply_to = reply_to @tag = tag @headers = headers || [] @track_opens = track_opens @track_links = track_links = || [] = || {} = || PostmarkClient.configuration. end |
Instance Attribute Details
#attachments ⇒ Array<Attachment>
Returns email attachments.
71 72 73 |
# File 'lib/postmark_client/models/email.rb', line 71 def end |
#bcc ⇒ String, ...
Returns BCC recipient email address(es).
44 45 46 |
# File 'lib/postmark_client/models/email.rb', line 44 def bcc @bcc end |
#cc ⇒ String, ...
Returns CC recipient email address(es).
41 42 43 |
# File 'lib/postmark_client/models/email.rb', line 41 def cc @cc end |
#from ⇒ String
Returns sender email address.
35 36 37 |
# File 'lib/postmark_client/models/email.rb', line 35 def from @from end |
#headers ⇒ Array<Hash>
Returns custom email headers.
62 63 64 |
# File 'lib/postmark_client/models/email.rb', line 62 def headers @headers end |
#html_body ⇒ String?
Returns HTML email body.
50 51 52 |
# File 'lib/postmark_client/models/email.rb', line 50 def html_body @html_body end |
#message_stream ⇒ String
Returns message stream identifier.
77 78 79 |
# File 'lib/postmark_client/models/email.rb', line 77 def end |
#metadata ⇒ Hash
Returns custom metadata key-value pairs.
74 75 76 |
# File 'lib/postmark_client/models/email.rb', line 74 def end |
#reply_to ⇒ String?
Returns reply-to email address.
56 57 58 |
# File 'lib/postmark_client/models/email.rb', line 56 def reply_to @reply_to end |
#subject ⇒ String
Returns email subject.
47 48 49 |
# File 'lib/postmark_client/models/email.rb', line 47 def subject @subject end |
#tag ⇒ String?
Returns email tag for categorization.
59 60 61 |
# File 'lib/postmark_client/models/email.rb', line 59 def tag @tag end |
#text_body ⇒ String?
Returns plain text email body.
53 54 55 |
# File 'lib/postmark_client/models/email.rb', line 53 def text_body @text_body end |
#to ⇒ String+
Returns recipient email address(es).
38 39 40 |
# File 'lib/postmark_client/models/email.rb', line 38 def to @to end |
#track_links ⇒ String
Returns link tracking setting (“None”, “HtmlAndText”, “HtmlOnly”, “TextOnly”).
68 69 70 |
# File 'lib/postmark_client/models/email.rb', line 68 def track_links @track_links end |
#track_opens ⇒ Boolean
Returns whether to track email opens.
65 66 67 |
# File 'lib/postmark_client/models/email.rb', line 65 def track_opens @track_opens end |
Instance Method Details
#add_attachment(attachment) ⇒ self #add_attachment(name:, content:, content_type:, content_id: nil) ⇒ self
Add an attachment to the email
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/postmark_client/models/email.rb', line 156 def ( = nil, **kwargs) if .is_a?(Attachment) << elsif kwargs.any? << Attachment.new(**kwargs) else raise ArgumentError, "Must provide an Attachment instance or attachment parameters" end self end |
#add_header(name:, value:) ⇒ self
Add a custom header to the email
138 139 140 141 |
# File 'lib/postmark_client/models/email.rb', line 138 def add_header(name:, value:) @headers << { "Name" => name, "Value" => value } self end |
#add_metadata(key, value) ⇒ self
Add metadata to the email
183 184 185 186 |
# File 'lib/postmark_client/models/email.rb', line 183 def (key, value) [key.to_s] = value self end |
#attach_file(file_path, content_type: nil, content_id: nil) ⇒ self
Add a file as an attachment
173 174 175 176 |
# File 'lib/postmark_client/models/email.rb', line 173 def attach_file(file_path, content_type: nil, content_id: nil) << Attachment.from_file(file_path, content_type: content_type, content_id: content_id) self end |
#to_h ⇒ Hash Also known as: to_api_hash
Convert the email to a hash for API requests
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/postmark_client/models/email.rb', line 217 def to_h hash = {} hash["From"] = from if from hash["To"] = normalize_recipients(to) if to hash["Cc"] = normalize_recipients(cc) if cc hash["Bcc"] = normalize_recipients(bcc) if bcc hash["Subject"] = subject if subject hash["HtmlBody"] = html_body if html_body hash["TextBody"] = text_body if text_body hash["ReplyTo"] = reply_to if reply_to hash["Tag"] = tag if tag hash["Headers"] = headers if headers.any? hash["TrackOpens"] = track_opens unless track_opens.nil? hash["TrackLinks"] = track_links if track_links hash["Attachments"] = .map(&:to_h) if .any? hash["Metadata"] = if .any? hash["MessageStream"] = if hash end |
#valid? ⇒ Boolean
Check if the email is valid
207 208 209 210 211 212 |
# File 'lib/postmark_client/models/email.rb', line 207 def valid? validate! true rescue ValidationError false end |
#validate! ⇒ Boolean
Validate the email before sending
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/postmark_client/models/email.rb', line 192 def validate! raise ValidationError, "From address is required" if from.nil? || from.empty? raise ValidationError, "To address is required" if to.nil? || (to.is_a?(Array) && to.empty?) || (to.is_a?(String) && to.empty?) raise ValidationError, "Either HtmlBody or TextBody is required" if (html_body.nil? || html_body.empty?) && (text_body.nil? || text_body.empty?) if track_links && !TRACK_LINKS_OPTIONS.include?(track_links) raise ValidationError, "TrackLinks must be one of: #{TRACK_LINKS_OPTIONS.join(', ')}" end true end |