def action_mailer_with_temporary_delivery_method
existing_delivery_method = ActionMailer::Base.delivery_method
existing_smtp_settings = ActionMailer::Base.smtp_settings
begin
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "your.domain.com",
:authentication => :plain,
:user_name => "do-not-reply@your.domain.com",
:password => "PASSword"
}
yield
ensure
ActionMailer::Base.delivery_method = existing_delivery_method
ActionMailer::Base.smtp_settings = existing_smtp_settings
end
end
def variables
result = []
@variables.reject { |k,v| k.to_s.downcase.eql?('password') }.each_pair do |key, value|
value = value.is_a?(Proc) ? value.call : value
value = value.is_a?(Array) ? value.join(", ") : value
result << "#{key} : #{value}"
end
result.sort.join("\n")
end
before 'deploy', 'deploy:notify'
namespace :deploy do
desc "sends a notification email letting everyone know we're deploying"
task :notify do
deployment_name = application.to_s.gsub(/(-.*)?/, '').upcase
stage_name = stage.to_s.upcase
action_mailer_with_temporary_delivery_method do
mail = TMail::Mail.new
mail.subject, = "Pending Deploy [#{deployment_name} #{stage}]"
mail.to, mail.from = "deploy-list@nomnom.com", ActionMailer::Base.smtp_settings[:user_name]
mail.date = Time.now
mail.body = ActionMailer::Utils.normalize_new_lines(%Q(
Hello everyone,
We're making a deploy to the #{stage} environment. The #{stage} site may (or may not) be unavailable for a few minutes sometime within the next 30 minutes (depending on how long it takes to upload the changes).
This message is automated.
---------------------- debugging information ----------------------
#{variables}
))
ActionMailer::Base.deliver(mail)
end
end
end