2014-03-12 99 views
0

现在,团队中的任何团队成员都可以向团队发送邮件。当他们发送消息时,只需在队伍新闻源中发布消息。我正在编写一个故事,通过电子邮件发送给团队,通知团队发送了一条消息给团队。无法将收件人邮件传递给邮件发送人

我有消息邮件工作,但我无法传递收件人(电子邮件和用户名)的信息。目前我已将其硬编码发送到我的电子邮件。

由于原始消息模型已将收件人设置为类组,因此此信息不可用。此信息仅适用于班级成员。我尝试切换类,但是打破了其他逻辑。


class MessagesController < ApplicationController 
    load_and_authorize_resource 

    def create 
    @message.sender = current_member 
    @team = @message.recipient 
    respond_to do |format| 
     if @message.save  
     format.html { redirect_to @team, notice: 'Your message was sent.' } 
     format.json { render json: @message, status: :created, location: @team } 
     else 
     prepare_team_dashboard 
     format.html { render "teams/show" } 
     format.json { render json: @message.errors, status: :unprocessable_entity } 
     end 
    end 
    end 
end 

class Message < ActiveRecord::Base 
    belongs_to :sender, :class_name => "Member" 
    belongs_to :recipient, :class_name => "Team" 
    has_many :news_events, :as => :topic 

    validates :sender, :presence => true 
    validates :recipient, :presence => true 
    validates :message, :presence => true 

    attr_accessible :team, :team_id, :recipient, :recipient_id, :message, :image 

    after_create { news_events.create :team => recipient } 
    after_create :message_email_notificitaion 

    has_attached_file :image, 
    :styles => { :thumb => ["200x300>", :png] }, 
    :default_url => "/assets/empty.png", 
    :path => ":rails_env/:class/:attachment/:id_partition/:style.:extension" 

    def message_email_notificitaion 
    begin 
     MessageMailer.message_notification(self).deliver! 
    rescue => ex 
     Rails.logger.error "ERROR SENDING EMAIL" 
     Airbrake.notify ex 
    end 
    end 
end 

class MessageMailer < ActionMailer::Base 
    default from: "Charactr <[email protected]>" 

    def message_notification(message) 
    @message = message 
    team = @message.recipient 

    mail(
     bcc: team.members.map(&:email), 
     subject: "#{message.sender.username} sent a message to the team." 
    ) 
    end 
end 
+0

您是否考虑将电子邮件的发送移交给控制器本身,而不是AR回调? –

+0

我原来是这样做的,但根本无法收到电子邮件。 – jallen29

+0

通过“无法发送电子邮件”您是否意味着您收到错误或者是什么问题?看起来像允许'message_sent'接收你需要的参数,并从控制器中传递它们来解决你所遇到的问题。不知道什么会导致您的电子邮件不被发送。也许配置在你的'development.rb'上? –

回答

0

我计算出来的和更新的上面的代码。

class MessageMailer < ActionMailer::Base 
    default from: "Charactr <[email protected]>" 

    def message_notification(message) 
    @message = message 
    team = @message.recipient 

    mail(
     bcc: team.members.map(&:email), 
     subject: "#{message.sender.username} sent a message to the team." 
    ) 
    end 
end 
相关问题