简体   繁体   中英

How can I extract all URls/links from an email using Ruby on Rails?

I am building a bookmarking site. I want to extract all URIs/links from an email. My site is in using Ruby on Rails.

How can I extract all the URLs of received email content?

Ruby's built-in URI module does this already:

From the extract docs:

require "uri"

URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
# => ["http://foo.example.com/bla", "mailto:test@example.com"]
require 'uri'

text = %{"test
<a href="http://www.a.com/">http://www.a.com/</a>, and be sure
to check http://www.a.com/blog/. Email me at <a href="mailto:b@a.com">b@a.com</a>.}


END_CHARS = %{.,'?!:;}
p URI.extract(text, ['http']).collect { |u| END_CHARS.index(u[-1]) ? u.chop : u }

Source: http://www.java2s.com/Code/Ruby/Network/ExtractURL.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM