Class: Mailersend::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/mailersend/email/email.rb

Overview

Send an email through MailerSend API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = Mailersend::Client.new) ⇒ Email

Returns a new instance of Email.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mailersend/email/email.rb', line 24

def initialize(client = Mailersend::Client.new)
  @client = client
  @from = {}
  @recipients = []
  @ccs = []
  @bcc = []
  @reply_to = {}
  @subject = nil
  @text = {}
  @html = {}
  @variables = []
  @personalization = []
  @attachments = []
  @tags = []
  @send_at = send_at
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def attachments
  @attachments
end

#bccObject

Returns the value of attribute bcc.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def bcc
  @bcc
end

#ccsObject

Returns the value of attribute ccs.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def ccs
  @ccs
end

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def client
  @client
end

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def from
  @from
end

#htmlObject

Returns the value of attribute html.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def html
  @html
end

#personalizationObject

Returns the value of attribute personalization.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def personalization
  @personalization
end

#recipientsObject

Returns the value of attribute recipients.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def recipients
  @recipients
end

#reply_toObject

Returns the value of attribute reply_to.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def reply_to
  @reply_to
end

#send_atObject

Returns the value of attribute send_at.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def send_at
  @send_at
end

#subjectObject

Returns the value of attribute subject.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def subject
  @subject
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def tags
  @tags
end

#template_idObject

Returns the value of attribute template_id.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def template_id
  @template_id
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def text
  @text
end

#variablesObject

Returns the value of attribute variables.



8
9
10
# File 'lib/mailersend/email/email.rb', line 8

def variables
  @variables
end

Instance Method Details

#add_attachment(content:, filename:, disposition:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mailersend/email/email.rb', line 89

def add_attachment(content:, filename:, disposition:)
  # content: Can be one of the following:
  # file_path(String) i.e. 'app/Sample-jpg-image-50kb.jpeg'
  # Base64 encoded string (String)
  # Supported types are: https://developers.mailersend.com/api/v1/email.html#supported-file-types

  content_string = content.to_s
  if File.readable?(content_string)
    data = File.read(content_string)
    base64_encoded = Base64.strict_encode64(data)
  else
    base64_encoded = content_string
  end
  @attachments << { 'content' => base64_encoded, 'filename' => filename, 'disposition' => disposition }
end

#add_bcc(bcc) ⇒ Object



53
54
55
# File 'lib/mailersend/email/email.rb', line 53

def add_bcc(bcc)
  @bcc << bcc
end

#add_cc(ccs) ⇒ Object



49
50
51
# File 'lib/mailersend/email/email.rb', line 49

def add_cc(ccs)
  @ccs << ccs
end

#add_from(from) ⇒ Object



45
46
47
# File 'lib/mailersend/email/email.rb', line 45

def add_from(from)
  @from = from
end

#add_html(html) ⇒ Object



69
70
71
# File 'lib/mailersend/email/email.rb', line 69

def add_html(html)
  @html = html
end

#add_personalization(personalization) ⇒ Object



77
78
79
# File 'lib/mailersend/email/email.rb', line 77

def add_personalization(personalization)
  @personalization << personalization
end

#add_recipients(recipients) ⇒ Object



41
42
43
# File 'lib/mailersend/email/email.rb', line 41

def add_recipients(recipients)
  @recipients << recipients
end

#add_reply_to(reply_to) ⇒ Object



57
58
59
# File 'lib/mailersend/email/email.rb', line 57

def add_reply_to(reply_to)
  @reply_to = reply_to
end

#add_send_at(send_at) ⇒ Object



105
106
107
# File 'lib/mailersend/email/email.rb', line 105

def add_send_at(send_at)
  @send_at = send_at
end

#add_subject(subject) ⇒ Object



61
62
63
# File 'lib/mailersend/email/email.rb', line 61

def add_subject(subject)
  @subject = subject
end

#add_tags(tags) ⇒ Object



85
86
87
# File 'lib/mailersend/email/email.rb', line 85

def add_tags(tags)
  @tags << tags
end

#add_template_id(template_id) ⇒ Object



81
82
83
# File 'lib/mailersend/email/email.rb', line 81

def add_template_id(template_id)
  @template_id = template_id
end

#add_text(text) ⇒ Object



65
66
67
# File 'lib/mailersend/email/email.rb', line 65

def add_text(text)
  @text = text
end

#add_variables(variables) ⇒ Object



73
74
75
# File 'lib/mailersend/email/email.rb', line 73

def add_variables(variables)
  @variables << variables
end

#sendObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mailersend/email/email.rb', line 109

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,
    'send_at' => @send_at,
    'tags' => @tags
  }

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