Last active
August 29, 2015 14:01
-
-
Save merqlove/d93e3889618d2754b8d3 to your computer and use it in GitHub Desktop.
Sinatra Mailer, similar with Rails :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Project | |
module Mailers | |
class BaseMailer < Sinatra::Mailer::Base | |
default from: ENV["MAIL_FROM"] || 'localhost', content_type: 'text/html' | |
class << self | |
def feedback!(text, email = nil, name='') | |
mail(to: "some@example.com", subject: 'Feedback', body: text) do | |
reply_to "#{name}<#{email}>" if email.present? | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/core_ext/class/attribute' | |
module Sinatra | |
module Mailer | |
class Base | |
class_attribute :default_params | |
self.default_params = { | |
mime_version: "1.0", | |
charset: "UTF-8", | |
content_type: "text/plain", | |
content_transfer_encoding: "8bit", | |
from: ENV['MAIL_FROM'] || 'localhost' | |
}.freeze | |
class << self | |
def default(value = nil) | |
self.default_params = default_params.merge(value).freeze if value | |
default_params | |
end | |
alias :default_options= :default | |
def mail(headers={}, &block) | |
default_values = {} | |
self.default.each do |k,v| | |
default_values[k] = v.is_a?(Proc) ? instance_eval(&v) : v | |
end | |
headers = headers.reverse_merge(default_values) | |
mail = Mail.new(headers, &block) | |
mail.deliver | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment