Rails:ActionMailer 的运行时配置? [英] Rails: Runtime configuration of ActionMailer?

查看:45
本文介绍了Rails:ActionMailer 的运行时配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 Gmail 从我的应用发送少量电子邮件.现在,SMTP 设置将在运行时确定(即:从数据库中),这可以做到吗?

I would like to send a small amount of email from my app through Gmail. Now, the SMTP settings will be determined at runtime (ie: from the db), can this be done?

--- 编辑---

我可以在类的方法之一中设置 ActionMailer 子类(名为 Notifier)smtp 设置.这样我就可以设置动态发送电子邮件的用户名和密码.唯一的事情是您必须设置所有 smtp_settings.是否可以只设置用户名 &类方法中的密码设置?

I can set the ActionMailer subclass (named Notifier) smtp settings in one of the class' methods. This way I can set the username and password for sending the email dynamically. The only thing is that you have to set ALL the smtp_settings. Is it possible to set just the username & password settings in the class method?

这是我现在正在使用的代码,它正在发送:

This is the code I'm using right now, it is sending:

class Notifier < ActionMailer::Base
  def call(user)
    Notifier.smtp_settings = { 
      :enable_starttls_auto => true, 
      :address => "smtp.gmail.com",
      :port => "587",
      :domain => "mydomain.com",
      :authentication => :plain,
      :user_name => "fabian@mydomain.com",
      :password => "password"
    }

    recipients user.email
    subject    "Test test"
    body       "Test"
  end
end

我只想在这里设置用户名和密码.

I would like to just set the username and pw here.

推荐答案

(Rails 3)

因为我这样称呼邮件程序:

Since I call mailer like this:

CustomerMailer.customer_auto_inform(@form).deliver

在 CustomerMailer 类中,我有私有方法:

In CustomerMailer class I have private method:

def init_email_account(shop_mail)
  ActionMailer::Base.raise_delivery_errors = true
  ActionMailer::Base.smtp_settings = {
    :address              => shop_mail.address,
    :port                 => shop_mail.port,
    :domain               => shop_mail.domain,
    :user_name            => shop_mail.user_name,
    :password             => shop_mail.password,
    :authentication       => shop_mail.authentication.name,
    :enable_starttls_auto => shop_mail.enable_starttls_auto
  }
end

在调用发送电子邮件的 ma​​il() 之前,您需要调用私有方法 init_email_account 以从数据库填充 smtp_settings.shop_mail 是存储有关邮件帐户设置的数据的模型.

Before calling mail() which sends email you need to call private method init_email_account to populate smtp_settings from database. shop_mail is model which stores the data about mail account settings.

HTH

这篇关于Rails:ActionMailer 的运行时配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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