在我的rails应用程序中,当用户创建帖子时,邮件会发送给创建帖子的用户。我已经尝试了“delayed_job_active_record”gem用于后台邮件操作。邮件发送成功。在那之后,我只是尝试通过开信刀使用sidekiq做同样的动作。事件发生,并且开信刀也显示邮件内容。在那之后,而不是开信刀使用谷歌smtp设置。甚至,sidekiq打开开信刀而不发送电子邮件。所以,我卸载了开信刀。但是这会导致像这样的错误
没有这样的文件或目录开信刀
这是我的Posts_controller :::
def create
@post = @topic.posts.new(post_params)
PostJob.perform_later(current_user, @post.name)
end
这是我的post_job文件:
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
这是我的开发.rb:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: '[email protected]'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '[email protected]',
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向用户发送电子邮件的方法。
尝试重启服务器!!如果没有任何反应意味着尝试重新启动系统。
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
}
我正在更新我的答案,因为我正在测试此问题并且必须进行以下更改,以便我的电子邮件也可以通过使用sidekiq的Letter开启者看到。
if Rails.env.development?
require 'sidekiq/testing'
Sidekiq::Testing.inline!
end
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
(这应该无关紧要)现在我可以轻松点击网址/letter_opener
,所以看看我的电子邮件存储在本地。
当我在使用c9编辑器时,它甚至可以使用任何localhost名称。
希望这可以帮助