Class: Mmailer::MailHelper

Inherits:
Object
  • Object
show all
Includes:
ErrorHandling
Defined in:
lib/mmailer/mail_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorHandling

#try

Methods included from Client

#client

Constructor Details

#initialize(args) ⇒ MailHelper

Returns a new instance of MailHelper.



6
7
8
9
10
11
# File 'lib/mmailer/mail_helper.rb', line 6

def initialize(args)
  set_provider args.fetch(:provider, :mandrill)
  @template=args[:template]
  @subject=args[:subject]
  @from=args[:from]
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/mmailer/mail_helper.rb', line 4

def from
  @from
end

#subjectObject (readonly)

Returns the value of attribute subject.



4
5
6
# File 'lib/mmailer/mail_helper.rb', line 4

def subject
  @subject
end

#templateObject (readonly)

Returns the value of attribute template.



4
5
6
# File 'lib/mmailer/mail_helper.rb', line 4

def template
  @template
end

Instance Method Details

#send_email(user) ⇒ Object



18
19
20
21
22
23
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
# File 'lib/mmailer/mail_helper.rb', line 18

def send_email(user)

  mail = Mail.new 
  mail.to = user.email
  mail.from = from
  mail.subject = subject
  #Defaulting to UTF-8, set your own if this is incorrect.
  mail.charset = 'UTF-8'
  mail.content_transfer_encoding = '8bit'
  
  compiled_source=ERB.new(File.read(Dir.pwd + "/" + Mmailer.configuration.template + ".md.erb")).result(binding)

  text_part = Mail::Part.new
  text_part.body=compiled_source

  html_part = Mail::Part.new
  html_part.content_type='text/html; charset=UTF-8'
  html_part.body=Kramdown::Document.new(compiled_source).to_html

  mail.text_part = text_part
  mail.html_part = html_part

  case ENV['MMAILER_ENV']
    when "production"
      try { mail.deliver! }
    when "development"
      puts mail.to_s
    else
      mail.to_s
  end
end

#set_provider(provider) ⇒ Object



13
14
15
16
# File 'lib/mmailer/mail_helper.rb', line 13

def set_provider(provider)
  providers = {gmail: Providers.gmail, mandrill: Providers.mandrill, zoho: Providers.zoho, mailgun: Providers.mailgun }
  Mail.defaults(&providers[provider])
end