Sidekiq打开开信刀,而不发送邮件

时间:2019-03-29 13:14:19

标签: ruby-on-rails ruby-on-rails-5 sidekiq

在我的Rails应用程序中,当用户创建帖子时,发送给创建帖子的用户的邮件。我尝试了“ delayed_job_active_record” gem进行后台邮件操作。邮件发送成功。之后,我只是尝试通过开信刀使用sidekiq进行相同的操作。事件发生,开信刀也显示邮件内容。之后,请使用Google smtp设置代替开信刀。甚至,sidekiq会在不发送电子邮件的情况下打开开信刀。因此,我卸载了开信刀。但这会导致这样的错误

  

没有这样的文件或目录开信刀

这是我的Posts_controller :::

def create
  @post = @topic.posts.new(post_params)
  PostJob.perform_later(current_user, @post.name)
end

这是我的求职文件:

class PostJob < ApplicationJob
  queue_as :default

  def perform(user, post_title)
    PostMailer.announce_email(user, post_title).deliver
  end       
end

这是我的Post_mailer文件:

class PostMailer < ApplicationMailer
  def announce_email(user, post_title)
    @user = user
    @post_title = post_title
    mail(to: @user.email, subject: 'hurrah!!! Post created                                 Successfully')
  end
end

这是我的development.rb:

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from:  'aaaaaaaa@gmail.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'gmail.com',
  user_name: 'xxxxxxx@gmail.com',
  password: 'yyyyyyy',
  authentication: 'plain',
  enable_starttls_auto: true
}

这是我的Gemfile:

source 'https://rubygems.org'
git_source(:github) {|repo| "https://github.com/#{repo}.git"}
ruby '2.3.3'
gem 'bootstrap', '~> 4.2.1'
gem 'bullet', group: 'development'
gem 'cancancan', '~> 2.0'
gem 'coffee-rails', '~> 4.2'
gem 'devise'
gem 'jbuilder', '~> 2.5'
gem 'jquery-rails'
gem 'jquery-turbolinks'
gem 'local_time'
gem 'paperclip', '~> 5.0.0.beta1'
gem 'puma', '~> 3.11'
gem 'rails', '~> 5.2.2'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks', '~> 5'
gem 'uglifier', '>= 1.3.0'
gem 'will_paginate', '>= 3.1.6'
group :production do
  gem 'pg', '~> 0.21'
end
group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails', '~> 3.8'
  gem 'sqlite3', '1.3.13'
  gem 'rack-mini-profiler'
  # gem 'letter_opener'
end
group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'launchy'
end
group :test do
  gem 'capybara'
  gem 'factory_bot_rails'
  gem 'shoulda-matchers', '~> 4.0.0.rc1'
  gem 'database_cleaner'
  gem 'selenium-webdriver'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'rails-controller-testing'
gem 'strong_migrations'
gem "image_magick"
gem "mini_magick"
gem 'rails_warden'
gem 'activesupport', '~> 5.2', '>= 5.2.2'
gem 'remotipart', :git => 'https://github.com/westonganger/remotipart', :branch => 'fix_escape_javascript'
gem 'will_paginate-bootstrap4'
gem 'best_in_place'
gem 'sidekiq'
gem 'daemon', '~> 1.2'
gem 'delayed_job_active_record'
gem 'delayed_job_web'
gem 'sinatra', '>= 1.3.0', :require => nil

如果我有任何错误。告诉其他使用sidekiq向用户发送电子邮件的方法。

3 个答案:

答案 0 :(得分:1)

尝试重新启动服务器!!如果什么都没有发生,则意味着尝试重新启动系统。

答案 1 :(得分:0)

我在测试此问题时正在更新答案,因此必须进行以下更改,以便开信者也可以使用sidekiq看到我的电子邮件。

  1. 在开发人员上禁用sidekiq

    if Rails.env.development? require 'sidekiq/testing' Sidekiq::Testing.inline! end

  2. 在development.rb中添加以下设置代码

    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } ###ADDED FOR LETTER OPENER AND DEVISE MAILERS config.action_mailer.delivery_method = :letter_opener Rails.application.routes.default_url_options[:host] = 'localhost:3000'

我还删除了config.action_mailer.perform_deliveries = true行(这无关紧要),现在我可以轻松访问URL /letter_opener,因此可以查看本地存储的电子邮件。

当我在c9编辑器上工作时,即使使用任何本地主机名称,它都可以工作。

希望这会有所帮助

答案 2 :(得分:0)

letter_opener是开发环境中用于“模仿”电子邮件发送过程的工具。但是,应该在生产环境中设置SMTP。所以:

development.rb:

  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false

  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.perform_deliveries = true

production.rb:

  config.action_mailer.default_url_options = { host: 'https://yourwebsite.com' }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    domain: 'mail.google.com',
    port: 587,
    user_name: SOME_SECRET_USERNAME,
    password: SOME_SECRET_PASSWORD,
    authentication: :plain,
    enable_starttls_auto: true
  }