Ami*_*mit 15 ruby ruby-on-rails actionmailer
我正在尝试创建一个应用程序,在用户注册时发送电子邮件.
我在config/application.rb文件中输入了gmail的smtp设置,邮件功能看起来像
mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")
Run Code Online (Sandbox Code Playgroud)
现在,当我看到日志时,我看到它说邮件已被发送,但我从来没有收到任何邮件......
此外,当我调用邮件传递功能时Emails.signed(@user).deliver
,表单页面不会重定向,但如果我注释掉电子邮件发送代码,它就可以工作
Emails.signed(@user).deliver
Run Code Online (Sandbox Code Playgroud)
要么
mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
编辑:development.rb
App::Application.configure do
# Settings specified here will take precedence over those in config/environment.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
end
Run Code Online (Sandbox Code Playgroud)
Ami*_*itA 35
有点晚了,但是这可能会让人节省几个小时的敲击声.这可能只与从Gmail发送电子邮件有关.首先,为了帮助调试情况,请将development.rb中的以下行设置为true(假设您处于开发模式):
config.action_mailer.raise_delivery_errors = true
Run Code Online (Sandbox Code Playgroud)
这将使ActionMailer不会默默地忽略错误.当我这样做时,我意识到gmail拒绝我的用户名和密码.然后我去了我的配置文件,在那里我放了所有的Action Mailer配置指令(对我而言,它在development.rb中,可能有一个更好的地方放置它),并注意到:user_name设置为"admin"而不是"admin@thedomain.com".改变它解决了这个问题.这是我更正的开发部分.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'thedomain.com',
:user_name => 'admin@thedomain.com',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
Run Code Online (Sandbox Code Playgroud)
参考文献:
http://forums.pragprog.com/forums/43/topics/541 http://edgeguides.rubyonrails.org/action_mailer_basics.html
这里写的东西对我没有帮助.
我正在使用Rails 3.2.8,我花了几个小时试图解决这个问题,最后它非常简单.我忘了调用方法调用返回.deliver()
的Mail::Message
对象mail(:to => @user.email, :subject => 'Welcome to the Site')
.
只需保留官方RoR教程中指定的所有内容即可.
也就是说,在您的开发/生产环境文件中,创建一个类似于的部分:
# mailer
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '<username>@gmail.com',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true
}
Run Code Online (Sandbox Code Playgroud)
然后你继承ActionMailer :: Base的子类,例如:
class InfoMailer < ActionMailer::Base
default from: "<username>@gmail.com"
def welcome_user_and_send_password(user, password)
default_url_options = self.default_url_options()
@user = user
@password = password
@url = default_url_options[:host]
mail(:to => @user.email, :subject => 'Welcome to the Site').deliver()
end
end
Run Code Online (Sandbox Code Playgroud)
之后,您可以InfoMailer
像代码方法一样在代码中使用此方法:
InfoMailer.welcome_user_and_send_password(user, password)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22545 次 |
最近记录: |