Skip to content

Instantly share code, notes, and snippets.

@ylluminate
Last active January 4, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ylluminate/8593384 to your computer and use it in GitHub Desktop.
Save ylluminate/8593384 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/smtp'
module MailMachine
class NetSmtp < Net::SMTP
class << self
EXCEPTIONS = [Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPSyntaxError,
Net::SMTPFatalError,
Net::SMTPUnknownError,
Net::OpenTimeout]
def start_server(server, port, helo, from, to)
## Options for sent mail ##
# `server` - typical 'localhost' #string
# `port` - typical '9999' #integer
# `helo` - domain name server.Should be good! #string
# `from` - from email or name #string
# `to` - email to #string
############################################
## Debug to mailcatcher
server ||= '127.0.0.1'
port ||= 1025
helo ||= 'localhost'
#######################
begin
smtp = new(server, port).start(helo)
#Very bad open hole
smtp.set_debug_output $stderr
smtp.send_message("this can be email template", from, [to])
smtp.finish
rescue *EXCEPTIONS => e
response = Net::SMTP::Response.parse(e.to_s)
rescue Exception => e
response = Net::SMTP::Response.new("666", e.to_s)
end
response ||= Net::SMTP::Response.new("250", "Email delivery")
end
end
end
end
#!/usr/bin/env ruby
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment