Module: Cybertrain::Mailer::Message

Defined in:
cybertrain/mailer/message.rb

Overview

Builds the multipart/alternative message #deliver sends when it is given html:, and reads one back (Message.parse, which Test::ReceivedMessage#message_parts uses).

Class Method Summary collapse

Class Method Details

.build(from, to, subject, text, html, boundary) ⇒ String

The message: headers (From, To, Subject, MIME-Version, Content-Type), a blank line, then two parts, text as text/plain and html as text/html, both charset=UTF-8 and Content-Transfer-Encoding: base64 (lines of 76 characters, CRLF), and the closing boundary. Every line ends CRLF; there is no Date header (the mailer adds it when it sends) and no dot-stuffing.

The boundary cannot occur inside a part: a boundary line starts with “–”, and a base64 body is made of A-Z, a-z, 0-9, “+”, “/” and “=” only, so none of its lines can start with “-“; the part headers are fixed text. That holds for any boundary, whatever the bodies contain (a lone “.” line, “–” lines, the boundary itself).

Every value is checked as Cybertrain::Mailer#deliver checks it: bare ASCII addresses, a subject without CR, LF or NUL (a non-ASCII subject is RFC 2047-encoded), bodies of valid UTF-8 without NUL (their line endings made CRLF before encoding, RFC 2045 6.8’s canonical form).

Examples:

Cybertrain::Mailer::Message.build("app@example.com", "bob@example.org", "Hi",
                                  "Hello\n", "<p>Hello</p>", "=_b1")

Parameters:

  • from (String) —

    the sender address

  • to (String) —

    one address, or several separated by commas

  • subject (String) —

    UTF-8

  • text (String) —

    the plain-text part, UTF-8

  • html (String) —

    the HTML part, UTF-8

  • boundary (String) —

    1 to 70 characters of RFC 2046’s bchars other than space (letters, digits, '()+_,-./:=?)

Returns:

  • (String)

Raises:

  • (ArgumentError) —

    for an address, subject, body or boundary refused



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'cybertrain/mailer/message.rb', line 47

def self.build(from, to, subject, text, html, boundary)
  sender = from.to_s
  Mailer.check_address(sender, "from")
  recipients = Mailer.split_recipients(to.to_s)
  title = subject.to_s
  Mailer.check_subject(title)
  plain = Mailer.normalize_body(text.to_s)
  markup = Mailer.normalize_body(html.to_s)
  mark = boundary.to_s
  Message.check_boundary(mark)
  to_header = Mailer.to_header(recipients)
  subject_header = Mailer.subject_header(title)
  out = String.new
  out << "From: " << sender << "\r\n"
  out << to_header
  out << subject_header
  out << "MIME-Version: 1.0\r\n"
  # Folded before the parameter: no header line past 78 characters
  # (RFC 5322 2.1.1) with the mailer's 45-character boundary.
  out << "Content-Type: multipart/alternative;\r\n boundary=\"" << mark << "\"\r\n"
  out << "\r\n"
  Message.append_part(out, mark, "text/plain", plain)
  Message.append_part(out, mark, "text/html", markup)
  out << "--" << mark << "--\r\n"
  out
end

.parse(data) ⇒ Hash{String => String}

A message read back, for tests: "subject" (RFC 2047 encoded words decoded), "text" and "html" (each part decoded from its transfer encoding, CRLF line endings kept). A message that is not multipart is all "text" and "html" is “”. data is the message as sent after DATA, dot-stuffing undone (Test::ReceivedMessage’s message_data); a Date header or none makes no difference.

Examples:

parts = Cybertrain::Mailer::Message.parse(mail.message_data)
parts["html"]   # => "<p>Hello</p>\r\n"

Parameters:

  • data (String)

Returns:

  • (Hash{String => String})


125
126
127
128
129
130
131
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
# File 'cybertrain/mailer/message.rb', line 125

def self.parse(data)
  s = data.to_s
  out = { "subject" => "", "text" => "", "html" => "" }
  stop = s.byteindex("\r\n\r\n")
  head = stop.nil? ? s : s.byteslice(0, stop)
  body = stop.nil? ? "" : s.byteslice(stop + 4, s.bytesize - stop - 4)
  out["subject"] = Message.decode_words(Message.header_in(head, "subject"))
  ctype = Message.header_in(head, "content-type")
  mark = Message.boundary_of(ctype)
  if mark.empty?
    out["text"] = Message.decode_body(body, Message.header_in(head, "content-transfer-encoding"))
    return out
  end
  delimiter = String.new
  delimiter << "--" << mark
  pieces = body.split(delimiter)
  i = 1
  while i < pieces.size
    piece = pieces[i]
    break if piece.start_with?("--")

    piece = piece.byteslice(2, piece.bytesize - 2) if piece.start_with?("\r\n")
    at = piece.byteindex("\r\n\r\n")
    unless at.nil?
      part_head = piece.byteslice(0, at)
      part_body = piece.byteslice(at + 4, piece.bytesize - at - 4)
      decoded = Message.decode_body(part_body, Message.header_in(part_head, "content-transfer-encoding"))
      kind = Message.header_in(part_head, "content-type").downcase
      if kind.start_with?("text/html")
        out["html"] = decoded
      elsif kind.start_with?("text/plain")
        out["text"] = decoded
      end
    end
    i += 1
  end
  out
end