class Sendmail
Public Class Methods
send_mail(smtp_host, smtp_port, logger, mail)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 58 def self.send_mail (smtp_host, smtp_port, logger, mail) validate_mail(mail) msg = build_message(mail) result = nil begin config = Sendmail.prepare_config(smtp_host, smtp_port, true) s = Sendmail.open_socket(config) myhostname = config[:test] ? 'sender' : Socket.gethostname Sendmail.send_to_socket(config, s, myhostname, msg, mail[:mail_from], mail[:recipient]) result = s rescue => e logger.log "Error: Unable to send mail: #{e.class}: #{e.message}" end if $test $quickml_sendmail = [smtp_host, smtp_port, mail[:mail_from], mail[:recipient], msg] end return result # Only for test end
Private Class Methods
build_message(mail)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 98 def self.build_message(mail) return mail[:header].map {|field| key, value = field "#{key}: #{value}\n" }.join+"\n"+mail[:body] end
open_socket(c)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 118 def self.open_socket(c) klass = c[:test] ? MockSmtpServer : TCPSocket s = $ml_sm = klass.open(c[:smtp_host], c[:smtp_port]) # $ml_sm is for test return s end
prepare_config(smtp_host, smtp_port, use_xverp = false, test = false)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 105 def self.prepare_config(smtp_host, smtp_port, use_xverp = false, test = false) test = true if defined?($test) && $test config = { :smtp_port => smtp_port, :smtp_host => smtp_host, :use_xverp => use_xverp, :xverp_available => false, :test => test, } return config end
send_command(c, s, command, code)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 156 def self.send_command (c, s, command, code) s.print(command + "\r\n") if command begin line = s.gets c[:xverp_available] = true if /^250-XVERP/.match(line) end while line[3] == ?- return_code = line[0, 3].to_i if return_code == code line else raise "smtp-error: #{command} => #{line}" end end
send_to_socket(c, s, myhostname, message, mail_from, recipients)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 124 def self.send_to_socket(c, s, myhostname, message, mail_from, recipients) recipients = [recipients] if recipients.kind_of?(String) send_command(c, s, nil, 220) send_command(c, s, "EHLO #{myhostname}", 250) if c[:use_xverp] and c[:xverp_available] and (not mail_from.empty?) send_command(c, s, "MAIL FROM: <#{mail_from}> XVERP===", 250) else send_command(c, s, "MAIL FROM: <#{mail_from}>", 250) end recipients.each {|recipient| send_command(c, s, "RCPT TO: <#{recipient}>", 250) } send_command(c, s, 'DATA', 354) message.each_line {|line| line.sub!(/\r?\n/, "") line.sub!(/^\./, '..') line << "\r\n" s.print(line) } send_command(c, s, '.', 250) send_command(c, s, 'QUIT', 221) s.close end
validate_mail(mail)
click to toggle source
# File vendor/qwik/lib/qwik/util-sendmail.rb, line 87 def self.validate_mail(mail) if mail[:mail_from].nil? || mail[:recipient].nil? || mail[:header].nil? || mail[:body].nil? raise "Missing mail header." end mail[:recipient] = [mail[:recipient]] if mail[:recipient].kind_of?(String) end