class Mailersend::Email

Send an email through MailerSend API

Attributes

attachments[RW]
bcc[RW]
ccs[RW]
client[RW]
from[RW]
html[RW]
personalization[RW]
recipients[RW]
reply_to[RW]
subject[RW]
tags[RW]
template_id[RW]
text[RW]
variables[RW]

Public Class Methods

new(client = Mailersend::Client.new) click to toggle source
# File lib/mailersend/email/email.rb, line 23
def initialize(client = Mailersend::Client.new)
  @client = client
  @from = {}
  @recipients = []
  @ccs = []
  @bcc = []
  @reply_to = {}
  @subject = nil
  @text = {}
  @html = {}
  @variables = []
  @personalization = []
  @attachments = []
  @tags = []
end

Public Instance Methods

add_attachment(content:, filename:) click to toggle source
# File lib/mailersend/email/email.rb, line 87
def add_attachment(content:, filename:)
  data = File.open(content.to_s).read
  encoded = Base64.strict_encode64(data)
  @attachments << {
    'content' => encoded,
    'filename' => filename
  }
end
add_bcc(bcc) click to toggle source
# File lib/mailersend/email/email.rb, line 51
def add_bcc(bcc)
  @bcc << bcc
end
add_cc(ccs) click to toggle source
# File lib/mailersend/email/email.rb, line 47
def add_cc(ccs)
  @ccs << ccs
end
add_from(from) click to toggle source
# File lib/mailersend/email/email.rb, line 43
def add_from(from)
  @from = from
end
add_html(html) click to toggle source
# File lib/mailersend/email/email.rb, line 67
def add_html(html)
  @html = html
end
add_personalization(personalization) click to toggle source
# File lib/mailersend/email/email.rb, line 75
def add_personalization(personalization)
  @personalization << personalization
end
add_recipients(recipients) click to toggle source
# File lib/mailersend/email/email.rb, line 39
def add_recipients(recipients)
  @recipients << recipients
end
add_reply_to(_reply_to) click to toggle source
# File lib/mailersend/email/email.rb, line 55
def add_reply_to(_reply_to)
  @reply_to = reply to
end
add_subject(subject) click to toggle source
# File lib/mailersend/email/email.rb, line 59
def add_subject(subject)
  @subject = subject
end
add_tags(tags) click to toggle source
# File lib/mailersend/email/email.rb, line 83
def add_tags(tags)
  @tags << tags
end
add_template_id(template_id) click to toggle source
# File lib/mailersend/email/email.rb, line 79
def add_template_id(template_id)
  @template_id = template_id
end
add_text(text) click to toggle source
# File lib/mailersend/email/email.rb, line 63
def add_text(text)
  @text = text
end
add_variables(variables) click to toggle source
# File lib/mailersend/email/email.rb, line 71
def add_variables(variables)
  @variables << variables
end
send() click to toggle source
# File lib/mailersend/email/email.rb, line 96
def send
  message = {
    'from' => @from,
    'to' => @recipients,
    'cc' => @ccs,
    'bcc' => @bcc,
    'reply_to' => @reply_to,
    'subject' => @subject,
    'text' => @text,
    'html' => @html,
    'variables' => @variables,
    'personalization' => @personalization,
    'template_id' => @template_id,
    'attachments' => @attachments
  }

  response = client.http.post("#{API_URL}/email", json: message.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} })
  puts response
  puts response.status.code
end