2015-08-23 20:21:04 +00:00
|
|
|
module EmailHelper
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-08-30 11:58:05 +00:00
|
|
|
get available driver
|
|
|
|
|
|
|
|
result = EmailHelper.available_driver
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
:inbound => ['imap', 'pop3'],
|
|
|
|
:outbound => ['smtp', 'sendmail'],
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.available_driver
|
|
|
|
if Setting.get('system_online_service')
|
|
|
|
return {
|
2015-08-30 12:04:48 +00:00
|
|
|
inbound: %w(imap pop3),
|
|
|
|
outbound: %w(smtp),
|
2015-08-30 11:58:05 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
{
|
2015-08-30 12:04:48 +00:00
|
|
|
inbound: %w(imap pop3),
|
|
|
|
outbound: %w(smtp sendmail),
|
2015-08-30 11:58:05 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-08-23 20:21:04 +00:00
|
|
|
get mail parts
|
|
|
|
|
|
|
|
user, domain = EmailHelper.parse_email('somebody@example.com')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
[user, domain]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.parse_email(email)
|
|
|
|
user = nil
|
|
|
|
domain = nil
|
|
|
|
if email =~ /^(.+?)@(.+?)$/
|
|
|
|
user = $1
|
|
|
|
domain = $2
|
|
|
|
end
|
|
|
|
[user, domain]
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get list of providers with inbound and outbound settings
|
|
|
|
|
|
|
|
map = EmailHelper.provider(email, password)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
google: {
|
|
|
|
domain: 'gmail.com|googlemail.com|gmail.de',
|
|
|
|
inbound: {
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: 'imap.gmail.com',
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: 'smtp.gmail.com',
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.provider(email, password)
|
|
|
|
# check domain based attributes
|
|
|
|
provider_map = {
|
|
|
|
google: {
|
|
|
|
domain: 'gmail.com|googlemail.com|gmail.de',
|
|
|
|
inbound: {
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: 'imap.gmail.com',
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: 'smtp.gmail.com',
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
microsoft: {
|
|
|
|
domain: 'outlook.com|hotmail.com',
|
|
|
|
inbound: {
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: 'imap-mail.outlook.com',
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: 'smtp-mail.outlook.com',
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
provider_map
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get possible inbound settings based on mx
|
|
|
|
|
|
|
|
map = EmailHelper.provider_inbound_mx(user, email, password, mx_domains)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: mx_domains[0],
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: mx_domains[0],
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.provider_inbound_mx(user, email, password, mx_domains)
|
|
|
|
inbounds = []
|
|
|
|
mx_domains.each {|domain|
|
|
|
|
inbound = [
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
inbounds = inbounds.concat(inbound)
|
|
|
|
}
|
|
|
|
inbounds
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get possible inbound settings based on guess
|
|
|
|
|
|
|
|
map = EmailHelper.provider_inbound_guess(user, email, password, domain)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.provider_inbound_guess(user, email, password, domain)
|
2015-08-23 20:26:03 +00:00
|
|
|
inbound = [
|
2015-08-23 20:21:04 +00:00
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "imap.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "imap.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'pop3',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 995,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'pop3',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 995,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'pop3',
|
|
|
|
options: {
|
|
|
|
host: "pop.#{domain}",
|
|
|
|
port: 995,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'pop3',
|
|
|
|
options: {
|
|
|
|
host: "pop.#{domain}",
|
|
|
|
port: 995,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'pop3',
|
|
|
|
options: {
|
|
|
|
host: "pop3.#{domain}",
|
|
|
|
port: 995,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'pop3',
|
|
|
|
options: {
|
|
|
|
host: "pop3.#{domain}",
|
|
|
|
port: 995,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
2015-08-23 20:26:03 +00:00
|
|
|
inbound
|
2015-08-23 20:21:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get possible outbound settings based on mx
|
|
|
|
|
|
|
|
map = EmailHelper.provider_outbound_mx(user, email, password, mx_domains)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.provider_outbound_mx(user, email, password, mx_domains)
|
2015-08-23 20:26:03 +00:00
|
|
|
outbounds = []
|
2015-08-23 20:21:04 +00:00
|
|
|
mx_domains.each {|domain|
|
2015-08-23 20:26:03 +00:00
|
|
|
outbound = [
|
2015-08-23 20:21:04 +00:00
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 465,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: domain,
|
|
|
|
port: 465,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
2015-08-23 20:26:03 +00:00
|
|
|
outbounds = outbounds.concat(outbound)
|
2015-08-23 20:21:04 +00:00
|
|
|
}
|
2015-08-23 20:26:03 +00:00
|
|
|
outbounds
|
2015-08-23 20:21:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get possible outbound settings based on guess
|
|
|
|
|
|
|
|
map = EmailHelper.provider_outbound_guess(user, email, password, domain)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 993,
|
|
|
|
ssl: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.provider_outbound_guess(user, email, password, domain)
|
2015-08-23 20:26:03 +00:00
|
|
|
outbound = [
|
2015-08-23 20:21:04 +00:00
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 465,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "mail.#{domain}",
|
|
|
|
port: 465,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "smtp.#{domain}",
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "smtp.#{domain}",
|
|
|
|
port: 25,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "smtp.#{domain}",
|
|
|
|
port: 465,
|
|
|
|
start_tls: true,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
|
|
|
host: "smtp.#{domain}",
|
|
|
|
port: 465,
|
|
|
|
start_tls: true,
|
|
|
|
user: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
2015-08-23 20:26:03 +00:00
|
|
|
outbound
|
2015-08-23 20:21:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get dns mx records of domain
|
|
|
|
|
|
|
|
mx_records = EmailHelper.mx_records('example.com')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
['mx1.example.com', 'mx2.example.com']
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.mx_records(domain)
|
|
|
|
mail_exchangers = mxers(domain)
|
|
|
|
if mail_exchangers && mail_exchangers[0]
|
|
|
|
Rails.logger.info "MX for #{domain}: #{mail_exchangers} - #{mail_exchangers[0][0]}"
|
|
|
|
end
|
|
|
|
mx_records = []
|
|
|
|
if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
|
|
|
|
mx_records.push mail_exchangers[0][0]
|
|
|
|
end
|
|
|
|
mx_records
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mxers(domain)
|
|
|
|
begin
|
|
|
|
mxs = Resolv::DNS.open do |dns|
|
|
|
|
ress = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
|
|
|
|
ress.map { |r|
|
|
|
|
[r.exchange.to_s, IPSocket.getaddress(r.exchange.to_s), r.preference]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
Rails.logger.error e.backtrace.inspect
|
|
|
|
end
|
|
|
|
mxs
|
|
|
|
end
|
|
|
|
|
2015-08-23 20:26:03 +00:00
|
|
|
end
|