module Net
If smtp or pop is already loaded, remove all constants.
Constants
- APOPSession
This class is equivalent to
POP3
, except that it usesAPOP
authentication.- POP
Net::POP3
¶ ↑What is This Library?¶ ↑
This library provides functionality for retrieving email via
POP3
, the Post Office Protocol version 3. For details ofPOP3
, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt).Examples¶ ↑
Retrieving Messages ¶ ↑
This example retrieves messages from the server and deletes them on the server.
Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your
POP3
server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.require 'net/pop' pop = Net::POP3.new('pop.example.com') pop.start('YourAccount', 'YourPassword') # (1) if pop.mails.empty? puts 'No mail.' else i = 0 pop.each_mail do |m| # or "pop.mails.each ..." # (2) File.open("inbox/#{i}", 'w') do |f| f.write m.pop end m.delete i += 1 end puts "#{pop.mails.size} mails popped." end pop.finish # (3)
-
Call
Net::POP3#start
and startPOP
session. -
Access messages by using
POP3#each_mail
and/orPOP3#mails
. -
Close
POP
session by callingPOP3#finish
or use the block form of start.
Shortened Code¶ ↑
The example above is very verbose. You can shorten the code by using some utility methods. First, the block form of
Net::POP3.start
can be used instead ofPOP3.new
,POP3#start
andPOP3#finish
.require 'net/pop' Net::POP3.start('pop.example.com', 110, 'YourAccount', 'YourPassword') do |pop| if pop.mails.empty? puts 'No mail.' else i = 0 pop.each_mail do |m| # or "pop.mails.each ..." File.open("inbox/#{i}", 'w') do |f| f.write m.pop end m.delete i += 1 end puts "#{pop.mails.size} mails popped." end end
POP3#delete_all
is an alternative for each_mail and delete.require 'net/pop' Net::POP3.start('pop.example.com', 110, 'YourAccount', 'YourPassword') do |pop| if pop.mails.empty? puts 'No mail.' else i = 1 pop.delete_all do |m| File.open("inbox/#{i}", 'w') do |f| f.write m.pop end i += 1 end end end
And here is an even shorter example.
require 'net/pop' i = 0 Net::POP3.delete_all('pop.example.com', 110, 'YourAccount', 'YourPassword') do |m| File.open("inbox/#{i}", 'w') do |f| f.write m.pop end i += 1 end
Memory Space Issues¶ ↑
All the examples above get each message as one big string. This example avoids this.
require 'net/pop' i = 1 Net::POP3.delete_all('pop.example.com', 110, 'YourAccount', 'YourPassword') do |m| File.open("inbox/#{i}", 'w') do |f| m.pop do |chunk| # get a message little by little. f.write chunk end i += 1 end end
Using
APOP
¶ ↑The net/pop library supports
APOP
authentication. To useAPOP
, use theNet::APOP
class instead of theNet::POP3
class. You can use the utility method,Net::POP3
.APOP(). For example:require 'net/pop' # Use APOP authentication if $isapop == true pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110) pop.start(YourAccount', 'YourPassword') do |pop| # Rest of the code is the same. end
Fetch Only Selected Mail Using 'UIDL'
POP
Command¶ ↑If your
POP
server provides UIDL functionality, you can grab only selected mails from thePOP
server. e.g.def need_pop?( id ) # determine if we need pop this mail... end Net::POP3.start('pop.example.com', 110, 'Your account', 'Your password') do |pop| pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m| do_something(m.pop) end end
The
POPMail#unique_id()
method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message.-
- POP3Session
Net::POP3
¶ ↑What is This Library?¶ ↑
This library provides functionality for retrieving email via
POP3
, the Post Office Protocol version 3. For details ofPOP3
, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt).Examples¶ ↑
Retrieving Messages ¶ ↑
This example retrieves messages from the server and deletes them on the server.
Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your
POP3
server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.require 'net/pop' pop = Net::POP3.new('pop.example.com') pop.start('YourAccount', 'YourPassword') # (1) if pop.mails.empty? puts 'No mail.' else i = 0 pop.each_mail do |m| # or "pop.mails.each ..." # (2) File.open("inbox/#{i}", 'w') do |f| f.write m.pop end m.delete i += 1 end puts "#{pop.mails.size} mails popped." end pop.finish # (3)
-
Call
Net::POP3#start
and startPOP
session. -
Access messages by using
POP3#each_mail
and/orPOP3#mails
. -
Close
POP
session by callingPOP3#finish
or use the block form of start.
Shortened Code¶ ↑
The example above is very verbose. You can shorten the code by using some utility methods. First, the block form of
Net::POP3.start
can be used instead ofPOP3.new
,POP3#start
andPOP3#finish
.require 'net/pop' Net::POP3.start('pop.example.com', 110, 'YourAccount', 'YourPassword') do |pop| if pop.mails.empty? puts 'No mail.' else i = 0 pop.each_mail do |m| # or "pop.mails.each ..." File.open("inbox/#{i}", 'w') do |f| f.write m.pop end m.delete i += 1 end puts "#{pop.mails.size} mails popped." end end
POP3#delete_all
is an alternative for each_mail and delete.require 'net/pop' Net::POP3.start('pop.example.com', 110, 'YourAccount', 'YourPassword') do |pop| if pop.mails.empty? puts 'No mail.' else i = 1 pop.delete_all do |m| File.open("inbox/#{i}", 'w') do |f| f.write m.pop end i += 1 end end end
And here is an even shorter example.
require 'net/pop' i = 0 Net::POP3.delete_all('pop.example.com', 110, 'YourAccount', 'YourPassword') do |m| File.open("inbox/#{i}", 'w') do |f| f.write m.pop end i += 1 end
Memory Space Issues¶ ↑
All the examples above get each message as one big string. This example avoids this.
require 'net/pop' i = 1 Net::POP3.delete_all('pop.example.com', 110, 'YourAccount', 'YourPassword') do |m| File.open("inbox/#{i}", 'w') do |f| m.pop do |chunk| # get a message little by little. f.write chunk end i += 1 end end
Using
APOP
¶ ↑The net/pop library supports
APOP
authentication. To useAPOP
, use theNet::APOP
class instead of theNet::POP3
class. You can use the utility method,Net::POP3
.APOP(). For example:require 'net/pop' # Use APOP authentication if $isapop == true pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110) pop.start(YourAccount', 'YourPassword') do |pop| # Rest of the code is the same. end
Fetch Only Selected Mail Using 'UIDL'
POP
Command¶ ↑If your
POP
server provides UIDL functionality, you can grab only selected mails from thePOP
server. e.g.def need_pop?( id ) # determine if we need pop this mail... end Net::POP3.start('pop.example.com', 110, 'Your account', 'Your password') do |pop| pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m| do_something(m.pop) end end
The
POPMail#unique_id()
method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message.-
- POPSession
Net::POP3
¶ ↑What is This Library?¶ ↑
This library provides functionality for retrieving email via
POP3
, the Post Office Protocol version 3. For details ofPOP3
, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt).Examples¶ ↑
Retrieving Messages ¶ ↑
This example retrieves messages from the server and deletes them on the server.
Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your
POP3
server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.require 'net/pop' pop = Net::POP3.new('pop.example.com') pop.start('YourAccount', 'YourPassword') # (1) if pop.mails.empty? puts 'No mail.' else i = 0 pop.each_mail do |m| # or "pop.mails.each ..." # (2) File.open("inbox/#{i}", 'w') do |f| f.write m.pop end m.delete i += 1 end puts "#{pop.mails.size} mails popped." end pop.finish # (3)
-
Call
Net::POP3#start
and startPOP
session. -
Access messages by using
POP3#each_mail
and/orPOP3#mails
. -
Close
POP
session by callingPOP3#finish
or use the block form of start.
Shortened Code¶ ↑
The example above is very verbose. You can shorten the code by using some utility methods. First, the block form of
Net::POP3.start
can be used instead ofPOP3.new
,POP3#start
andPOP3#finish
.require 'net/pop' Net::POP3.start('pop.example.com', 110, 'YourAccount', 'YourPassword') do |pop| if pop.mails.empty? puts 'No mail.' else i = 0 pop.each_mail do |m| # or "pop.mails.each ..." File.open("inbox/#{i}", 'w') do |f| f.write m.pop end m.delete i += 1 end puts "#{pop.mails.size} mails popped." end end
POP3#delete_all
is an alternative for each_mail and delete.require 'net/pop' Net::POP3.start('pop.example.com', 110, 'YourAccount', 'YourPassword') do |pop| if pop.mails.empty? puts 'No mail.' else i = 1 pop.delete_all do |m| File.open("inbox/#{i}", 'w') do |f| f.write m.pop end i += 1 end end end
And here is an even shorter example.
require 'net/pop' i = 0 Net::POP3.delete_all('pop.example.com', 110, 'YourAccount', 'YourPassword') do |m| File.open("inbox/#{i}", 'w') do |f| f.write m.pop end i += 1 end
Memory Space Issues¶ ↑
All the examples above get each message as one big string. This example avoids this.
require 'net/pop' i = 1 Net::POP3.delete_all('pop.example.com', 110, 'YourAccount', 'YourPassword') do |m| File.open("inbox/#{i}", 'w') do |f| m.pop do |chunk| # get a message little by little. f.write chunk end i += 1 end end
Using
APOP
¶ ↑The net/pop library supports
APOP
authentication. To useAPOP
, use theNet::APOP
class instead of theNet::POP3
class. You can use the utility method,Net::POP3
.APOP(). For example:require 'net/pop' # Use APOP authentication if $isapop == true pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110) pop.start(YourAccount', 'YourPassword') do |pop| # Rest of the code is the same. end
Fetch Only Selected Mail Using 'UIDL'
POP
Command¶ ↑If your
POP
server provides UIDL functionality, you can grab only selected mails from thePOP
server. e.g.def need_pop?( id ) # determine if we need pop this mail... end Net::POP3.start('pop.example.com', 110, 'Your account', 'Your password') do |pop| pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m| do_something(m.pop) end end
The
POPMail#unique_id()
method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message.-
- SMTPSession
Net::SMTP
¶ ↑What is This Library?¶ ↑
This library provides functionality to send internet mail via
SMTP
, the Simple Mail Transfer Protocol. For details ofSMTP
itself, see [RFC2821] (www.ietf.org/rfc/rfc2821.txt).What is This Library NOT?¶ ↑
This library does NOT provide functions to compose internet mails. You must create them by yourself. If you want better mail support, try RubyMail or TMail. You can get both libraries from RAA. (www.ruby-lang.org/en/raa.html)
FYI: the official documentation on internet mail is: [RFC2822] (www.ietf.org/rfc/rfc2822.txt).
Examples¶ ↑
Sending Messages¶ ↑
You must open a connection to an
SMTP
server before sending messages. The first argument is the address of yourSMTP
server, and the second argument is the port number. UsingSMTP.start
with a block is the simplest way to do this. This way, theSMTP
connection is closed automatically after the block is executed.require 'net/smtp' Net::SMTP.start('your.smtp.server', 25) do |smtp| # Use the SMTP object smtp only in this block. end
Replace 'your.smtp.server' with your
SMTP
server. Normally your system manager or internet provider supplies a server for you.Then you can send messages.
msgstr = <<END_OF_MESSAGE From: Your Name <your@mail.address> To: Destination Address <someone@example.com> Subject: test message Date: Sat, 23 Jun 2001 16:26:43 +0900 Message-Id: <unique.message.id.string@example.com> This is a test message. END_OF_MESSAGE require 'net/smtp' Net::SMTP.start('your.smtp.server', 25) do |smtp| smtp.send_message msgstr, 'your@mail.address', 'his_addess@example.com' end
Closing the Session¶ ↑
You MUST close the
SMTP
session after sending messages, by calling the finish method:# using SMTP#finish smtp = Net::SMTP.start('your.smtp.server', 25) smtp.send_message msgstr, 'from@address', 'to@address' smtp.finish
You can also use the block form of
SMTP.start
/SMTP#start. This closes theSMTP
session automatically:# using block form of SMTP.start Net::SMTP.start('your.smtp.server', 25) do |smtp| smtp.send_message msgstr, 'from@address', 'to@address' end
I strongly recommend this scheme. This form is simpler and more robust.
HELO domain¶ ↑
In almost all situations, you must provide a third argument to
SMTP.start
/SMTP#start. This is the domain name which you are on (the host to send mail from). It is called the “HELO domain”. TheSMTP
server will judge whether it should send or reject theSMTP
session by inspecting the HELO domain.Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain') { |smtp| ... }
SMTP
Authentication¶ ↑The
Net::SMTP
class supports three authentication schemes; PLAIN, LOGIN and CRAM MD5. (SMTP
Authentication: [RFC2554]) To useSMTP
authentication, pass extra arguments toSMTP.start
/SMTP#start.# PLAIN Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain', 'Your Account', 'Your Password', :plain) # LOGIN Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain', 'Your Account', 'Your Password', :login) # CRAM MD5 Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain', 'Your Account', 'Your Password', :cram_md5)