【问题标题】:Action Mailer not working in Dev environmentAction Mailer 在开发环境中不起作用
【发布时间】:2015-06-16 10:49:07
【问题描述】:

我查看了此处的其他 Action Mailer 答案,但似乎没有一个解决方案可以解决问题。

我正在尝试实现here 显示的 Action Mailer,但它根本不适合我。我正在使用 FB 登录来收集用户信息,然后使用 find_or_create 选项来更新用户。

user = User.find_or_create_by(fb_id: profile_hash["id"])
  user.update(sanitized_hash)
  user.save

  log_in user

  redirect_to current_user_profession(user)

在用户控制器中,我在保存方法上调用邮件方法。但是由于某种原因,一旦保存了用户实例,它就不会发送电子邮件。

def create
@user = User.new(user_params)

@image = Image.new

respond_to do |format|
  if @user.save
    UserMailer.welcome_email(@user).deliver
    log_in @user
    format.html { redirect_to "/talent", 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

结束

我确信我的所有编码都是正确的,但由于某种原因它没有发送欢迎电子邮件。据我了解,我正在使用我的个人 gmail 帐户凭据以及我的用户名和密码,这对我来说似乎很奇怪,但我还是这样做了。

这是我的开发.rb

config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = {:host => 'localhost:3000'}

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV['GMAIL_USER_NAME'],
password: ENV['GMAIL_PASSWORD']
}

要发送的实际电子邮件

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <link type="text/css" rel="stylesheet" href="styles/style.css"/>
</head>
<body>
  <h1>Hey <%= @user.name  %>,</h1>
    <p>Welcome to the site!</p>
    <p>To login to the site just click on this link <%= @url %></p>
</body>
</html>

还有 user_mailer.rb 文件。

class UserMailer < ApplicationMailer
  default from: "support@mystyleblox.com"

  def welcome_email(user)
    @user = user
    @url = 'http://www.mystyleblox.com'
    mail(to:@user.email, subject:'test welcome email')
  end
end

任何帮助都将不胜感激,如果有人可以解释 development.rb 文件以及 gmail 访问需要什么,这是我的个人帐户,还是我必须与我的个人帐户分开做的事情。提前致谢。

【问题讨论】:

  • 定义“不工作”?我猜邮件没有发送。但是您看到了什么样的错误消息?
  • 这就是我什至无法在终端中看到任何错误的原因。但是是的,如果不工作,它不会将电子邮件发送给新用户。

标签: ruby-on-rails actionmailer


【解决方案1】:

为了消除问题的可能根源,可能值得对您的 gmail 用户名和密码进行硬编码 - 再次作为测试 - 以便加载 ENV 变量时不会出现问题。

我使用 Rails.application.secrets.domain_name 来隐藏我的超级机密域,但在我到达那里之前,我通常使用硬编码值等。我注意到你和我的本地设置之间的一件事是域。我的域名不是gmail.com;相反,它是我的应用程序的实际域。

在我的本地应用程序中,我有以下适用于本地服务器发送电子邮件的内容。

config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: 'my domain',
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: 'my email address',
  password: 'mypass'
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true

只是一个想法......

【讨论】:

  • 好的,我会试一试。谢谢,我会告诉你它是否有效
  • 没问题!确保在您的 development.rb 文件中也将 perform_deliveries 设置为 true ...有点问题 :)
  • 是的,仍然无法正常工作。还仔细检查了我在 Google 中的密码,但在创建用户后它仍然没有向用户发送电子邮件。
  • 是的,我在第一次试用中没有 perform_deliveries,一旦我看到你的答案,我就将它包含在内但仍然没有发送电子邮件
  • 我可能刚刚经历了一个完全的面部护理时刻。更改 development.rb 文件后是否重新启动 Rails 服务器?它需要重新启动,以便获取新的 perform_deliveries 值 (true)。
猜你喜欢
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 2012-09-29
相关资源
最近更新 更多