Class: EasySuite::SMTP

Inherits:
Object
  • Object
show all
Defined in:
lib/easysuite/smtp.rb

Overview

– SMTP ++ Class to send mail easily.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSMTP

– initialize ++



16
17
18
19
# File 'lib/easysuite/smtp.rb', line 16

def initialize
  @port = Net::SMTP.default_port
  @helo_domain = "localhost"
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def 
  @account
end

#addressObject

Returns the value of attribute address.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def address
  @address
end

#authtypeObject

Returns the value of attribute authtype.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def authtype
  @authtype
end

#bccObject

Returns the value of attribute bcc.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def bcc
  @bcc
end

#ccObject

Returns the value of attribute cc.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def cc
  @cc
end

#fromObject

Returns the value of attribute from.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def from
  @from
end

#helo_domainObject

Returns the value of attribute helo_domain.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def helo_domain
  @helo_domain
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def password
  @password
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def port
  @port
end

#toObject

Returns the value of attribute to.



10
11
12
# File 'lib/easysuite/smtp.rb', line 10

def to
  @to
end

Instance Method Details

#mail_fromObject

– mail_from ++



59
60
61
# File 'lib/easysuite/smtp.rb', line 59

def mail_from
  extract_address(@from)
end

#rcpt_toObject

– rcpt_to ++



66
67
68
69
70
71
72
73
74
75
# File 'lib/easysuite/smtp.rb', line 66

def rcpt_to
  addrs = []

  ([@to].flatten + [@cc].flatten + [@bcc].flatten).each do |addr|
    next unless addr
    addrs << extract_address(addr)
  end

  addrs.uniq
end

#send_mail(subject, message) ⇒ Object

– send_mail ++



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/easysuite/smtp.rb', line 24

def send_mail(subject, message)
  smtp = Net::SMTP.new(@address, @port)

  case @port
  when Net::SMTP.default_port
  when Net::SMTP.default_tls_port
    smtp.enable_tls
  when Net::SMTP.default_submission_port
    smtp.enable_starttls
  else
    raise "Not supported port: #{@port}"
  end

  body, charset, transfer_encoding = make_body(message)
  headers = []
  headers << make_header("From", @from) if @from
  headers << make_header("To", @to) if @to
  headers << make_header("Cc", @cc) if @cc
  headers << make_header("Bcc", @bcc) if @bcc
  headers << make_header("Subject", subject)
  headers << make_header("Date", Time.now.strftime("%a, %d %b %Y %X %z"))
  headers << make_header("Mime-Version", "1.0")
  headers << make_header("Content-Type", "text/plain; charset=#{charset}")
  headers << make_header("Content-Transfer-Encoding", transfer_encoding)
  mailsrc = headers.join("\n")
  mailsrc += "\n\n#{body}"

  smtp.start(@helo_domain, @account, @password, @authtype) do |smtp|
    smtp.send_mail(mailsrc, mail_from, rcpt_to)
  end
end