Class: Cybertrain::MailMessage

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/mailer/later.rb,
cybertrain/mailer/mail_message.rb

Overview

One mail, built now and sent later or at once: what a generated mailer method returns, so application code reads as in Rails:

PasswordsMailer.reset(user, url).deliver_later

#deliver_later is Cybertrain::Mailer.deliver_later (the built-in “cybertrain mail” job, so it needs the database); #deliver_now is Cybertrain::Mailer.deliver with the settings in Config. In the test environment each delivery is stored in Cybertrain::Mailer.deliveries as one of these.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to:, subject:, body:, html: "", from: "") ⇒ MailMessage

Keeps the fields; nothing is checked or sent until a deliver call.

Examples:

Cybertrain::MailMessage.new(to: user.email, subject: "Welcome", body: text, html: html)

Parameters:

  • to (String) —

    one address, or several separated by commas

  • subject (String) —

    UTF-8

  • body (String) —

    UTF-8 plain text

  • html (String) (defaults to: "") —

    UTF-8 HTML, “” for a plain-text message

  • from (String) (defaults to: "") —

    the sender; “” for Config#smtp_from



51
52
53
54
55
56
57
# File 'cybertrain/mailer/mail_message.rb', line 51

def initialize(to:, subject:, body:, html: "", from: "")
  @mail_to = to.to_s
  @mail_subject = subject.to_s
  @mail_body = body.to_s
  @mail_html = html.to_s
  @mail_from = from.to_s
end

Instance Attribute Details

#mail_body ⇒ String (readonly)

Returns UTF-8 plain text.

Returns:

  • (String) —

    UTF-8 plain text



33
34
35
# File 'cybertrain/mailer/mail_message.rb', line 33

def mail_body
  @mail_body
end

#mail_from ⇒ String (readonly)

Returns the sender, “” for Config#smtp_from (a message in Cybertrain::Mailer.deliveries has the sender it was delivered with).

Returns:



40
41
42
# File 'cybertrain/mailer/mail_message.rb', line 40

def mail_from
  @mail_from
end

#mail_html ⇒ String (readonly)

Returns UTF-8 HTML, “” for a plain-text message.

Returns:

  • (String) —

    UTF-8 HTML, “” for a plain-text message



36
37
38
# File 'cybertrain/mailer/mail_message.rb', line 36

def mail_html
  @mail_html
end

#mail_subject ⇒ String (readonly)

Returns UTF-8.

Returns:

  • (String) —

    UTF-8



30
31
32
# File 'cybertrain/mailer/mail_message.rb', line 30

def mail_subject
  @mail_subject
end

#mail_to ⇒ String (readonly)

Returns one address, or several separated by commas.

Returns:

  • (String) —

    one address, or several separated by commas



27
28
29
# File 'cybertrain/mailer/mail_message.rb', line 27

def mail_to
  @mail_to
end

Instance Method Details

#deliver_later ⇒ Integer

Enqueues the message for a job thread to send (Cybertrain::Mailer.deliver_later: the fields are checked now, so a refused one raises and nothing is queued).

Examples:

PasswordsMailer.reset(user, url).deliver_later

Returns:

Raises:

  • (ArgumentError) —

    for an address, subject or body refused

  • (Cybertrain::DB::Error) —

    when the database is not connected or the insert fails



144
145
146
# File 'cybertrain/mailer/later.rb', line 144

def deliver_later
  Mailer.deliver_later(to: @mail_to, subject: @mail_subject, body: @mail_body, html: @mail_html, from: @mail_from)
end

#deliver_now ⇒ nil

Sends the message now, with the settings in Config (Cybertrain::Mailer.deliver).

Examples:

WelcomeMailer.welcome(user).deliver_now

Returns:

  • (nil)

Raises:

  • (Mailer::Error) —

    a subclass, when the server cannot be reached or refuses the message

  • (ArgumentError) —

    for an address, subject or body refused



67
68
69
70
# File 'cybertrain/mailer/mail_message.rb', line 67

def deliver_now
  Mailer.deliver(to: @mail_to, subject: @mail_subject, body: @mail_body, html: @mail_html, from: @mail_from)
  nil
end