Class: MmMail::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/mmmail.rb

Overview

Handles the transportation of a Message to its destination. Basic support for SMTP (through Net::SMTP) or sendmail.

You can either pass a new Config object during transport or use the system wide DefaultConfig object.

Examples:

To set transport to use sendmail

MmMail::Transport::DefaultConfig.method = :sendmail
# Note you might need to point to sendmail if it's not in your PATH:
MmMail::Transport::DefaultConfig.sendmail_binary = '/path/to/sendmail'

To connect to your ISP SMTP server on 587

MmMail::Transport::DefaultConfig.host = 'smtp.myisp.com'
MmMail::Transport::DefaultConfig.port = 587

See Also:

Defined Under Namespace

Classes: Config

Constant Summary collapse

DefaultConfig =

The default system wide configuration used when no custom config object is provided to a Transport object. If you want to make global configuration changes, change the settings here.

Config.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Transport

Creates a new Transport object to send emails with. To change settings to sendmail or use SMTP auth, set these in the Config object.

Parameters:

  • a (Config)

    configuration to use

Raises:

  • (ArgumentError)

    if config is not a Config object.


76
77
78
79
80
81
82
# File 'lib/mmmail.rb', line 76

def initialize(config = nil)
  if config && !config.is_a?(Config)
    raise ArgumentError, "expected #{self.class}::Config"
  end
  
  @config = config || DefaultConfig
end

Instance Attribute Details

#configObject

Sets a Config object to use when sending mail


68
69
70
# File 'lib/mmmail.rb', line 68

def config
  @config
end

Class Method Details

.mail(message, config = nil) ⇒ Object

Creates a new MmMail::Transport object and sends an email.

See Also:


63
64
65
# File 'lib/mmmail.rb', line 63

def self.mail(message, config = nil)
  new(config).mail(message)
end

Instance Method Details

#mail(message) ⇒ Object

Sends a Message object out as an email using the configuration set during initialization.

Parameters:

  • message (Message)

    an email to send

Raises:


90
91
92
93
94
95
96
97
98
# File 'lib/mmmail.rb', line 90

def mail(message)
  unless Message === message
    raise ArgumentError, "expected MmMail::Message, got #{message.class}"
  end
  
  raise TransportError, "invalid message" unless message.valid?
  
  send("mail_#{config.method}", message)
end

#mail_sendmail(message) ⇒ Object

Sends a mail through sendmail using the MmMail::Transport::Config#sendmail_binary as the location of the file.

Parameters:

  • message (#to_s)

    the message to send

Raises:


116
117
118
119
120
121
122
123
124
125
# File 'lib/mmmail.rb', line 116

def mail_sendmail(message)
  bin, err = config.sendmail_binary, ''
  result = IO.popen("#{bin} -t 2>&1", "w+") do |io|
    io.write(message.to_s)
    io.close_write
    err = io.read.chomp
  end
  
  raise TransportError, err if $? != 0
end

#mail_smtp(message) ⇒ Object

Sends a mail through Net::SMTP using the #config if any SMTP or hostname information is set.

Parameters:

  • message (#to_s)

    the message to send


104
105
106
107
108
109
# File 'lib/mmmail.rb', line 104

def mail_smtp(message)
  Net::SMTP.start(config.host, config.port, 'localhost.localdomain', 
      config.auth_user, config.auth_pass, config.auth_type) do |smtp|
    smtp.send_message(message.to_s, message.from, message.recipients_list)
  end
end