简体   繁体   English

具有2个域的Rails应用程序:在initializers / devise.rb和environment / development.rb中定义2个SMTP?

[英]Rails app with 2 domains: Define 2 SMTP's in initializers/devise.rb & environments/development.rb?

I have a Rails app that handles two domains. 我有一个处理两个域的Rails应用程序。 The app is set up like described in this blogpost . 该应用程序的设置如本博客文章中所述 In my controllers and views I am using request.domain to determine which app a visitor visits. 在我的控制器和视图中,我正在使用request.domain来确定访问者访问哪个应用程序。

When someone signs up for an account, Devise sends out a confirmation email. 当有人注册帐户时,Devise会发送一封确认电子邮件。 This process depends on the following lines: 此过程取决于以下几行:

# config/environments/development.rb

MyApp::Application.configure do
  ...
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => 'localhost',
    :user_name            => 'my@email.com',
    :password             => 'MyPassword',
    :authentication       => 'plain',
    :enable_starttls_auto => true
  }
  ...
end

# config/initializers/devise.rb

Devise.setup do |config|
  ...
  # ==> Mailer Configuration
  # Configure the e-mail address which will be shown in Devise::Mailer,
  # note that it will be overwritten if you use your own mailer class with default "from" parameter.
  config.mailer_sender = "my@email.com"
  ...
end

I need the :username in development.rb and the config.mailer_sender in devise.rb to depend on request.domain , because the user should of course receive an email from the domain he signs up for. 我需要development.rb:usernameconfig.mailer_sender中的devise.rb依赖于request.domain ,因为用户当然应该从他注册的域中接收电子邮件。

I use SiteSetting model to specify different mailboxes for each Site 我使用SiteSetting模型为每个Site指定不同的邮箱
SiteSetting contains configs for each mailboxes. SiteSetting包含每个邮箱的配置。
A code for mailer is: 邮件程序的代码是:

# app/mailers/invitation_mailer.rb
class InvitationMailer < ActionMailer::Base
  def invitation(invitation)
    @site = invitation.site
    @invitation = invitation
    email_settings = @site.email_settings.first

    mail(to: invitation.email,
         from: email_settings.try(:from) || 'notifier@example.com',
         subject: "Invitation",
         delivery_method_options: delivery_options(email_settings))
  end
  private

    def delivery_options(email_settings)
       @_delivery_options ||= 
         if email_settings
           {
             address:              email_settings.address,
             port:                 email_settings.port,
             domain:               email_settings.domain,
             user_name:            email_settings.user_name,
             password:             email_settings.password,
             authentication:       email_settings.authentication,
             enable_starttls_auto: email_settings.enable_startls_auto
           }
         else
           {
             address:              'smtp.google.com',
             port:                 587,
             domain:               'example.com',
             user_name:            'notifier@example.com',
             password:             'secret',
             authentication:       'plain',
             enable_starttls_auto: true
           }
         end
    end

end

Update 更新资料

You can pass username like invitation in this examle and then you can get acsess to username in your Mailer class (for example InvitationMailer ) 你可以通过username类似invitation在此examle,然后你可以得到acsess到usernameMailer类(例如InvitationMailer

# app/controller/users_controller.rb
class UsersController < ApplicationController
  def invitation
    invitation = Invitation.find(params[:invitation_id])
    InvitationMailer.invitation(invitation).deliver
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从config / initializers / devise.rb获取facebook app id - Get facebook app id from config/initializers/devise.rb 找不到config / environments / development.rb命令 - config/environments/development.rb command not found 没有development.rb的情况下如何运行Rails应用程序 - how is it possible to run a rails app without development.rb Devise.rb确认无法正常工作 - Devise.rb Confirmable Not Working rails生成rspec:install config / environments / development.rb:1:在`<top(required)>':undefined method`configure' - rails generate rspec:install config/environments/development.rb:1:in `<top (required)>': undefined method `configure' 迁移 Rails 6.0.34 -&gt; 6.1 后,我无法在 config/environments/development.rb 中使用 model - After migration Rails 6.0.34 -> 6.1 I can't use model inside the config/environments/development.rb Rails 3在config / environments / development.rb中从lib加载类的问题 - Rails 3 problem loading classes from lib in config/environments/development.rb 在Rails项目中,我们将所有常量定义放在config / environment.rb中,将更多特定定义放在config / environments / development.rb中? - In a Rails project, we put all constants definition in config/environment.rb and more particular ones in config/environments/development.rb? git pull说config / environments / development.rb未提交 - git pull says config/environments/development.rb is not committed 将变量添加到config / development.rb rails引擎 - Adding Variables to config/development.rb rails engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM