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.



83
84
85
86
87
88
89
# File 'lib/mmmail.rb', line 83

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



75
76
77
# File 'lib/mmmail.rb', line 75

def config
  @config
end

Class Method Details

.mail(message, config = nil) ⇒ Object

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

See Also:



70
71
72
# File 'lib/mmmail.rb', line 70

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:



97
98
99
100
101
102
103
104
105
# File 'lib/mmmail.rb', line 97

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:



124
125
126
127
128
129
130
131
132
133
# File 'lib/mmmail.rb', line 124

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



111
112
113
114
115
116
117
# File 'lib/mmmail.rb', line 111

def mail_smtp(message)
  Net::SMTP.enable_tls if config.enable_tls
  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