Class: Sendgrid::Web::Mail

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

Instance Method Summary collapse

Methods inherited from Client

base_uri, config, #config, configure

Instance Method Details

#send(to: nil, to_name: nil, x_smtpapi: nil, subject: nil, text: nil, html: nil, from: nil, bcc: nil, from_name: nil, reply_to: nil, date: nil, files: nil, content: nil, headers: nil) ⇒ Sendgrid::Web::Response

Note:

The required parameters are: from, text/html, subject and to.

Send an email.

Parameters:

  • to (String|Array<String>) (defaults to: nil)

    This can also be passed in as an array, to send to multiple locations. Note that recipients passed in this parameter will be visible as part of the message.

  • to_name (String|Array<String>) (defaults to: nil)

    Give a name to the recipient.

  • x_smtpapi (Hash) (defaults to: nil)

    see X-SMTP API for details.

  • subject (String) (defaults to: nil)

    The subject of your email.

  • text (String) (defaults to: nil)

    The actual content of your email message.

  • html (String) (defaults to: nil)

    The actual content of your email message.

  • from (String) (defaults to: nil)

    This is where the email will appear to originate from for your recipient.

  • bcc (String|Array<String>) (defaults to: nil)

    This can also be passed in as an array of email addresses for multiple recipients.

  • from_name (String) (defaults to: nil)

    This is name appended to the from email field.

  • reply_to (String) (defaults to: nil)

    Append a reply-to field to your email message.

  • date (DateTime) (defaults to: nil)

    Specify the date header of your email.

  • files (Array) (defaults to: nil)

    Files to be attached. The file contents must be part of the multipart HTTP POST.

  • content (Hash) (defaults to: nil)

    Content IDs of the files to be used as inline images. Content IDs should match the cid’s used in the HTML markup.

  • headers (Hash) (defaults to: nil)

    Each key represents a header name and the value the header value.

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sendgrid/web/mail.rb', line 35

def send(
  to: nil, to_name: nil, x_smtpapi: nil,
  subject: nil, text: nil, html: nil,
  from: nil, bcc: nil, from_name: nil,
  reply_to: nil, date: nil, files: nil,
  content: nil, headers: nil)
  options = {
    'to' => to, 'toname' => to_name, 'x-smtpapi' => x_smtpapi,
    'subject' => subject, 'text' => text, 'html' => html,
    'from' => from, 'bcc' => bcc, 'fromname' => from_name,
    'replyto' => reply_to, 'date' => date, 'files' => files,
    'content' => content, 'headers' => headers }
  %w{ subject to }.each do |required_option|
    if options[required_option].nil?
      raise ArgumentError.new(
        "Missing required `#{required_option}` option")
    end
  end
  if options['text'].nil? && options['html'].nil?
    raise ArgumentError.new(
      'Missing required `text` or `html` option')
  end
  res = connection.post(
    '/api/mail.send.json',
    default_params(options))
  craft_response(res)
end