如何使用ruby on rails检查电子邮件地址?
有没有任何宝石插件?
我能找到的任何电子邮件地址都是实时的。
答案 0 :(得分:1)
查看ActiveModel::Validator
中集成的Email validator based on DNS
,这意味着您可以在Rails
app中使用它:
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