Rails:在行动邮件中更改默认发件人 [英] Rails: change default sender in action mailer

查看:78
本文介绍了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 => "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发送电子邮件。

But still it is sending email from "example@example.com"

是否有任何方法可以更改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 => "example@example.com",
  :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: "example@example.com", to: "user@example.com"
end

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆