15

ユーザーが登録するとメールを送信するアプリケーションを作成しようとしています。

GmailのSMTP設定をconfig/application.rbファイルに入力すると、メール機能は次のようになります。

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

ログを見ると、メールが送信されたと表示されていますが、メールをまったく受信していません...

また、メール配信関数を呼び出すとEmails.signed(@user).deliver、フォームページはリダイレクトされませんが、次のいずれかのメール送信コードをコメントアウトすると機能します。

Emails.signed(@user).deliver

また

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

ありがとう :)

編集:development.rb

App::Application.configure do
  # Settings specified here will take precedence over those in config/environment.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 webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

end
4

4 に答える 4

35

やや遅いですが、それでも、これにより、誰かが数時間頭を叩くのを節約できるかもしれません。これはおそらくGmailからのメールの送信にのみ関係します。まず、状況のデバッグを支援するために、development.rbの次の行をtrueに設定します(開発モードになっていると仮定します)。

config.action_mailer.raise_delivery_errors = true

これにより、ActionMailerはエラーを黙って無視しなくなります。私がそれをしたとき、私はgmailが私のユーザー名とパスワードを拒否していることに気づきました。次に、すべてのAction Mailer構成ディレクティブを配置した構成ファイルに移動し(私にとってはdevelopment.rbにあり、おそらく配置するのに適した場所があります)、:user_nameが「admin」ではなく「admin」に設定されていることに気付きました。 「admin@thedomain.com」。それを変更すると問題は解決しました。これがdevelopment.rbの私の修正された部分です:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'thedomain.com',
  :user_name            => 'admin@thedomain.com',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

参照:

http://forums.pragprog.com/forums/43/topics/541 http://edgeguides.rubyonrails.org/action_mailer_basics.html

于 2010-11-23T16:29:02.667 に答える
7

もう1つ忘れてはならないのは、環境構成ファイルに変更を加えた後、アプリケーションを再起動する必要があるということです。乗客を使用する場合、これはすぐに見逃される可能性があります:)

これが、ActionMailerがエラーを表示せずにメールを送信したくないという私の「問題」を解決したものです。

于 2011-02-09T15:38:19.840 に答える
6

ここに書かれていることは私を助けませんでした。

私はRails3.2.8を使用していますが、これを理解するために数時間を費やしましたが、最終的には非常に簡単でした。メソッド呼び出しによって返されるオブジェクトを呼び出す.deliver()のを忘れました。Mail::Messagemail(:to => @user.email, :subject => 'Welcome to the Site')

公式のRoRチュートリアルで指定されているようにすべてを残してください。

つまり、開発/本番環境のファイルで、次のようなセクションを作成します。

  # mailer
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'gmail.com',
      user_name:            '<username>@gmail.com',
      password:             '<password>',
      authentication:       'plain',
      enable_starttls_auto: true
  }

次に、ActionMailer :: Baseをサブクラス化します。たとえば、次のようになります。

class InfoMailer < ActionMailer::Base
  default from: "<username>@gmail.com"

  def welcome_user_and_send_password(user, password)
    default_url_options = self.default_url_options()


    @user = user
    @password = password
    @url = default_url_options[:host]
    mail(:to => @user.email, :subject => 'Welcome to the Site').deliver()
  end

end

その後InfoMailer、クラスメソッドのようにコードからこのメソッドを簡単に使用できます。

InfoMailer.welcome_user_and_send_password(user, password)
于 2014-05-09T23:34:02.030 に答える
0

テスト環境を使用している場合は、environments/test.rbの次のコード行を必ずコメントアウトしてください。

config.action_mailer.delivery_method = :test
于 2015-03-15T00:53:33.673 に答える