Class: MmMail::Transport
- Inherits:
-
Object
- Object
- MmMail::Transport
- 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.
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
-
#config ⇒ Object
Sets a Config object to use when sending mail.
Class Method Summary collapse
-
.mail(message, config = nil) ⇒ Object
Creates a new Transport object and sends an email.
Instance Method Summary collapse
-
#initialize(config = nil) ⇒ Transport
constructor
Creates a new Transport object to send emails with.
-
#mail(message) ⇒ Object
Sends a Message object out as an email using the configuration set during initialization.
-
#mail_sendmail(message) ⇒ Object
Sends a mail through sendmail using the Config#sendmail_binary as the location of the file.
-
#mail_smtp(message) ⇒ Object
Sends a mail through Net::SMTP using the #config if any SMTP or hostname information is set.
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.
79 80 81 82 83 84 85 |
# File 'lib/mmmail.rb', line 79 def initialize(config = nil) if config && !config.is_a?(Config) raise ArgumentError, "expected #{self.class}::Config" end @config = config || DefaultConfig end |
Instance Attribute Details
#config ⇒ Object
Sets a Config object to use when sending mail
71 72 73 |
# File 'lib/mmmail.rb', line 71 def config @config end |
Class Method Details
.mail(message, config = nil) ⇒ Object
Creates a new MmMail::Transport object and sends an email.
66 67 68 |
# File 'lib/mmmail.rb', line 66 def self.mail(, config = nil) new(config).mail() end |
Instance Method Details
#mail(message) ⇒ Object
Sends a Message object out as an email using the configuration set during initialization.
93 94 95 96 97 98 99 100 101 |
# File 'lib/mmmail.rb', line 93 def mail() unless Message === raise ArgumentError, "expected MmMail::Message, got #{.class}" end raise TransportError, "invalid message" unless .valid? send("mail_#{config.method}", ) end |
#mail_sendmail(message) ⇒ Object
Sends a mail through sendmail using the MmMail::Transport::Config#sendmail_binary as the location of the file.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mmmail.rb', line 119 def mail_sendmail() bin, err = config.sendmail_binary, '' result = IO.popen("#{bin} -t 2>&1", "w+") do |io| io.write(.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.
107 108 109 110 111 112 |
# File 'lib/mmmail.rb', line 107 def mail_smtp() Net::SMTP.start(config.host, config.port, 'localhost.localdomain', config.auth_user, config.auth_pass, config.auth_type) do |smtp| smtp.(.to_s, .from, .recipients_list) end end |