Method: Net::SMTP#open_message_stream

Defined in:
lib/net/smtp.rb

#open_message_stream(from_addr, *to_addrs, &block) ⇒ Object Also known as: ready

Opens a message writer stream and gives it to the block. The stream is valid only in the block, and has these methods:

puts(str = ”)

outputs STR and CR LF.

print(str)

outputs STR.

printf(fmt, *args)

outputs sprintf(fmt,*args).

write(str)

outputs STR and returns the length of written bytes.

<<(str)

outputs STR and returns self.

If a single CR (“r”) or LF (“n”) is found in the message, it is converted to the CR LF pair. You cannot send a binary message with this method.

Parameters

from_addr is a String or Net::SMTP::Address representing the source mail address.

to_addr is a String or Net::SMTP::Address or Array of them, representing the destination mail address or addresses.

Example

Net::SMTP.start('smtp.example.com', 25) do |smtp|
  smtp.open_message_stream('[email protected]', ['[email protected]']) do |f|
    f.puts 'From: [email protected]'
    f.puts 'To: [email protected]'
    f.puts 'Subject: test message'
    f.puts
    f.puts 'This is a test message.'
  end
end

Errors

This method may raise:

  • Net::SMTPServerBusy

  • Net::SMTPSyntaxError

  • Net::SMTPFatalError

  • Net::SMTPUnknownError

  • Net::ReadTimeout

  • IOError

Raises:

  • (IOError)


849
850
851
852
853
854
855
# File 'lib/net/smtp.rb', line 849

def open_message_stream(from_addr, *to_addrs, &block)   # :yield: stream
  to_addrs.flatten!
  raise IOError, 'closed session' unless @socket
  from_addr = Address.new(from_addr, 'SMTPUTF8') if any_require_smtputf8(to_addrs) && capable?('SMTPUTF8')
  mailfrom from_addr
  rcptto_list(to_addrs) {data(&block)}
end