Module: Ronin::Support::Network::SMTP::Mixin

Includes:
Ronin::Support::Network::SSL::Mixin
Included in:
ESMTP::Mixin, Mixin
Defined in:
lib/ronin/support/network/smtp/mixin.rb

Overview

Provides helper methods for communicating with SMTP services.

Constant Summary collapse

DEFAULT_PORT =

Default SMTP port

25

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ronin::Support::Network::SSL::Mixin

#ssl_accept, #ssl_banner, #ssl_cert, #ssl_connect, #ssl_connect_and_send, #ssl_context, #ssl_open?, #ssl_send, #ssl_server_loop, #ssl_server_socket, #ssl_socket

Methods included from TCP::Mixin

#tcp_accept, #tcp_banner, #tcp_connect, #tcp_connect_and_send, #tcp_open?, #tcp_send, #tcp_server, #tcp_server_loop, #tcp_server_session

Class Method Details

.message(**kwargs) {|email| ... } ⇒ String

Creates a properly formatted email.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Email#initialize.

Yields:

  • (email)

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

Yield Parameters:

  • email (Email)

    The new Email object.

Returns:

  • (String)

    Formatted SMTP email.

See Also:



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

def self.message(**kwargs,&block)
  Email.new(**kwargs,&block).to_s
end

Instance Method Details

#smtp_connect(host, user:, password:, port: DEFAULT_PORT, ssl: nil, helo: 'localhost', auth: :login) {|smtp| ... } ⇒ Net::SMTP?

Creates a connection to the SMTP server.

Examples:

smtp_connect('www.example.com', user: 'joe')
smtp_connect('www.example.com', user: 'joe') do |smtp|
  # ...
end

Parameters:

  • host (String)

    The host to connect to.

  • user (String)

    The user-name to authenticate with.

  • password (String)

    The password to authenticate with.

  • port (Integer) (defaults to: DEFAULT_PORT)

    The port to connect to.

  • ssl (Boolean, Hash) (defaults to: nil)

    Additional SSL options.

  • helo (String) (defaults to: 'localhost')

    The HELO domain.

  • auth (:login, :plain, :cram_md5) (defaults to: :login)

    The type of authentication to use. Can be either :login, :plain, or :cram_md5.

Options Hash (ssl:):

  • :verify (Boolean)

    Specifies that the SSL certificate should be verified.

  • :certs (String)

    The path to the file containing CA certs of the server.

Yields:

  • (smtp)

    If a block is given, it will be passed an SMTP session object. Once the block returns the SMTP session will be closed.

Yield Parameters:

  • smtp (Net::SMTP)

    The SMTP session.

Returns:

  • (Net::SMTP, nil)

    The SMTP session. If a block is given, then nil will be returned.



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/mixin.rb', line 144

def smtp_connect(host, user: ,
                       password: , port: DEFAULT_PORT,
                       ssl:  nil,
                       helo: 'localhost',
                       auth: :login)
  host     = DNS::IDN.to_ascii(host)
  user     = user.to_s
  password = password.to_s

  case ssl
  when Hash
    ssl     = true
    context = ssl_context(ssl)
  when TrueClass
    ssl     = true
    context = ssl_context
  end

  smtp = Net::SMTP.new(host,port)
  smtp.enable_starttls(context) if ssl
  smtp.start(helo,user,password,auth)

  if block_given?
    begin
      yield smtp
    ensure
      smtp.finish
    end
  else
    return smtp
  end
end

#smtp_message(**kwargs) {|email| ... } ⇒ Email

Creates a new email message.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Email#initialize.

Yields:

  • (email)

    The given block will be passed the new email.

Yield Parameters:

  • email (Email)

    The new email.

Returns:

  • (Email)

    the new email message object.

See Also:



88
89
90
# File 'lib/ronin/support/network/smtp/mixin.rb', line 88

def smtp_message(**kwargs,&block)
  Email.new(**kwargs,&block)
end

#smtp_send_message(host, user:, password:, port: DEFAULT_PORT, ssl: nil, helo: 'localhost', auth: :login, from: nil, to: nil, subject: nil, date: Time.now, message_id: nil, headers: nil, body: nil) {|email| ... } ⇒ Object

Connects to an SMTP server and sends a message.

Examples:

smtp_send_message 'www.example.com',
                  to:         'joe@example.com',
                  from:       'eve@example.com',
                  subject:    'Hello',
                  message_id: 'XXXX',
                  body:       'Hello'

Using the block:

smtp_send_message('www.example.com') do |email|
  email.to = 'joe@example.com'
  email.from 'eve@example.com'
  email.subject = 'Hello'
  email.message_id = 'XXXXXXXXXX'
  email.body << 'Hello!'
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer) (defaults to: DEFAULT_PORT)

    The port to connect to.

  • user (String)

    The user to authenticate as.

  • password (String)

    The password to authenticate with.

  • ssl (Boolean, Hash) (defaults to: nil)

    Additional SSL options.

  • helo (String) (defaults to: 'localhost')

    The HELO domain.

  • auth (:login, :plain, :cram_md5) (defaults to: :login)

    The type of authentication to use. Can be either :login, :plain, or :cram_md5.

  • from (String) (defaults to: nil)

    The address the email is from.

  • to (Array, 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.

Options Hash (ssl:):

  • :verify (Boolean)

    Specifies that the SSL certificate should be verified.

  • :certs (String)

    The path to the file containing CA certs of the server.

Yields:

  • (email)

    The given block will be passed the new email to be sent.

Yield Parameters:

See Also:

Since:

  • 0.2.0



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ronin/support/network/smtp/mixin.rb', line 258

def smtp_send_message(host, user: ,
                            password: ,
                            # smtp options
                            port: DEFAULT_PORT,
                            ssl:  nil,
                            helo: 'localhost',
                            auth: :login,
                            # email options
                            from:       nil,
                            to:         nil,
                            subject:    nil,
                            date:       Time.now,
                            message_id: nil,
                            headers:    nil,
                            body:       nil,
                            &block)
  email = smtp_message(
    to:         to,
    subject:    subject,
    date:       date,
    message_id: message_id,
    headers:    headers,
    body:       body,
    &block
  )

  smtp_connect(host,user,password,
               port: port, ssl: ssl, helo: helo, auth: auth) do |smtp|
    smtp.send_message(email.to_s, email.from, email.to)
  end
end