Class: SendGmail::Client

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

Constant Summary collapse

SMTP_DOMAIN =
"smtp.gmail.com"
SMTP_PORT =
587
DEFAULT_DATE =
Time.now
DEFAULT_MIME_VESION =
"1.0"

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &block) ⇒ Client

creates a new Gmail client instance.

SendGmail::Client.new do |c|
  c. = "[email protected]"
  c.password = "yourpassword"
end


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sendgmail.rb', line 33

def initialize(args = {}, &block)
  @config = Config.new do |c|
    c. = args[:account]
    c.password = args[:password] 
    c.smtp_domain = args[:smtp_domain] ||= SMTP_DOMAIN
    c.smtp_port = args[:smtp_port] ||= SMTP_PORT
  end
  if block_given?
    block.call @config
  end
end

Instance Method Details

#create_mail(params = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sendgmail.rb', line 69

def create_mail(params ={})
  mail         = ::TMail::Mail.new
  mail.to      = params[:to]
  mail.from    = params[:from]
  mail.subject = params[:subject]
  mail.date    = params[:date] ||= DEFAULT_DATE
  mail.mime_version = params[:mime_version] ||= DEFAULT_MIME_VESION
  mail.set_content_type 'text', 'plain', {'charset'=>'iso-2022-jp'}
  mail.body    = params[:body]
  return mail
end

#send(params = {}) ⇒ Object

send your mail from Gmail.

client.send(
  :to   => '[email protected]',
  :from => '[email protected]',
  :subject => 'hello',
  :body    => 'world'
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sendgmail.rb', line 54

def send(params = {})
  mail = create_mail(params)

  ::Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
  ::Net::SMTP.start(
    @config.smtp_domain,
    @config.smtp_port,
    "localhost.localdomain",
    @config.,
    @config.password,
    "plain"){ |smtp| 
    smtp.sendmail(mail.encoded, mail.from, mail.to) 
  }
end