module MailSpy class CoreMailer < ActionMailer::Base helper "mail_spy/email" def template(email) # Slight hack to provide information to helpers @_email_id = email.id @template_values = email.template_values # Set Headers email_hash = {} std_email_keys = [:to, :cc, :bcc, :from, :subject, :message_id, :sender, :reply_to] std_email_keys.each { |key| set_if_present(email_hash,email, key) } headers.merge!(email.headers) if email.headers.present? # Content of the email html_erb = email.email_template.html_erb || "" text_erb = email.email_template.html_erb || "" mail_message = mail(email_hash) do |format| format.text { render :inline => text_erb } format.html { render :inline => html_erb } end # Email Service provider setup # TODO Only support smtp at the moment raise "No Email service providers installed" unless MailSpy.esps.present? esp = MailSpy.esps[email.email_service_provider] esp_key = MailSpy.esps.keys[rand(MailSpy.esps.keys.count)] esp = MailSpy.esps[esp_key] if esp.blank? mail_message.delivery_method.settings.merge!( { :address => esp.address, :user_name => esp.user_name, :password => esp.password, :port => esp.port, :authentication => esp.authentication, :enable_starttls_auto => esp.enable_starttls_auto, :domain => esp.domain }) mail_message end protected def set_if_present(email_hash, email, hash_key, email_key=nil) email_key = hash_key if email_key.nil? value = email.send("#{email_key}") email_hash[hash_key] = value if value.present? end end end