您如何测试 Rails 邮件程序凭据是否有效? [英] how do you test that Rails mailer credentials are valid?

查看:62
本文介绍了您如何测试 Rails 邮件程序凭据是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 GMail 帐户发送的邮件程序,我想测试 ActionMailer 是否可以使用我提供的凭据登录 GMail 的 SMTP 服务器.对此进行测试的最佳方法是什么?

I've got a mailer sending through a GMail account, and I want to test that ActionMailer can actually log in to GMail's SMTP server with the credentials I've given it. What's the best way to test this?

推荐答案

这不是一个全栈解决方案,但您可以通过直接使用 Net::SMTP 来检查服务器身份验证是否正确进行.Rails 3 用于发送 ActionMailer 电子邮件的 Mail gem 使用类似这样的 Mail(使用您的 ActionMailer.smtp_settings):

It's not a full-stack solution, but you can check that the server authentication happens correctly by using Net::SMTP directly. The Mail gem that Rails 3 uses to send ActionMailer emails uses Mail like so (with your ActionMailer.smtp_settings):

  #line 96 of mail-2.2.7/lib/mail/network/delivery_methods/smtp.rb
  smtp = Net::SMTP.new(settings[:address], settings[:port])
  if settings[:enable_starttls_auto]
    smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto) 
  end

  smtp.start(settings[:domain], settings[:user_name], settings[:password],
   settings[:authentication]) do |smtp|
     smtp.sendmail(message, envelope_from, destinations)
     # @Mason: this line need not be included in your code. SMTP#start throws
     # a Net::SMTPAuthenticationError if the authentication was not successful.
     # So just putting this call to #start with an empty block in a method and
     # calling assert_no_raise Net::SMTPAuthenticationError should do the trick.
     # The empty block is necessary so that the connection gets closed.
     # Reference #{rubydir}/lib/ruby/1.8/net/smtp.rb for more info.
  end

看起来 ActionMailer::Base.smtp_settings 也可以访问:

Looks like ActionMailer::Base.smtp_settings is accessible too:

  settings = ActionMailer::Base.smtp_settings

把它放在你的测试中并注释掉上面提到的那一行应该有一个适合你的例子.

Putting that in your test and commenting out the line spoken of above should have a working example for you.

这篇关于您如何测试 Rails 邮件程序凭据是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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