¿Cómo recuperar la URL del sitio web que proporciona el servicio de correo electrónico desde una cadena de correo electrónico?

I am using Ruby on Rails v3.0.9 and I am finding la mejor manera to retrieve the "last part" of a email string and the related web site URL (that is, the web site that provides the email service).

Por ejemplo, si tengo

sample_email_title@gmail.com

Me gustaria recuperar

gmail.com

and "transform" that so to have the following:

http://www.gmail.com

¿Cómo puedo lograrlo?

preguntado el 27 de agosto de 11 a las 15:08

2 Respuestas

Podrías hacer algo como esto:

a = "my_email@gmail.com"
b = a.split("@").last
=> "gmail.com"

"http://www." + b
=> "http://www.gmail.com"

You could do it all in one line with:

"http://www." + "my_email@gmail.com".split('@').last

There may be better ways, but this is fairly simple.

Respondido 27 ago 11, 19:08

The mail exchange server will often be on a different domain than the email address, so you will have to lookup the MX records using the DNS server to get that information:

require 'resolv'

def mx_host_of_domain(domain)
  mx = nil
  Resolv::DNS.open do |dns|
    servers = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
    if servers && !servers.empty?
      mx = servers.sort_by(&:preference).first.exchange.to_s
    end
  end
  mx
end

email = 'stackoverflow' + '@' + 'larshaugseth.com'

mxhost = mx_host_of_domain email.split('@').last
# => in1.smtp.messagingengine.com

url = "http://www.#{mxhost.split('.').last(2).join('.')}/"
# => http://www.messagingengine.com/

Note that there is no guarantee for a web server to be located at this address. In my case the real web address to the email service is https://www.fastmail.fm/, but luckily the one generated by using the above method will forward you there.

Respondido 27 ago 11, 22:08

+1 especially for the comments about the lack of usefulness of doing this. Why are you doing it, and why are you assuming that an email domain has a web site, or vice versa? - triples

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.