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

#initialize(params = {}) ⇒ SMTP

– initialize ++



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

def initialize(params = {})
  @port = Net::SMTP.default_port
  @helo_domain = "localhost"
  Config.set_values(self, params)
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

#send_mail(subject, message, params = {}) ⇒ Object

– send_mail ++



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
55
56
57
58
59
60
61
62
# File 'lib/easysuite/smtp.rb', line 25

def send_mail(subject, message, params = {})
  params = params.dup
  params[:from] ||= @from
  params[:to]   ||= @to
  params[:cc]   ||= @cc
  params[:bcc]  ||= @bcc
  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 = []

  ["From", "To", "Cc", "Bcc"].each do |key|
    next unless value = params[key.downcase.to_sym]
    headers << make_header(key, value)
  end

  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(params), rcpt_to(params))
  end
end