无法将收件人电子邮件传递给rails邮件程序

时间:2014-03-12 17:42:35

标签: ruby-on-rails email actionmailer

现在团队中的任何团队成员都可以向团队发送消息。当他们发送消息时,它只是在团队新闻源中发布。我正在编写一个故事,通过电子邮件向团队发送消息,告知团队已向团队发送了消息。

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

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


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 <admin@charactr.co>"

  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

1 个答案:

答案 0 :(得分:0)

我想出来并更新了上面的代码。

class MessageMailer < ActionMailer::Base
  default from: "Charactr <admin@charactr.co>"

  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