【问题标题】:check mail address whether is under gmail client <Ruby> [closed]检查邮件地址是否在 gmail 客户端 <Ruby> [关闭]
【发布时间】:2013-03-28 04:30:21
【问题描述】:

Gmail 能够让用户创建任何电子邮件样式,例如 account1@gmail.com account2@company.com account3@university.com

还有更多。 目前,我正在使用来自 https://github.com/liangzan/contacts 的联系人 gem, 但是当我使用 account2@company.com 之类的帐户登录时,它无法获取我的联系人,因为我阅读代码时的电子邮件地址不是 @gmail 或 @googlemail。

那么,如何查看邮箱地址是否在gmail地址下?

【问题讨论】:

    标签: ruby-on-rails ruby gmail email-client


    【解决方案1】:

    您可以检查他们的 MX 记录以查看它们是否由 googlemail 服务器托管。

    如果在域的 mx 记录中找到 googlemail 服务器,此方法将返回 true。 它使用google的dns服务器(8.8.8.8)

    require 'resolv'
    
    def isGmailAddress?(address)
      domain = address.split("@").last
      Resolv::DNS.open({:nameserver=>["8.8.8.8"]}) do |r|
        mx = r.getresources(domain,Resolv::DNS::Resource::IN::MX)
        if mx.any? {|server| server.exchange.to_s.include? "googlemail" or server.exchange.to_s.include? "gmail-smtp-in.l.google.com"} then
          return true
        end
        return false
      end
    end
    
    p isGmailAddress?("emailaddress@gmail.com")
    

    http://ruby-doc.org/stdlib-1.9.2/libdoc/resolv/rdoc/Resolv.html

    【讨论】:

    • 在 Rails 控制台中不能要求解析?
    • 可以,也许你没有图书馆?
    • 嗯...谢谢!它只是在里面预加载...
    • 这个方法对你有用吗?
    • emrm,不...它为我的 gmail 地址“nichthinkof30@gmail.com”以及我的 hotmail 返回 false
    猜你喜欢
    • 2015-07-02
    • 2011-04-08
    • 1970-01-01
    • 2016-04-05
    • 2016-04-06
    • 2012-11-10
    • 2011-09-23
    • 2011-03-28
    • 2016-08-20
    相关资源
    最近更新 更多