Class: GmailSender

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

Constant Summary collapse

ATTACHMENT_READ_PORTION =

you may change this, but must be multiply to 3

150360

Instance Method Summary collapse

Constructor Details

#initialize(user, password, domain = "gmail.com") ⇒ GmailSender

Returns a new instance of GmailSender.



8
9
10
11
12
13
14
15
16
# File 'lib/gmail_sender.rb', line 8

def initialize(user, password, domain="gmail.com")
  @sender_domain   = domain
  @sender_password = password
  @sender_email     = "#{user}@#{domain}"
  @net_smtp = Net::SMTP.new("smtp.gmail.com", 587)
  @net_smtp.enable_starttls
  @attachments = []
  @boundary = rand(2**256).to_s(16)
end

Instance Method Details

#attach(file) ⇒ Object



18
19
20
# File 'lib/gmail_sender.rb', line 18

def attach(file)
  @attachments << file if File.exist?(file)
end

#send(to, subject = "", content = "") ⇒ Object



22
23
24
25
26
27
28
# File 'lib/gmail_sender.rb', line 22

def send(to, subject = "", content = "")
  @net_smtp.start(@sender_domain, @sender_email, @sender_password, :plain) do |smtp|
    smtp.open_message_stream(@sender_email, [to]) do |msg_stream|
      set_message_stream(msg_stream, to, subject, content)
    end
  end
end

#set_message_stream(msg_stream, to, subject, content) ⇒ Object



30
31
32
33
34
# File 'lib/gmail_sender.rb', line 30

def set_message_stream(msg_stream, to, subject, content)
  set_headers(msg_stream, to, subject)
  set_content(msg_stream, content)
  set_attachments(msg_stream)
end