Class: Ronin::Support::Network::SMTP::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/network/smtp/email.rb

Overview

Represents an Email to be sent over Ronin::Support::Network::SMTP.

Constant Summary collapse

CRLF =

The CR-LF String

"\n\r"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from: nil, to: nil, subject: nil, date: Time.now, message_id: nil, headers: nil, body: nil) {|email| ... } ⇒ Email

Creates a new Email object.

Parameters:

  • from (String) (defaults to: nil)

    The address the email is from.

  • to (Array<#to_s>, String) (defaults to: nil)

    The address that the email should be sent to.

  • subject (String) (defaults to: nil)

    The subject of the email.

  • message_id (String) (defaults to: nil)

    Message-ID of the email.

  • date (String, Time) (defaults to: Time.now)

    The date the email was sent on.

  • headers (Hash<String => String}) (defaults to: nil)

    Additional headers.

  • body (String, Array<String>) (defaults to: nil)

    The body of the email.

Yields:

  • (email)

    If a block is given, it will be passed the newly created email object.

Yield Parameters:

  • email (Email)

    The newly created email object.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ronin/support/network/smtp/email.rb', line 99

def initialize(from:       nil,
               to:         nil,
               subject:    nil,
               date:       Time.now,
               message_id: nil,
               headers:    nil,
               body:       nil)
  @from       = from
  @to         = to
  @subject    = subject
  @date       = date
  @message_id = message_id

  @headers = {}
  @headers.merge!(headers) if headers

  @body = []

  if body
    case body
    when Array
      @body += body
    else
      @body << body
    end
  end

  yield self if block_given?
end

Instance Attribute Details

#bodyString+

Body of the email

Returns:



64
65
66
# File 'lib/ronin/support/network/smtp/email.rb', line 64

def body
  @body
end

#dateString, Time

Date of the email

Returns:



49
50
51
# File 'lib/ronin/support/network/smtp/email.rb', line 49

def date
  @date
end

#fromString

Sender of the email

Returns:



34
35
36
# File 'lib/ronin/support/network/smtp/email.rb', line 34

def from
  @from
end

#headersHash{String => String} (readonly)

Additional headers

Returns:



59
60
61
# File 'lib/ronin/support/network/smtp/email.rb', line 59

def headers
  @headers
end

#message_idString

Unique message-id string

Returns:



54
55
56
# File 'lib/ronin/support/network/smtp/email.rb', line 54

def message_id
  @message_id
end

#subjectString

Subject of the email

Returns:



44
45
46
# File 'lib/ronin/support/network/smtp/email.rb', line 44

def subject
  @subject
end

#toArray<#to_s>, String

Recipient of the email

Returns:



39
40
41
# File 'lib/ronin/support/network/smtp/email.rb', line 39

def to
  @to
end

Instance Method Details

#to_sString

Formats the email into a SMTP message.

Returns:

  • (String)

    Properly formatted SMTP message.

See Also:



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
# File 'lib/ronin/support/network/smtp/email.rb', line 139

def to_s
  message = []

  if @from
    message << "From: #{@from}"
  end

  if @to
    message << case @to
               when Array
                 "To: #{@to.join(', ')}"
               else
                 "To: #{@to}"
               end
  end

  if @subject
    message << "Subject: #{@subject}"
  end

  if @date
    message << "Date: #{@date}"
  end

  if @message_id
    message << "Message-Id: <#{@message_id}>"
  end

  @headers.each do |name,value|
    message << "#{name}: #{value}"
  end

  message << ''
  message += @body

  return message.join(CRLF)
end