发送电子邮件不适用于 rails
sending email not working with rails
我构建了一个示例应用程序来尝试使邮件程序正常工作。该应用托管在 https://blooming-brushlands-80122.herokuapp.com,
源代码是here。这是我与操作邮件相关的配置:
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply@example.com'}
并用于发送邮件
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
# Sends email to user when user is created.
ExampleMailer.sample_email(@user).deliver
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
该应用程序不向用户发送电子邮件。
有人知道为什么吗?
此答案假设您使用的是 Heroku 提供的免费 sendgrid 插件,因为我看到您使用的是 sendmail。要添加它,只需登录 heroku 并将其添加为免费资源。由于您没有指定是尝试在本地发送电子邮件还是在生产环境中发送电子邮件,因此我为两者都添加了配置。在您的 config/environment/development.rb 文件中,您应该执行以下操作以从本地发送:
config.action_mailer.delivery_method = :smtp #Yours used sendmail, change to this.
config.action_mailer.perform_deliveries = true #Needed if this is dev env. file
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['KUNZIG_SENDGRID_USERNAME'],
:password => ENV['KUNZIG_SENDGRID_PASSWORD'],
:domain => 'localhost:3000',
:enable_starttls_auto => true
}
显然用你自己的环境替换用户名和密码。 Heroku 分配的变量。 Heroku 的配置。变量可以在设置选项卡下找到。只需单击 "reveal" 按钮。
您的 environment/production.rb 文件类似于:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'kunzig.herokuapp.com',
:enable_starttls_auto => true
}
请注意,唯一的变化是域名和环境变量,因为当您将 Sengrid 添加为插件时,这些是 Heroku 自动设置的。此外,将控制器中的 deliver
调用更改为 deliver_now
,这样我们就知道这不是您的后台工作人员配置的问题。
我构建了一个示例应用程序来尝试使邮件程序正常工作。该应用托管在 https://blooming-brushlands-80122.herokuapp.com, 源代码是here。这是我与操作邮件相关的配置:
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply@example.com'}
并用于发送邮件
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
# Sends email to user when user is created.
ExampleMailer.sample_email(@user).deliver
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
该应用程序不向用户发送电子邮件。
有人知道为什么吗?
此答案假设您使用的是 Heroku 提供的免费 sendgrid 插件,因为我看到您使用的是 sendmail。要添加它,只需登录 heroku 并将其添加为免费资源。由于您没有指定是尝试在本地发送电子邮件还是在生产环境中发送电子邮件,因此我为两者都添加了配置。在您的 config/environment/development.rb 文件中,您应该执行以下操作以从本地发送:
config.action_mailer.delivery_method = :smtp #Yours used sendmail, change to this.
config.action_mailer.perform_deliveries = true #Needed if this is dev env. file
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['KUNZIG_SENDGRID_USERNAME'],
:password => ENV['KUNZIG_SENDGRID_PASSWORD'],
:domain => 'localhost:3000',
:enable_starttls_auto => true
}
显然用你自己的环境替换用户名和密码。 Heroku 分配的变量。 Heroku 的配置。变量可以在设置选项卡下找到。只需单击 "reveal" 按钮。
您的 environment/production.rb 文件类似于:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'kunzig.herokuapp.com',
:enable_starttls_auto => true
}
请注意,唯一的变化是域名和环境变量,因为当您将 Sengrid 添加为插件时,这些是 Heroku 自动设置的。此外,将控制器中的 deliver
调用更改为 deliver_now
,这样我们就知道这不是您的后台工作人员配置的问题。