undefined method `mails' for #<Net::POPMail 1> (NoMethodErro

F

Frioffol Friofool

Hello,
I'm trying to do a method to get mail via pop3 server.
I created the following script but i receive this message during
executio :

testmail.rb:12: undefined method `mails' for #<Net::pOPMail 1>
(NoMethodError)


#!/usr/bin/env ruby
require 'net/pop' # I renamed the file from pop.rb to pop_ssl.rb to
ensure I was requiring the correct version

username = '(e-mail address removed)'
password = 'pass'

#Net::pOP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
conn = Net::pOP3.new('pop.host.com',110)
conn.start(username,password)

conn.mails.each do |pop|
msg = pop.mails[0]

# Print the 'From:' header line
puts msg.header.split("\r\n").grep(/^From: /)

# Put message to $stdout (by calling <<)
puts "\nFull message:\n"
msg.all($stdout)
end

maybe a lib problem ?

thx for your answer...
 
S

Stefano Crocco

There's a mistake in your code. conn.mails return an array of the mails
on the server, i.e an array of Net::pOPMail objects. The mails method,
instead, is a method of class Net::pOP3. To make your code work, simply
substitute the pop parameter of the block with msg and remove the first
line of the block:

conn.mails.each do |msg|

# Print the 'From:' header line
puts msg.header.split("\r\n").grep(/^From: /)

# Put message to $stdout (by calling <<)
puts "\nFull message:\n"
msg.all($stdout)
end

You can also use the each (or each_mail) methods of Net::pOPMail, which
also iterate on the mails:

conn.each_mail do |msg|

# Print the 'From:' header line
puts msg.header.split("\r\n").grep(/^From: /)

# Put message to $stdout (by calling <<)
puts "\nFull message:\n"
msg.all($stdout)
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,308
Latest member
Foster Bronson

Latest Threads

Top