Ruby on rails 操作邮件程序在开发环境中不工作
我在这里查看了Action Mailer的其他答案,没有一个解决方案能够解决这个问题 我正在尝试实现Mailer显示的操作,但它根本不适合我。我使用FB登录来收集用户信息,然后使用find_或create选项来更新用户Ruby on rails 操作邮件程序在开发环境中不工作,ruby-on-rails,actionmailer,Ruby On Rails,Actionmailer,我在这里查看了Action Mailer的其他答案,没有一个解决方案能够解决这个问题 我正在尝试实现Mailer显示的操作,但它根本不适合我。我使用FB登录来收集用户信息,然后使用find_或create选项来更新用户 user = User.find_or_create_by(fb_id: profile_hash["id"]) user.update(sanitized_hash) user.save log_in user redirect_to current_use
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)
在用户控制器中,我调用save方法上的mail方法。但由于某些原因,一旦用户实例被保存,它就不会发送电子邮件
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帐户凭证,还有我的用户名和密码,这对我来说似乎很奇怪,但我还是这样做了
这是我的发展
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>
嘿
欢迎来到这个网站
要登录该站点,只需单击此链接
以及用户_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
class UserMailer
如果有人能解释development.rb文件以及gmail访问需要什么,我将不胜感激。这是我的个人帐户,还是我必须与我的个人帐户分开做的事情。提前谢谢 为了消除一个可能的问题来源,可能需要对你的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\u deliveries
设置为true
。。。这类事情:)是的,仍然不起作用。也在谷歌上仔细检查了我的密码,但在用户创建后,它仍然没有向用户发送电子邮件。是的,我在第一次试用中没有执行交付,当我看到你的答案时,我将其包括在内,但仍然没有发送电子邮件。我可能刚刚经历了一个全面的facepalm时刻。更改development.rb文件后是否重新启动了Rails服务器?它需要重新启动,以便拾取新的perform\u deliveries
值(true
)。