【问题标题】:Rails: change default sender in action mailerRails:更改动作邮件中的默认发件人
【发布时间】:2013-05-27 10:21:32
【问题描述】:

我正在使用我的 rails 应用程序中的 action mailer 发送电子邮件。但它只允许一个默认发件人。这是我的 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 
 }

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 嘿,我想发送来自不同用户的电子邮件。我更新了我的问题。你能帮忙吗?
    • 那你需要发几封邮件:)
    【解决方案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

    【讨论】:

      【解决方案3】:

      我发现,这不能使用 smtp 来完成。需要使用允许多发件人支持的亚马逊 SES。

      【讨论】:

        【解决方案4】:

        这是我使用的,它允许使“标题”不同。

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-03
          • 1970-01-01
          • 2013-03-08
          • 2013-08-30
          • 1970-01-01
          • 2010-09-08
          相关资源
          最近更新 更多