2015-04-24 121 views
0

如何使用ruby在rails上檢查電子郵件地址?如何查看電子郵件地址是否有效?

有沒有寶石插件?

任何我能找到的電子郵件地址都是實時的。

+0

你檢查https://github.com/kamilc/email_verifier寶石? –

+0

我是一名網頁設計師,所以對後端代碼不太瞭解。但對於你的問題,我認爲有一些東西。檢查它「gmail-smtp-in」。 –

回答

1

看看這個Email validator based on DNS集成在ActiveModel::Validator這意味着你可以在Rails應用程序中使用它:

require 'resolv' 

class EmailValidator < ActiveModel::EachValidator 
    def validate_each(record, attribute, value) 
    if Resolv::DNS.new.getresources(value.split("@").last, Resolv::DNS::Resource::IN::MX).empty? 
     record.errors[attribute] << (options[:message] || "does not have a valid domain") 
    end 
    rescue Resolv::ResolvError, Resolv::ResolvTimeout 
    record.errors[attribute] << (options[:message] || "does not have a valid domain") 
    end 
end 

And this code i use in my sent email script

require 'resolv' 

module MailMachine 
    class Validator 
    def initialize(name) 
     @name = name.split('@').last 
    end 

    def servers_list 
     Resolv::DNS.open.getresources(@name, Resolv::DNS::Resource::IN::MX).map do |resolv| 
     resolv.exchange.to_s 
     end  
    end 

    def server 
     servers_list.find do |name| 
     Resolv.getaddress(name) =~ Resolv::AddressRegex 
     end 
    rescue Resolv::ResolvError => e 
     #holly molyy 
     return false if e.is_a? Exception 
    end 
    end 
end 
相關問題