在行动邮件中更改默认发件人

在行动邮件中更改默认发件人

本文介绍了Rails:在行动邮件中更改默认发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am sending email using action mailer in my rails app. But it allows only one default sender. This is my UserMailer class:

class UserMailer < ActionMailer::Base
 default :from => "[email protected]"
 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 => "[email protected]")
 end
 def signup_email(user)
   @user = user
   mail(:to => user.email, :subject => "Thank you.", :from => "[email protected]")
 end
 def invite_confirm(curuser,usemail,post)
  @greeting = "Hi"
  @user = curuser
  @post = post
  mail(:to => user.email, :subject => "Hello", :from => "[email protected]")
 end
end

但是它仍然从 [email protected]发送电子邮件。

But still it is sending email from "[email protected]"

是否有任何方法可以更改UserMailer类中编写的每个方法的发件人?我应该在其他地方进行更改吗?

Is there any way to change sender for each method written in UserMailer class? Am i supposed to change anywhere else?

在config / environments / development.rb和config / environments / production.rb中,我有以下内容:

In config/environments/development.rb and config/environments/production.rb i have this:

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

我想,我不应该在这里更改任何内容。

I guess, i should not change anything here.

推荐答案

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

You can pass it as a parameter to the mail method:

def new_mail
  mail from: "[email protected]", to: "[email protected]"
end

这篇关于Rails:在行动邮件中更改默认发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:07