无法在 rails 中发送邮件?
Unable to Send mails in rails?
我正在尝试在用户注册时发送欢迎邮件,但一切正常
我在不同的项目中也使用了相同的代码,但邮件未发送:
我的应用程序日志:
User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."id"
= LIMIT [["id", 14], ["LIMIT", 1]] [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd]
Performing ActionMailer::DeliveryJob from Async(mailers) with
arguments: "UserMailer", "user_mailer", "deliver_now",
> [ActiveJob] [ActionMailer::DeliveryJob]
[3a0b9366-b50f-4934-8ca6-89303d1749bd] Rendering
user_mailer/user_mailer.html.erb within layouts/mailer [ActiveJob]
[ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd]
Rendered user_mailer/user_mailer.html.erb within layouts/mailer
(8.0ms) [ActiveJob] [ActionMailer::DeliveryJob]
[3a0b9366-b50f-4934-8ca6-89303d1749bd] UserMailer#user_mailer:
processed outbound mail in 79.8ms [ActiveJob]
[ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd]
Performed ActionMailer::DeliveryJob from Async(mailers) in 81.07ms
和我的 environment.rb 文件:
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
Rails.application.configure do
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
if Rails.env.development? || Rails.env.test?
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
elsif Rails.env.production?
config.action_mailer.default_url_options = {:host => ENV['HOST']}
end
config.action_mailer.smtp_settings = {
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => 'ravichem12@gmail.com',
:password => '********'
}
end
我的 development.rb 文件:
Rails.application.configure do
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.delivery_method = :smtp
end
我的 user_mailer.rb :
class UserMailer < ApplicationMailer
default from: 'support@lokavidya.com'
def user_mailer(user)
@user = user
if Rails.env.development? || Rails.env.test?
@url = 'http://localhost:3000/login'
elsif Rails.env.production?
@url = 'http://client-development-env.fr4s6529gr.us-west-2.elasticbeanstalk.com/login'
end
mail(to: @user.email, subject: 'Welcome to Lokavidya')
end
end
我的user_mailer.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1><%= Welcome to Lokavidya %> <%= @user.name %></h1>
<p>
<%= Welcome To Lokavidya %> <%= @user.email %>.<br>
</p>
<h3>
<% if Rails.env.development? || Rails.env.test? %>
<%#= verify_users_url(@user.confirmation_link) %>
<a href="http://localhost:3000/users/verify?link=<%= @user.confirmation_link%>">
<% elsif Rails.env.production? %>
<a href="http://client-development-env.fr4s6529gr.us-west-2.elasticbeanstalk.com/users/verify?link=<%= @user.confirmation_link%>">
<% end %>
<%= "Click The Above Link to Verify Your Account" %>
</a>
</h3>
<p><%= "Thanks For Joining Lokavidya" %></p>
</body>
</html>
和我的用户控制器方法:
def create
@user = User.new(user_params)
@user.confirmation_link = confirmation_link!
@user.uuid = unique_id!
if @user.password == @user.password_confirmation
if @user.save
UserMailer.user_mailer(@user).deliver_later
msg = {:status => 200, :uuid => @user.uuid, :message => "User created Succesfully" }
return render :json => msg
else
render json: @user.errors, status: :unprocessable_entity
end
end
end
请告诉我我做错了什么,我真的被困住了。
更改了以下行:
UserMailer.user_mailer(@user).deliver_later
到
UserMailer.user_mailer(@user).deliver
并检查邮件是否发送。
如果您想在后台发送邮件,则必须将 Gem delayed_job
或 sidekiq
添加到您的 Gem 文件中。
Smtp 设置:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxxxx@gmail.com",
:password => "xxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}
我正在尝试在用户注册时发送欢迎邮件,但一切正常 我在不同的项目中也使用了相同的代码,但邮件未发送:
我的应用程序日志:
User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT [["id", 14], ["LIMIT", 1]] [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd] Performing ActionMailer::DeliveryJob from Async(mailers) with arguments: "UserMailer", "user_mailer", "deliver_now",
> [ActiveJob] [ActionMailer::DeliveryJob]
[3a0b9366-b50f-4934-8ca6-89303d1749bd] Rendering user_mailer/user_mailer.html.erb within layouts/mailer [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd]
Rendered user_mailer/user_mailer.html.erb within layouts/mailer (8.0ms) [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd] UserMailer#user_mailer: processed outbound mail in 79.8ms [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd] Performed ActionMailer::DeliveryJob from Async(mailers) in 81.07ms
和我的 environment.rb 文件:
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
Rails.application.configure do
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
if Rails.env.development? || Rails.env.test?
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
elsif Rails.env.production?
config.action_mailer.default_url_options = {:host => ENV['HOST']}
end
config.action_mailer.smtp_settings = {
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => 'ravichem12@gmail.com',
:password => '********'
}
end
我的 development.rb 文件:
Rails.application.configure do
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.delivery_method = :smtp
end
我的 user_mailer.rb :
class UserMailer < ApplicationMailer
default from: 'support@lokavidya.com'
def user_mailer(user)
@user = user
if Rails.env.development? || Rails.env.test?
@url = 'http://localhost:3000/login'
elsif Rails.env.production?
@url = 'http://client-development-env.fr4s6529gr.us-west-2.elasticbeanstalk.com/login'
end
mail(to: @user.email, subject: 'Welcome to Lokavidya')
end
end
我的user_mailer.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1><%= Welcome to Lokavidya %> <%= @user.name %></h1>
<p>
<%= Welcome To Lokavidya %> <%= @user.email %>.<br>
</p>
<h3>
<% if Rails.env.development? || Rails.env.test? %>
<%#= verify_users_url(@user.confirmation_link) %>
<a href="http://localhost:3000/users/verify?link=<%= @user.confirmation_link%>">
<% elsif Rails.env.production? %>
<a href="http://client-development-env.fr4s6529gr.us-west-2.elasticbeanstalk.com/users/verify?link=<%= @user.confirmation_link%>">
<% end %>
<%= "Click The Above Link to Verify Your Account" %>
</a>
</h3>
<p><%= "Thanks For Joining Lokavidya" %></p>
</body>
</html>
和我的用户控制器方法:
def create
@user = User.new(user_params)
@user.confirmation_link = confirmation_link!
@user.uuid = unique_id!
if @user.password == @user.password_confirmation
if @user.save
UserMailer.user_mailer(@user).deliver_later
msg = {:status => 200, :uuid => @user.uuid, :message => "User created Succesfully" }
return render :json => msg
else
render json: @user.errors, status: :unprocessable_entity
end
end
end
请告诉我我做错了什么,我真的被困住了。
更改了以下行:
UserMailer.user_mailer(@user).deliver_later
到
UserMailer.user_mailer(@user).deliver
并检查邮件是否发送。
如果您想在后台发送邮件,则必须将 Gem delayed_job
或 sidekiq
添加到您的 Gem 文件中。
Smtp 设置:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxxxx@gmail.com",
:password => "xxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}