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

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

Overview

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

Constant Summary collapse

CRLF =
"\n\r"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|email| ... } ⇒ Email

Creates a new Email object.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :from (String)

    The address the email is from.

  • :to (Array, String)

    The address that the email should be sent to.

  • :subject (String)

    The subject of the email.

  • :message_id (String)

    Message-ID of the email.

  • :date (String, Time) — default: Time.now

    The date the email was sent on.

  • :headers (Hash<String => String})

    Additional headers.

  • :body (String, Array<String>)

    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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ronin/network/smtp/email.rb', line 87

def initialize(options={})
  @from = options[:from]
  @to = options[:to]
  @subject = options[:subject]
  @date = options.fetch(:date,Time.now)
  @message_id = options[:message_id]
  @headers = {}

  if options[:headers]
    @headers.merge!(options[:headers])
  end

  @body = []

  if options[:body]
    case options[:body]
    when Array
      @body += options[:body]
    else
      @body << options[:body]
    end
  end

  yield self if block_given?
end

Instance Attribute Details

#bodyObject

Body of the email



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

def body
  @body
end

#dateObject

Date of the email



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

def date
  @date
end

#fromObject

Sender of the email



31
32
33
# File 'lib/ronin/network/smtp/email.rb', line 31

def from
  @from
end

#headersObject (readonly)

Additional headers



46
47
48
# File 'lib/ronin/network/smtp/email.rb', line 46

def headers
  @headers
end

#message_idObject

Unique message-id string



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

def message_id
  @message_id
end

#subjectObject

Subject of the email



37
38
39
# File 'lib/ronin/network/smtp/email.rb', line 37

def subject
  @subject
end

#toObject

Recipient of the email



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

def to
  @to
end

Instance Method Details

#to_sString

Formats the email into a SMTP message.

Returns:

  • (String)

    Properly formatted SMTP message.

See Also:



123
124
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
163
164
165
166
167
168
# File 'lib/ronin/network/smtp/email.rb', line 123

def to_s
  address = lambda { |info|
    case info
    when Array
      "#{info[0]} <#{info[1]}>"
    else
      info
    end
  }

  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