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

use*_*724 5 ruby-on-rails actionmailer ruby-on-rails-3.2

我在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
Run Code Online (Sandbox Code Playgroud)

我试过这个:

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
Run Code Online (Sandbox Code Playgroud)

但它仍然是从"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 
 }
Run Code Online (Sandbox Code Playgroud)

我猜,我不应该在这里改变任何东西.

mih*_*hai 5

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

def new_mail
  mail from: "example@example.com", to: "user@example.com"
end
Run Code Online (Sandbox Code Playgroud)