foodsoft/app/mailers/mailer.rb

121 lines
3.7 KiB
Ruby
Raw Normal View History

2012-08-06 12:00:40 +02:00
# encoding: utf-8
2014-06-18 13:28:19 +02:00
# ActionMailer class that handles all emails for Foodsoft.
2011-05-11 10:53:18 +02:00
class Mailer < ActionMailer::Base
# XXX Quick fix to allow the use of show_user. Proper take would be one of
# (1) Use draper, decorate user
# (2) Create a helper with this method, include here and in ApplicationHelper
helper :application
include ApplicationHelper
2011-05-11 10:53:18 +02:00
2012-08-17 19:08:02 +02:00
layout 'email' # Use views/layouts/email.txt.erb
2011-05-11 10:53:18 +02:00
default from: "#{I18n.t('layouts.foodsoft')} <#{FoodsoftConfig[:email_sender]}>",
'X-Auto-Response-Suppress' => 'All'
2011-05-11 10:53:18 +02:00
# Sends an email with instructions on how to reset the password.
# Assumes user.setResetPasswordToken has been successfully called already.
def reset_password(user)
@user = user
@link = new_password_url(id: @user.id, token: @user.reset_password_token)
2011-05-11 10:53:18 +02:00
mail to: user,
subject: I18n.t('mailer.reset_password.subject', username: show_user(user))
2011-05-11 10:53:18 +02:00
end
2011-05-11 10:53:18 +02:00
# Sends an invite email.
def invite(invite)
@invite = invite
@link = accept_invitation_url(token: @invite.token)
2011-05-11 10:53:18 +02:00
mail to: invite.email,
subject: I18n.t('mailer.invite.subject')
2011-05-11 10:53:18 +02:00
end
# Notify user of upcoming task.
def upcoming_tasks(user, task)
@user = user
@task = task
mail to: user,
subject: I18n.t('mailer.upcoming_tasks.subject')
2011-05-11 10:53:18 +02:00
end
# Sends order result for specific Ordergroup
def order_result(user, group_order)
@order = group_order.order
@group_order = group_order
mail to: user,
subject: I18n.t('mailer.order_result.subject', name: group_order.order.name)
2011-05-11 10:53:18 +02:00
end
# Notify user if account balance is less than zero
def negative_balance(user,transaction)
@group = user.ordergroup
@transaction = transaction
mail to: user,
subject: I18n.t('mailer.negative_balance.subject')
2011-05-11 10:53:18 +02:00
end
def feedback(user, feedback)
2011-05-11 10:53:18 +02:00
@user = user
@feedback = feedback
2011-05-11 10:53:18 +02:00
mail to: FoodsoftConfig[:notification][:error_recipients],
from: user,
subject: I18n.t('mailer.feedback.subject')
2011-05-11 10:53:18 +02:00
end
def not_enough_users_assigned(task, user)
2011-05-11 10:53:18 +02:00
@task = task
@user = user
mail to: user,
subject: I18n.t('mailer.not_enough_users_assigned.subject', task: task.name)
end
def mail(args)
args[:message_id] ||= "#{Mail.random_tag}@#{default_url_options[:host]}"
args[:subject] = "[#{FoodsoftConfig[:name]}] #{args[:subject]}"
if args[:from].is_a? User
args[:reply_to] ||= args[:from]
args[:from] = "#{I18n.t('mailer.from_via_foodsoft', name: show_user(args[:from]))} <#{FoodsoftConfig[:email_sender]}>"
end
[:bcc, :cc, :reply_to, :sender, :to].each do |k|
user = args[k]
args[k] = "#{show_user user} <#{user.email}>" if user.is_a? User
end
if contact_email = FoodsoftConfig[:contact][:email]
args[:reply_to] ||= "#{FoodsoftConfig[:name]} <#{contact_email}>"
end
2017-08-19 16:24:02 +02:00
reply_email_domain = FoodsoftConfig[:reply_email_domain]
if reply_email_domain && !args[:return_path] && args[:to].is_a?(String)
#TODO: Remove workaround for https://github.com/mikel/mail/issues/39 after next mail release
ascii_to = args[:to].encode('ASCII', invalid: :replace, undef: :replace, replace: '_')
address = Mail::Parsers::AddressListsParser.new.parse(ascii_to).addresses.first
2017-08-19 16:24:02 +02:00
args[:return_path] = "<#{FoodsoftConfig.scope}.bounce+#{address.local}=#{address.domain}@#{reply_email_domain}>"
end
super
2011-05-11 10:53:18 +02:00
end
def self.deliver_now_with_user_locale(user, &block)
I18n.with_locale(user.settings['profile']['language']) do
self.deliver_now &block
end
end
def self.deliver_now
message = yield
message.deliver_now
rescue => error
MailDeliveryStatus.create email: message.to[0], message: error.message
end
2011-05-11 10:53:18 +02:00
end