Email Parsing

R

Robert Johns

How would I only pull links from the email's body and not the full
email.

require 'net/pop'
require 'rubygems'
require 'tmail'

username = ' '
pass = ' '

Net::pOP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
Net::pOP3.start('pop.gmail.com', 995, username, pass) do |pop|
if pop.mails.empty?
puts "No mail"
else
pop.each_mail do |mail|
email = TMail::Mail.parse(mail.pop)
p email.body
end
end
end


Thanks
 
B

Brian Candler

Robert Johns wrote in post #998352:
How would I only pull links from the email's body

"Links" to me implies that this is an HTML email. In that case, just
parse the body with an HTML parser (e.g. nokogiri). It can easily pull
out all the A (anchor) tags with their href attributes.

If it's plain text, but contains URLs like http://..., then you can
match the body against a regexp. e.g.

p body.scan(%r{\bhttps?://\S+})
 
R

Ryan Davis

Net::pOP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
Net::pOP3.start('pop.gmail.com', 995, username, pass) do |pop|
if pop.mails.empty?
puts "No mail"
else
pop.each_mail do |mail|
email = TMail::Mail.parse(mail.pop)
p email.body
end
end
end

BTW... we're discussing stabbing you in the 'indenting "end"' thread. :)
 
P

Phillip Gawlowski

BTW... we're discussing stabbing you in the 'indenting "end"' thread. :)

def stab
"ouch"
end

SCNR. :D


--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top