Class: SendGrid::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ Mail

Returns a new instance of Mail.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
# File 'lib/sendgrid/mail.rb', line 10

def initialize(params = {})
  params.each do |k, v|
    send(:"#{k}=", v) unless v.nil?
  end

  yield self if block_given?
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def attachments
  @attachments
end

#bccObject

Returns the value of attribute bcc.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def bcc
  @bcc
end

#bcc_nameObject

Returns the value of attribute bcc_name.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def bcc_name
  @bcc_name
end

#ccObject

Returns the value of attribute cc.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def cc
  @cc
end

#cc_nameObject

Returns the value of attribute cc_name.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def cc_name
  @cc_name
end

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def content
  @content
end

#dateObject

Returns the value of attribute date.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def date
  @date
end

#fromObject

Returns the value of attribute from.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def from
  @from
end

#from_nameObject

Returns the value of attribute from_name.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def from_name
  @from_name
end

#htmlObject

Returns the value of attribute html.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def html
  @html
end

#reply_toObject

Returns the value of attribute reply_to.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def reply_to
  @reply_to
end

#smtpapiObject

Returns the value of attribute smtpapi.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def smtpapi
  @smtpapi
end

#subjectObject

Returns the value of attribute subject.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def subject
  @subject
end

#templateObject

Returns the value of attribute template.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def template
  @template
end

#textObject

Returns the value of attribute text.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def text
  @text
end

#toObject

Returns the value of attribute to.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def to
  @to
end

#to_nameObject

Returns the value of attribute to_name.



7
8
9
# File 'lib/sendgrid/mail.rb', line 7

def to_name
  @to_name
end

Instance Method Details

#add_attachment(path, name = nil) ⇒ Object



93
94
95
96
97
98
# File 'lib/sendgrid/mail.rb', line 93

def add_attachment(path, name = nil)
  mime_type = MimeMagic.by_path(path)
  file = Faraday::UploadIO.new(path, mime_type)
  name ||= File.basename(file)
  attachments << {file: file, name: name}
end

#add_bcc(email, name = nil) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/sendgrid/mail.rb', line 76

def add_bcc(email, name = nil)
  if email.is_a?(Array)
    bcc.concat(email)
  else
    bcc << email
  end
  add_bcc_name(name) if name
end

#add_bcc_name(name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/sendgrid/mail.rb', line 85

def add_bcc_name(name)
  if name.is_a?(Array)
    bcc_name.concat(name)
  else
    bcc_name << name
  end
end

#add_cc(email, name = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/sendgrid/mail.rb', line 51

def add_cc(email, name = nil)
  if email.is_a?(Array)
    cc.concat(email)
  else
    cc << email
  end
  add_cc_name(name) if name
end

#add_cc_name(name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/sendgrid/mail.rb', line 60

def add_cc_name(name)
  if name.is_a?(Array)
    cc_name.concat(name)
  else
    cc_name << name
  end
end

#add_content(path, cid) ⇒ Object



112
113
114
115
116
117
# File 'lib/sendgrid/mail.rb', line 112

def add_content(path, cid)
  mime_type = MimeMagic.by_path(path)
  file = Faraday::UploadIO.new(path, mime_type)
  name ||= File.basename(file)
  contents << {file: file, cid: cid, name: name}
end

#add_to(email, name = nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/sendgrid/mail.rb', line 18

def add_to(email, name = nil)
  if email.is_a?(Array)
    to.concat(email)
  else
    to << email
  end
  add_to_name(name) if name
end

#add_to_name(name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/sendgrid/mail.rb', line 35

def add_to_name(name)
  if name.is_a?(Array)
    to_name.concat(name)
  else
    to_name << name
  end
end

#contentsObject



108
109
110
# File 'lib/sendgrid/mail.rb', line 108

def contents
  @contents ||= []
end

#headersObject



100
101
102
# File 'lib/sendgrid/mail.rb', line 100

def headers
  @headers ||= {}
end

#smtpapi_jsonObject



123
124
125
126
127
128
129
# File 'lib/sendgrid/mail.rb', line 123

def smtpapi_json
  if !template.nil? && template.is_a?(Template)
    template.add_to_smtpapi(smtpapi)
  end

  smtpapi.to_json
end

#to_hObject

rubocop:disable Style/HashSyntax



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/sendgrid/mail.rb', line 132

def to_h
  payload = {
    :from => from,
    :fromname => from_name,
    :subject => subject,
    :to => to,
    :toname => to_name,
    :date => date,
    :replyto => reply_to,
    :cc => cc,
    :ccname => cc_name,
    :bcc => bcc,
    :bccname => bcc_name,
    :text => text,
    :html => html,
    :'x-smtpapi' => smtpapi_json,
    :content => ({":default"=>"0"} unless contents.empty?),
    :files => ({":default"=>"0"} unless attachments.empty? and contents.empty?)
    # If I don't define a default value, I get a Nil error when
    # in attachments.each do |file|
    #:files => ({} unless attachments.empty?)
  }.reject {|_, v| v.nil? || v.empty?}

  payload.delete(:'x-smtpapi') if payload[:'x-smtpapi'] == '{}'

  payload[:to] = payload[:from] if payload[:to].nil? and not smtpapi.to.empty?

  unless attachments.empty?
    attachments.each do |file|
      payload[:files][file[:name]] = file[:file]
    end
    if payload[:files].has_key?(":default")
      payload[:files].delete(":default")
    end
  end

  unless contents.empty?
    contents.each do |content|
      payload[:content][content[:name]] = content[:cid]
      payload[:files][content[:name]] = content[:file]
    end
    if payload[:content].has_key?(":default")
      payload[:content].delete(":default")
    end
  end

  payload
end