Email Parsing

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

Robert J. 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+})

That worked out well thank you very much.

On May 12, 2011, at 13:28 , Robert J. wrote:

end

BTW… we’re discussing stabbing you in the ‘indenting “end”’ thread. :slight_smile:

On Fri, May 13, 2011 at 08:57:27AM +0900, Ryan D. wrote:

BTW… we’re discussing stabbing you in the ‘indenting “end”’ thread.

I, for one, would really rather not be stabbed in the indenting end.

On Fri, May 13, 2011 at 1:57 AM, Ryan D. [email protected]
wrote:

BTW… we’re discussing stabbing you in the ‘indenting “end”’ thread. :slight_smile:

def stab
“ouch”
end

SCNR. :smiley:


Phillip G.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs