Rails:在动作邮件中更改默认发件人

时间:2013-05-31 14:42:46

标签: ruby-on-rails ruby-on-rails-3.2 actionmailer

我在rails应用中使用动作邮件发送电子邮件。但它只允许一个默认发件人。这是我的UserMailer类:

class UserMailer < ActionMailer::Base
 default :from => "example@example.com"
 def welcome_email(user, order)
  @user = user
  @order = order
  mail(:to => user.email, :subject => "Your Order")
 end
 def signup_email(user)
  @user = user
  mail(:to => user.email, :subject => "Thank you.")
 end
 def invite_confirm(curuser,usemail,post)
  @greeting = "Hi"
  @user = curuser
  @post = post
  mail(:to => user.email, :subject => "Hello")
 end
end

我试过了:

class UserMailer < ActionMailer::Base
 def welcome_email(user, order)
@user = user
    @order = order
    mail(:to => user.email, :subject => "Your Order", :from => "abc@xyz.com")
 end
 def signup_email(user)
   @user = user
   mail(:to => user.email, :subject => "Thank you.", :from => "qwe@asd.com")
 end
 def invite_confirm(curuser,usemail,post)
  @greeting = "Hi"
  @user = curuser
  @post = post
  mail(:to => user.email, :subject => "Hello", :from => "zyx@asd.com")
 end
end

但它仍然是从“example@example.com”发送电子邮件

有没有办法为UserMailer类中编写的每个方法更改发件人?我应该在其他任何地方改变吗?

在config / environments / development.rb和config / environments / production.rb中我有:

 config.action_mailer.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => "587",
  :domain => "gmail.com",
  :authentication => "plain",
  :user_name => "example@example.com",
  :password => "example",
  :enable_starttls_auto => true 
 }

我想,我不应该在这里做任何改变。

4 个答案:

答案 0 :(得分:5)

您可以将其作为参数传递给mail方法:

def new_mail
  mail from: "example@example.com", to: "user@example.com"
end

答案 1 :(得分:2)

我认为你想发送包含for-each动作的三封不同电子邮件的邮件。因为您使用gmail,所以需要Sending mail from a different address

  

对于所有三种类型的电子邮件,没有一家供应商是最佳的;你可能   将使用几家供应商。

     

对于“公司电子邮件”,即向客户或个人发送个人电子邮件   商业伙伴,您可能会使用Gmail或Google Apps for Business。对于单个地址,您可以设置一个Gmail帐户   接收和send email from a different address。更倾向于,   你需要几个公司邮箱的电子邮件地址。为了那个原因,   使用Google Apps for Business。

Send Email with Rails

答案 2 :(得分:2)

我发现,使用smtp无法做到这一点。需要使用亚马逊SES,它允许多发送者支持。

答案 3 :(得分:0)

这就是我所使用的,它可以使“标题”有所不同。

class UserMailer < ActionMailer::Base
      default :from => '"example" <example@domain.com>'

      def send_signup_email(user)
        @user = user
        mail(to: @user.email, subject: 'example')
      end
    end