From c93681396792de0d659cba4bebc5397bcf824743 Mon Sep 17 00:00:00 2001 From: benni Date: Fri, 17 Aug 2012 19:08:02 +0200 Subject: [PATCH] Some fixes for mailer module. --- app/controllers/messages_controller.rb | 3 +- app/mailers/mailer.rb | 21 +++++-------- app/models/message.rb | 30 +++++++++++-------- .../{email.html.erb => email.text.erb} | 2 +- app/views/mailer/foodsoft_message.text.erb | 13 ++++++++ app/views/mailer/message.rhtml | 12 -------- 6 files changed, 41 insertions(+), 40 deletions(-) rename app/views/layouts/{email.html.erb => email.text.erb} (56%) create mode 100644 app/views/mailer/foodsoft_message.text.erb delete mode 100644 app/views/mailer/message.rhtml diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 1787a2ff..4ca5200b 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -15,7 +15,8 @@ class MessagesController < ApplicationController @message = @current_user.send_messages.new(params[:message]) if @message.save #FIXME: Send Mails wit ID instead of using message.state ... - call_rake :send_emails + #call_rake :send_emails + @message.deliver redirect_to messages_url, :notice => "Nachricht ist gespeichert und wird versendet." else render :action => 'new' diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 3132f8df..c6e12fd3 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -2,24 +2,19 @@ # ActionMailer class that handles all emails for the FoodSoft. class Mailer < ActionMailer::Base - layout 'email' # Use views/layouts/email.html.erb + layout 'email' # Use views/layouts/email.txt.erb - default :from => "FoodSoft <#{Foodsoft.config[:email_sender]}>" + default from: "FoodSoft <#{Foodsoft.config[:email_sender]}>" # Sends an email copy of the given internal foodsoft message. def foodsoft_message(message, recipient) - @body = message.body - @sender = message.sender.nick - @recipients = recipient.nick - @reply = url_for(:controller => "messages", :action => "reply", :id => message.id) - @link = url_for(:controller => "messages", :action => "show", :id => message.id) - @profile = url_for(:controller => "home", :action => "profile") + @message = message - mail :sender => Foodsoft.config[:email_sender], - :errors_to => Foodsoft.config[:email_sender], - :subject => "[#{Foodsoft.config[:name]}] " + message.subject, - :to => recipient.email, - :from => "#{message.sender.nick} <#{message.sender.email}>" + mail sender: Foodsoft.config[:email_sender], + errors_to: Foodsoft.config[:email_sender], + subject: "[#{Foodsoft.config[:name]}] " + message.subject, + to: recipient.email, + from: "#{message.sender.nick} <#{message.sender.email}>" end # Sends an email with instructions on how to reset the password. diff --git a/app/models/message.rb b/app/models/message.rb index 42cbd520..2cdb2549 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -43,15 +43,15 @@ class Message < ActiveRecord::Base def reply_to=(message_id) message = Message.find(message_id) - add_recipients(message.sender.to_a) + add_recipients([message.sender]) self.subject = "Re: #{message.subject}" - self.body = "#{message.sender.nick} schrieb am #{I18n.l(message.created_at.to_date)} um #{I18n.l(message.created_at, :format => :time)}:\n" + self.body = "#{message.sender.nick} schrieb am #{I18n.l(message.created_at, :format => :short)}:\n" message.body.each_line{ |l| self.body += "> #{l}" } end def mail_to=(user_id) user = User.find(user_id) - add_recipients(user.to_a) + add_recipients([user]) end # Returns true if this message is a system message, i.e. was sent automatically by the FoodSoft itself. @@ -67,20 +67,24 @@ class Message < ActiveRecord::Base User.find(recipients_ids) end + def deliver + for recipient in recipients + if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank? + begin + Mailer.foodsoft_message(self, recipient).deliver +# rescue +# logger.warn "Deliver failed for #{recipient.nick}: #{recipient.email}" + end + end + end + update_attribute(:email_state, 1) + end + # Sends all pending messages that are to be send as emails. def self.send_emails messages = Message.pending for message in messages - for recipient in message.recipients - if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank? - begin - Mailer.foodsoft_message(message, recipient).deliver - rescue - logger.warn "Deliver failed for #{recipient.nick}: #{recipient.email}" - end - end - end - message.update_attribute(:email_state, 1) + message.deliver end end end diff --git a/app/views/layouts/email.html.erb b/app/views/layouts/email.text.erb similarity index 56% rename from app/views/layouts/email.html.erb rename to app/views/layouts/email.text.erb index a5aa7eff..fdc39233 100644 --- a/app/views/layouts/email.html.erb +++ b/app/views/layouts/email.text.erb @@ -1,6 +1,6 @@ <%= yield %> -- -FoodSoft: <%= url_for(:controller => "home", :action => "index", :only_path => false) %> +FoodSoft: <%= root_url %> Foodcoop-Homepage: <%= Foodsoft.config[:homepage] %> Hilfe/Help: <%= Foodsoft.config[:help_url] %> diff --git a/app/views/mailer/foodsoft_message.text.erb b/app/views/mailer/foodsoft_message.text.erb new file mode 100644 index 00000000..fbeae93e --- /dev/null +++ b/app/views/mailer/foodsoft_message.text.erb @@ -0,0 +1,13 @@ +Foodsoft-Nachricht + +Von: <%= @message.sender.nick %> + +====================================================================== + +<%= @message.body %> + +====================================================================== + +Antworten: <%= new_message_url('message[reply_to]' => @message.id) %> +Nachricht online einsehen: <%= message_url(@message) %> +Nachrichten-Einstellungen: <%= my_profile_url %> \ No newline at end of file diff --git a/app/views/mailer/message.rhtml b/app/views/mailer/message.rhtml deleted file mode 100644 index 4824c7f9..00000000 --- a/app/views/mailer/message.rhtml +++ /dev/null @@ -1,12 +0,0 @@ -Foodsoft-Nachricht - -Von: <%= @sender %> -An: <%= @recipients %> - -====================================================================== -<%= @body %> -====================================================================== - -Antworten: <%= @reply %> -Nachricht online einsehen: <%= @link %> -Nachrichten-Einstellungen: <%= @profile %>