Class: CamaleonCms::HtmlMailer

Inherits:
ActionMailer::Base
  • Object
show all
Includes:
HooksHelper, PluginsHelper, SiteHelper
Defined in:
app/mailers/camaleon_cms/html_mailer.rb

Instance Method Summary collapse

Methods included from PluginsHelper

#current_plugin, #plugin_asset_path, #plugin_asset_url, #plugin_destroy, #plugin_install, #plugin_layout, #plugin_load_helpers, #plugin_uninstall, #plugin_upgrade, #plugin_view, #plugins_initialize, #self_plugin_key

Methods included from SiteHelper

#cama_current_site_host_port, #cama_get_list_layouts_files, #cama_get_list_template_files, #cama_is_test_request?, #current_locale, #current_site, #current_theme, #site_after_install, #site_install_theme, #site_uninstall_theme

Methods included from HooksHelper

#hook_run, #hook_skip, #hooks_run

Instance Method Details

#sender(email, subject = 'Hello', data = {}) ⇒ Object

content='', from=nil, attachs=[], url_base='', current_site, template_name, layout_name, extra_data, format, cc_to


10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/mailers/camaleon_cms/html_mailer.rb', line 10

def sender(email, subject='Hello', data = {})
  data = data.to_sym
  if data[:current_site].present?
    if data[:current_site].is_a?(Integer)
      data[:current_site] = CamaleonCms::Site.find(data[:current_site]).decorate
    end
  else
    data[:current_site] = CamaleonCms::Site.main_site.decorate
  end
  @current_site = data[:current_site]
  data = {
    cc_to: @current_site.get_option("email_cc", '').split(','),
    from: @current_site.get_option("email_from") || @current_site.get_option("email"),
    template_name: 'mailer',
    layout_name: 'camaleon_cms/mailer',
    format: 'html',
  }.merge(data)
  data[:cc_to] = [data[:cc_to]] if data[:cc_to].is_a?(String) || !data[:cc_to].present?

  mail_data = {to: email, subject: subject}
  if @current_site.get_option("mailer_enabled") == 1
    mail_data[:delivery_method] = :smtp
    mail_data[:delivery_method_options] = {
      user_name: @current_site.get_option("email_username"),
      password: @current_site.get_option("email_pass"),
      address: @current_site.get_option("email_server"),
      port: @current_site.get_option("email_port"),
      domain: (@current_site.the_url.to_s.parse_domain rescue "localhost"),
      authentication: "plain",
      enable_starttls_auto: true,
    }
  end
  mail_data[:cc] = data[:cc_to].clean_empty.join(",") if data[:cc_to].present?
  mail_data[:from] = data[:from] if data[:from].present?
  
  data[:mail_data] = mail_data
  hooks_run('email_late', data)
  
  @subject = subject
  @html = data[:content]
  @url_base = data[:url_base]
  @extra_data = data[:extra_data]

  views_dir = "app/apps/"
  self.prepend_view_path(File.join($camaleon_engine_dir, views_dir).to_s)
  self.prepend_view_path(Rails.root.join(views_dir).to_s)

  theme = @current_site.get_theme
  lookup_context.prefixes.prepend("themes/#{theme.slug}") if theme.settings["gem_mode"]
  lookup_context.prefixes.prepend("themes/#{theme.slug}/views") unless theme.settings["gem_mode"]
  lookup_context.use_camaleon_partial_prefixes = true
  ((data[:files] || []) + (data[:attachments] || [])).each{ |attach|
    if File.exist?(attach) && !File.directory?(attach)
      attachments["#{File.basename(attach)}"] = File.open(attach, 'rb') { |f| f.read }
    else
      Rails.logger.error "Camaleon CMS - File attached in the email doesn't exist: #{attach}".cama_log_style(:red)
    end
  }

  layout = data[:layout_name].present? ? data[:layout_name] : false
  if data[:template_name].present? # render email with template
    mail(mail_data) { |format| format.html { render data[:template_name], layout: layout } } if data[:format] == "html"
    mail(mail_data) { |format| format.text { render data[:template_name], layout: layout } } if data[:format] == "txt"
  else # inline render content
    mail(mail_data) { |format| format.html { render inline: @html, layout: layout } } if data[:format] == "html"
    mail(mail_data) { |format| format.text { render inline: @html, layout: layout } } if data[:format] == "txt"
  end
  mail(mail_data) unless data[:format].present?
end