【问题标题】:Send emails via gmail from different accounts通过 gmail 从不同帐户发送电子邮件
【发布时间】:2008-10-28 06:37:52
【问题描述】:

所以我有 action_mailer_optional_tls (http://svn.douglasfshearer.com/rails/plugins/action_mailer_optional_tls) 这在我的环境中。rb

ActionMailer::Base.server_settings = {
  :tls => true,
  :address => "smtp.gmail.com",
  :port => "587",
  :domain => "www.somedomain.com",
  :authentication => :plain,
  :user_name => "someusername",
  :password => "somepassword"
}

但是现在如果我想从不同的电子邮件帐户发送电子邮件怎么办? 如何即时覆盖用户名和密码字段?

我正在寻找一种允许在帐户之间动态切换的解决方案。以以下场景为例: 10 个“管理员”可以向我们的客户发送通知。每个人都有自己的 gmail 帐户,当他们使用他们的帐户填写站点上的表格 rails connect 并发送邮件时。

提前致谢!

阿里

【问题讨论】:

    标签: ruby-on-rails email gmail


    【解决方案1】:

    我现在无法验证这是否有效,但您应该尝试即时修改这些设置。即在发送电子邮件之前从用户帐户中设置用户名/密码。您甚至可以在控制器上设置一个前置过滤器来加载该信息。

    before_filter :load_email_settings
    
    def load_email_settings
      ActionMailer::Base.server_settings.merge!(:user_name => current_user.email, :password => current_user.email_password)
    end
    
    def current_user
       @current_user ||= User.find(session[:user_id])
    end
    

    请注意,将用户电子邮件密码存储为纯文本非常危险,我不知道是否有任何方法可以使用 Google 帐户的 third party authentication 方案来做您想做的事情,但您可能想检查一下。

    【讨论】:

      【解决方案2】:

      如果您想为目标收件人的回复使用不同的电子邮件地址, 您可以为 SMTP 协议指定 Reply-To: otheremail@example.com 并仍然使用现有的 google smtp 服务帐户。

      但是,您需要转到 Gmail 设置以将 otheremail@example.com 添加到“发送电子邮件为”列表中,这还需要您通过发送指向该收件箱的链接来确认 otheremail@example.com 是您的。

      【讨论】:

        【解决方案3】:

        从 Rails 2.3 开始,action_mailer_optional_tls 插件不是必需的。相反,您应该将以下内容放入您的environment.rb

        config.action_mailer.delivery_method = :smtp 
        config.action_mailer.smtp_settings = { 
          :enable_starttls_auto => :true, 
          :address => "smtp.gmail.com", 
          :port => 587, 
          :domain => "mydomain.example.com", 
          :authentication => :plain, 
          :user_name => "myaddress@example.com", 
          :password => "xxxxxxx", 
          :tls => :true 
        } 
        config.action_mailer.perform_deliveries = :true 
        config.action_mailer.raise_delivery_errors = :true 
        config.action_mailer.default_charset = "utf-8"
        

        【讨论】:

        • 答案是从douglasfshearer.com/blog/…Ivan 的评论中复制的
        • 这仍然没有回答主要问题。我认为应该在实际的邮件方法中更改 smtp_settings,但由于这些不是直接调用的,我不确定。有人可以确认吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 2016-05-28
        • 2011-12-15
        相关资源
        最近更新 更多