Class: SendGrid::Mail
- Inherits:
-
Object
- Object
- SendGrid::Mail
- Defined in:
- lib/sendgrid/mail.rb
Instance Attribute Summary collapse
-
#attachments ⇒ Object
Returns the value of attribute attachments.
-
#bcc ⇒ Object
Returns the value of attribute bcc.
-
#bcc_name ⇒ Object
Returns the value of attribute bcc_name.
-
#cc ⇒ Object
Returns the value of attribute cc.
-
#cc_name ⇒ Object
Returns the value of attribute cc_name.
-
#content ⇒ Object
Returns the value of attribute content.
-
#date ⇒ Object
Returns the value of attribute date.
-
#from ⇒ Object
Returns the value of attribute from.
-
#from_name ⇒ Object
Returns the value of attribute from_name.
-
#html ⇒ Object
Returns the value of attribute html.
-
#reply_to ⇒ Object
Returns the value of attribute reply_to.
-
#smtpapi ⇒ Object
Returns the value of attribute smtpapi.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#template ⇒ Object
Returns the value of attribute template.
-
#text ⇒ Object
Returns the value of attribute text.
-
#to ⇒ Object
Returns the value of attribute to.
-
#to_name ⇒ Object
Returns the value of attribute to_name.
Instance Method Summary collapse
- #add_attachment(path, name = nil) ⇒ Object
- #add_bcc(email, name = nil) ⇒ Object
- #add_bcc_name(name) ⇒ Object
- #add_cc(email, name = nil) ⇒ Object
- #add_cc_name(name) ⇒ Object
- #add_content(path, cid) ⇒ Object
- #add_to(email, name = nil) ⇒ Object
- #add_to_name(name) ⇒ Object
- #contents ⇒ Object
- #headers ⇒ Object
-
#initialize(params = {}) {|_self| ... } ⇒ Mail
constructor
A new instance of Mail.
- #smtpapi_json ⇒ Object
-
#to_h ⇒ Object
rubocop:disable Style/HashSyntax.
Constructor Details
#initialize(params = {}) {|_self| ... } ⇒ Mail
Returns a new instance of Mail.
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
#attachments ⇒ Object
Returns the value of attribute attachments.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def end |
#bcc ⇒ Object
Returns the value of attribute bcc.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def bcc @bcc end |
#bcc_name ⇒ Object
Returns the value of attribute bcc_name.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def bcc_name @bcc_name end |
#cc ⇒ Object
Returns the value of attribute cc.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def cc @cc end |
#cc_name ⇒ Object
Returns the value of attribute cc_name.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def cc_name @cc_name end |
#content ⇒ Object
Returns the value of attribute content.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def content @content end |
#date ⇒ Object
Returns the value of attribute date.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def date @date end |
#from ⇒ Object
Returns the value of attribute from.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def from @from end |
#from_name ⇒ Object
Returns the value of attribute from_name.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def from_name @from_name end |
#html ⇒ Object
Returns the value of attribute html.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def html @html end |
#reply_to ⇒ Object
Returns the value of attribute reply_to.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def reply_to @reply_to end |
#smtpapi ⇒ Object
Returns the value of attribute smtpapi.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def smtpapi @smtpapi end |
#subject ⇒ Object
Returns the value of attribute subject.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def subject @subject end |
#template ⇒ Object
Returns the value of attribute template.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def template @template end |
#text ⇒ Object
Returns the value of attribute text.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def text @text end |
#to ⇒ Object
Returns the value of attribute to.
7 8 9 |
# File 'lib/sendgrid/mail.rb', line 7 def to @to end |
#to_name ⇒ Object
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 (path, name = nil) mime_type = MimeMagic.by_path(path) file = Faraday::UploadIO.new(path, mime_type) name ||= File.basename(file) << {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 |
#contents ⇒ Object
108 109 110 |
# File 'lib/sendgrid/mail.rb', line 108 def contents @contents ||= [] end |
#headers ⇒ Object
100 101 102 |
# File 'lib/sendgrid/mail.rb', line 100 def headers @headers ||= {} end |
#smtpapi_json ⇒ Object
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_h ⇒ Object
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 .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 .empty? .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 |