ruby 电子邮件ping.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 电子邮件ping.rb相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env ruby
#
# = Email Ping
#
# Check to see if an email address exists by looking up MX records and connecting
# to the address's home SMTP server. It then starts to send a message to the address
# but quits before the message is actually sent.

require 'resolv'
require 'net/smtp'

address = ARGV[0].chomp
domain = address.split('@').last
dns = Resolv::DNS.new

puts "Resolving MX records for #{domain}..."
mx_records = dns.getresources domain, Resolv::DNS::Resource::IN::MX
mx_server  = mx_records.first.exchange.to_s
puts "Connecting to #{mx_server}..."

Net::SMTP.start mx_server, 25 do |smtp|
  smtp.helo "loldomain.com"
  smtp.mailfrom "test@loldomain.com"

  puts "Pinging #{address}..."

  puts "-" * 50

  begin
    smtp.rcptto address
    puts "Address probably exists."
  rescue Net::SMTPFatalError => err
    puts "Address probably doesn't exist."
  end
end

# vim: set ft=ruby ts=2 sw=2 :

以上是关于ruby 电子邮件ping.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby:如何从标准输入解析电子邮件 mime

Ruby,Rails - 如何检查 var 是不是是电子邮件

使用 Ruby 脚本发送电子邮件 - 501 5.5.4 无效地址

如何使用 Ruby 的邮件 gem 通过 smtp 发送电子邮件?

ruby 检查电子邮件内容差异。

ruby 电子邮件正则表达式