Module: Ronin::Network::SMTP

Included in:
Net, ESMTP, Mixins::SMTP, Support
Defined in:
lib/ronin/network/smtp/smtp.rb,
lib/ronin/network/smtp/email.rb

Overview

Provides helper methods for communicating with SMTP services.

Defined Under Namespace

Classes: Email

Constant Summary

DEFAULT_PORT =

Default SMTP port

25

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Integer) default_port

Returns The default Ronin SMTP port.

Returns:

  • (Integer)

    The default Ronin SMTP port.



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

def self.default_port
  @default_port ||= DEFAULT_PORT
end

+ (Object) default_port=(port)

Sets the default Ronin SMTP port.

Parameters:

  • port (Integer)

    The new default Ronin SMTP port.



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

def self.default_port=(port)
  @default_port = port
end

+ (String) message(options = {}) {|email| ... }

Creates a properly formatted email.

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:



72
73
74
# File 'lib/ronin/network/smtp/smtp.rb', line 72

def self.message(options={},&block)
  Email.new(options,&block).to_s
end

Instance Method Details

- (Net::SMTP) smtp_connect(host, options = {}) {|session| ... }

Creates a connection to the SMTP server.

Examples:

smtp_connect('www.example.com', :user => 'joe')

Parameters:

  • host (String)

    The host to connect to.

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

    Additional options.

Options Hash (options):

  • :port (Integer) — default: SMTP.default_port

    The port to connect to.

  • :helo (String)

    The HELO domain.

  • :auth (Symbol)

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

  • :user (String)

    The user-name to authenticate with.

  • :password (String)

    The password to authenticate with.

Yields:

  • (session)

    If a block is given, it will be passed an SMTP session object.

Yield Parameters:

  • session (Net::SMTP)

    The SMTP session.

Returns:

  • (Net::SMTP)

    The SMTP session.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ronin/network/smtp/smtp.rb', line 135

def smtp_connect(host,options={})
  host = host.to_s
  port = (options[:port] || SMTP.default_port)

  helo     = options[:helo]
  auth     = options[:auth]
  user     = options[:user]
  password = options[:password]

  session = Net::SMTP.start(host,port,helo,user,password,auth)

  yield session if block_given?
  return session
end

- (Object) smtp_message(options = {}) {|email| ... }

Creates a new email message.

Parameters:

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

    Additional options for the email.

Yields:

  • (email)

    The given block will be passed the new email.

Yield Parameters:

  • email (Email)

    The new email.

See Also:

  • Email.new


92
93
94
# File 'lib/ronin/network/smtp/smtp.rb', line 92

def smtp_message(options={},&block)
  Email.new(options,&block)
end

- (Object) smtp_send_message(host, options = {}) {|email| ... }

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.

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

    Additional SMTP and Email options.

Yields:

  • (email)

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

Yield Parameters:

See Also:

Since:

  • 0.2.0

  • 0.2.0



221
222
223
224
225
226
227
# File 'lib/ronin/network/smtp/smtp.rb', line 221

def smtp_send_message(host,options={},&block)
  email = smtp_message(options,&block)

  smtp_session(host,options) do |smtp|
    smtp.send_message(email.to_s, email.from, email.to)
  end
end

- (Object) smtp_session(host, options = {}) {|session| ... }

Starts a session with the SMTP server.

Examples:

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

Parameters:

  • host (String)

    The host to connect to.

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

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed an SMTP session object. After the block has returned, the session will be closed.

Yield Parameters:

  • session (Net::SMTP)

    The SMTP session.

See Also:



175
176
177
178
179
180
181
182
# File 'lib/ronin/network/smtp/smtp.rb', line 175

def smtp_session(host,options={})
  session = smtp_connect(host,options)

  yield session if block_given?

  session.finish
  return nil
end