Module: Mailtrap::Mail

Defined in:
lib/mailtrap/mail.rb,
lib/mailtrap/mail/base.rb,
lib/mailtrap/mail/from_template.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Classes: Base, FromTemplate

Class Method Summary collapse

Class Method Details

.batch_base_from_content(from: nil, reply_to: nil, attachments: [], headers: {}, custom_variables: {}, subject: nil, text: nil, html: nil, category: nil) ⇒ Object

Builds a base mail object for batch sending with custom content (subject, text, html, category). This “base” defines shared properties for all emails in the batch, such as sender, subject, and body. Individual batch requests can override these values as needed. Use this method when you want to send multiple emails with similar custom content to different recipients.

Examples:

base_mail = Mailtrap::Mail.batch_base_from_content(
  from: { email: '[email protected]', name: 'Mailtrap Test' },
  subject: 'You are awesome!',
  text: 'Congrats for sending test email with Mailtrap!'
)
# Use base_mail as the base for batch sending with Mailtrap::Client#send_batch


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mailtrap/mail.rb', line 165

def batch_base_from_content( # rubocop:disable Metrics/ParameterLists
  from: nil,
  reply_to: nil,
  attachments: [],
  headers: {},
  custom_variables: {},
  subject: nil,
  text: nil,
  html: nil,
  category: nil
)
  Mailtrap::Mail::Base.new(
    from:,
    reply_to:,
    attachments:,
    headers:,
    custom_variables:,
    subject:,
    text:,
    html:,
    category:
  )
end

.batch_base_from_template(from: nil, reply_to: nil, attachments: [], headers: {}, custom_variables: {}, template_uuid: nil, template_variables: {}) ⇒ Object

Builds a base mail object for batch sending using a pre-defined email template. This “base” defines shared properties (such as sender, reply-to, template UUID, and template variables) that will be used as defaults for all emails in the batch. Individual batch requests can override these values. Use this method when you want to send multiple emails with similar content, leveraging a template defined in the Mailtrap dashboard. # rubocop:disable Layout/LineLength Template variables can be passed to customize the template content for all recipients, and can be overridden per request. # rubocop:disable Layout/LineLength

Examples:

base_mail = Mailtrap::Mail.batch_base_from_template(
  from: { email: '[email protected]', name: 'Mailtrap Test' },
  template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
  template_variables: {
    'user_name' => 'John Doe'
  }
)
# Use base_mail as the base for batch sending with Mailtrap::Client#send_batch


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mailtrap/mail.rb', line 134

def batch_base_from_template( # rubocop:disable Metrics/ParameterLists
  from: nil,
  reply_to: nil,
  attachments: [],
  headers: {},
  custom_variables: {},
  template_uuid: nil,
  template_variables: {}
)
  Mailtrap::Mail::Base.new(
    from:,
    reply_to:,
    attachments:,
    headers:,
    custom_variables:,
    template_uuid:,
    template_variables:
  )
end

.from_content(from: nil, to: [], reply_to: nil, cc: [], bcc: [], attachments: [], headers: {}, custom_variables: {}, subject: nil, text: nil, html: nil, category: nil) ⇒ Object

Builds a mail object with content including subject, text, html, and category.

Examples:

mail = Mailtrap::Mail.from_content(
  from: { email: '[email protected]', name: 'Mailtrap Test' },
  to: [
    { email: '[email protected]' }
  ],
  subject: 'You are awesome!',
  text: 'Congrats for sending test email with Mailtrap!'
)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mailtrap/mail.rb', line 90

def from_content( # rubocop:disable Metrics/ParameterLists
  from: nil,
  to: [],
  reply_to: nil,
  cc: [],
  bcc: [],
  attachments: [],
  headers: {},
  custom_variables: {},
  subject: nil,
  text: nil,
  html: nil,
  category: nil
)
  Mailtrap::Mail::Base.new(
    from:,
    to:,
    reply_to:,
    cc:,
    bcc:,
    attachments:,
    headers:,
    custom_variables:,
    subject:,
    text:,
    html:,
    category:
  )
end

.from_message(message) ⇒ Mailtrap::Mail::Base

Builds a mail object from Mail::Message

Parameters:

  • message (Mail::Message)

Returns:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/mailtrap/mail.rb', line 192

def from_message(message)
  Mailtrap::Mail::Base.new(
    from: prepare_addresses(message['from']).first,
    to: prepare_addresses(message['to']),
    cc: prepare_addresses(message['cc']),
    bcc: prepare_addresses(message['bcc']),
    subject: message.subject,
    text: prepare_text_part(message),
    html: prepare_html_part(message),
    headers: prepare_headers(message),
    attachments: prepare_attachments(message.attachments),
    category: message['category']&.unparsed_value,
    custom_variables: message['custom_variables']&.unparsed_value,
    template_uuid: message['template_uuid']&.unparsed_value,
    template_variables: message['template_variables']&.unparsed_value
  )
end

.from_template(from: nil, to: [], reply_to: nil, cc: [], bcc: [], attachments: [], headers: {}, custom_variables: {}, template_uuid: nil, template_variables: {}) ⇒ Object

Builds a mail object that will be sent using a pre-defined email template. The template content (subject, text, html, category) is defined in the Mailtrap dashboard and referenced by the template_uuid. Template variables can be passed to customize the template content.

Examples:

mail = Mailtrap::Mail.from_template(
  from: { email: '[email protected]', name: 'Mailtrap Test' },
  to: [
    { email: '[email protected]' }
  ],
  template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
  template_variables: {
    'user_name' => 'John Doe'
  }
)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mailtrap/mail.rb', line 54

def from_template( # rubocop:disable Metrics/ParameterLists
  from: nil,
  to: [],
  reply_to: nil,
  cc: [],
  bcc: [],
  attachments: [],
  headers: {},
  custom_variables: {},
  template_uuid: nil,
  template_variables: {}
)
  Mailtrap::Mail::Base.new(
    from:,
    to:,
    reply_to:,
    cc:,
    bcc:,
    attachments:,
    headers:,
    custom_variables:,
    template_uuid:,
    template_variables:
  )
end