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 =

The CR-LF String

"\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.


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

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


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

def body
  @body
end

#dateObject

Date of the email


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

def date
  @date
end

#fromObject

Sender of the email


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

def from
  @from
end

#headersObject (readonly)

Additional headers


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

def headers
  @headers
end

#message_idObject

Unique message-id string


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

def message_id
  @message_id
end

#subjectObject

Subject of the email


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

def subject
  @subject
end

#toObject

Recipient of the email


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

def to
  @to
end

Instance Method Details

#to_sString

Formats the email into a SMTP message.

Returns:

  • (String)

    Properly formatted SMTP message.

See Also:


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

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