代码之家  ›  专栏  ›  技术社区  ›  Addsy

在开发环境中使用Rails 3发送邮件

  •  54
  • Addsy  · 技术社区  · 15 年前

    我肯定这已经被问过无数次了,但我找不到任何对我有用的东西,所以我又问了一次!

    我只需要一种在Rails3中使用ActionMailer发送电子邮件的方法。我已经学习了很多教程,包括关于新ActionMailer的Railscasts教程,我可以看到正在生成的邮件,但我没有收到它们。

    我尝试了许多不同的方法,但它们通常相当于配置以下设置

    ActionMailer::Base.delivery_method = :smtp
    
    ActionMailer::Base.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => "587",
      :domain               => "gmail.com",
      :user_name            => "xxx@gmail.com",
      :password             => "yyy",
      :authentication       => "plain",
      :enable_starttls_auto => true
    }
    

    我已经在我的config/environment.rb、config/environments/development.rb中尝试过上述代码(当然还有有效的gmail详细信息),目前在自己的initializer config/initializers/setup.rb中使用过。

    我还尝试了一些不同的SMTP服务器,包括gmail和sendgrid,相应地调整了SMTP设置,但仍然没有什么。我可以在终端上看到邮件和开发日志,就这样。

    是否有人知道我可能错过了其他需要设置为ActionMailer才能工作的gotcha?如果失败了,有一种方法可以获得更多关于邮件为什么不被发送的信息?我有

    config.action_mailer.raise_delivery_errors = true
    

    在config/development.rb中设置,但开发日志仍然显示与在终端中看到的相同。

    值得一提的是,我正在一台Ubuntu10.04笔记本电脑上开发,以防需要任何特定的设置。

    多谢

    8 回复  |  直到 8 年前
        1
  •  55
  •   Addsy    15 年前

    好吧,我已经解决了这个问题,但我不知道为什么这个方法和其他方法不起作用。

    解决方案是在config/initializers/setup\mail.rb中创建一个包含以下内容的初始化器

    if Rails.env != 'test'
      email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
      ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
    end
    

    然后我添加了config/email.yml,其中包含开发和生产电子邮件帐户的详细信息

    development:
      :address: smtp.gmail.com
      :port: 587
      :authentication: plain
      :user_name: xxx
      :password: yyy
      :enable_starttls_auto: true
    production:
      :address: smtp.gmail.com
      :port: 587
      :authentication: plain
      :user_name: xxx
      :password: yyy
      :enable_starttls_auto: true
    

    就像我说的,不知道为什么,但这似乎起到了作用。谢谢你的指点

        2
  •  27
  •   nathanvda    15 年前

    我有以下内容 config/environments/development.rb

    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.perform_deliveries = true
    

    实际邮件配置, config.actionmailer.* 我已经放进去了 config\application.rb .

    希望这有帮助:)

        3
  •  6
  •   R Milushev    11 年前

    尝试使用“sendmail”而不是“smtp”。

    ActionMailer::Base.delivery_method = :sendmail
    ActionMailer::Base.sendmail_settings = {
      :address              => "smtp.gmail.com",
      :port                 => "587",
      :domain               => "gmail.com",
      :user_name            => "xxx@gmail.com",
      :password             => "yyy",
      :authentication       => "plain",
      :enable_starttls_auto => true
    }
    
        4
  •  3
  •   IAmNaN    15 年前

    三件事。

    首先,端口是一个整数,不需要引号,如第一个示例所示。(但我认为一根绳子还是可以用的。)

    第二,每次修改这个(或任何)初始值设定项文件时,不要忘记重新启动服务器。这可以解释为什么添加以下内容后没有看到错误:

    config.action_mailer.raise_delivery_errors = true

    如果没有这个错误信息,很难确定邮件为什么不发送,但现在是。一种可能是在密码周围使用双引号。如果您使用的是一个强密码,并且密码中有一个未被转义的令牌,那么它可能已经被重新解释了。(即 "P@ssw\0rd" 将成为 P@ssrd )出于这个原因,我总是在代码中使用单引号,除非我特别需要语法上的制糖。

    最后, enable_starttls_auto: true 是默认的和不必要的。

        5
  •  1
  •   Nickle    12 年前

    只需将所有配置设置为: 配置/环境/开发.rb

    我的意思是

    ActionMailer::Base.delivery_method = :smtp
    
    ActionMailer::Base.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => "587",
      :domain               => "gmail.com",
      :user_name            => "xxx@gmail.com",
      :password             => "yyy",
      :authentication       => "plain",
      :enable_starttls_auto => true
    }
    

    和

    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.perform_deliveries = true
    

    这对我很有用。

        6
  •  1
  •   Jerome    11 年前

    ActionMailer::Base.delivery_method = :sendmail
    和
    config.action_mailer.perform_deliveries = true

    是两个必要的步骤使我克服了这个问题吗?

        7
  •  0
  •   seyyah    13 年前

    此外,您的gmail用户名没有别名。

    裁判: https://support.google.com/mail/answer/12096?hl=en

        8
  •  0
  •   Varus Septimus    8 年前

    我的两个硬币价值:

    我在Rails 5.1上也有同样的症状:什么都没有发生,我的设置 development.rb 文件被完全忽略…

    然后我记得重新启动机器! (神奇地解决了这个问题)

    之前的一些评论指出了这一点。

    然而,这个问题很棘手,因为您不希望发生这种行为。在我的视图中,默认注释位于 开发RB 在这方面具有误导性:

    # In the development environment your application's code is reloaded on
    # every request. This slows down response time but is perfect for development
    # since *you don't have to restart the web server when you make code changes*.