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.
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
#config ⇒ Object
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.
70 71 72 |
# File 'lib/mmmail.rb', line 70 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.
97 98 99 100 101 102 103 104 105 |
# File 'lib/mmmail.rb', line 97 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.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mmmail.rb', line 124 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.
111 112 113 114 115 116 117 |
# File 'lib/mmmail.rb', line 111 def mail_smtp() 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.(.to_s, .from, .recipients_list) end end |