Devise 和 ActionMailer 的混淆
我正在使用 Devise 进行身份验证,但我对如何设置邮件感到困惑。您是否仍然应该创建自己的邮件程序和初始化程序文件,或者是否应该通过 Devise 发送所有邮件?您在 Devise 的哪里创建电子邮件模板以及发送电子邮件的方法?
我意识到这是一个广泛的问题,所以本质上我是在问使用 Devise 设置邮件的最佳方式是什么?
另外,如果您想在用户确认电子邮件后向他们发送电子邮件,您会怎么做?
I'm using Devise for authentication, and I'm confused on how to set up mail along with it. Should you still be creating your own mailer and initializer file, or should you be sending all mail through Devise? Where do you go in Devise to create the email template and the method for sending the email?
I realize this is kind of a broad question, so essentially I'm asking what is the best way to set up mail with Devise?
Also, if you wanted to send an email to a user after they have confirmed their email, how would you do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Devise 确实创建了自己的邮件程序 - 如果您在 GitHub 上查看 https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb 可以看到里面已经封装了一堆方法,
可以继续并使用命令生成这些方法的视图
,然后对其进行编辑。
如果您想发送其他电子邮件,您应该创建自己的邮件程序来执行此操作。我建议 http://edgeguides.rubyonrails.org/action_mailer_basics.html 。这是关于如何从头开始设置邮件程序的很好的概述。
Devise does create it's own Mailer--if you take a look on GitHub https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb you can see that it comes with a bunch of methods already packaged in.
You can go ahead and generate the views for these methods using the command
and then edit them.
If you would like to send additional email messages you should create your own Mailer to do so. I would recommend http://edgeguides.rubyonrails.org/action_mailer_basics.html . It's a pretty good overview of how to set up a mailer from the ground up.
Devise 会为您创建邮件和电子邮件模板,因此您不必担心这一点。但是,如果您想更改电子邮件模板,请使用以下命令安装设计视图:
这将在您的视图中添加一个新的“设计”文件夹。您可以在“views/devise”下的“mailer”文件夹中找到所有电子邮件模板。
使用可确认属性在注册后向用户发送确认电子邮件。
默认情况下,该属性被注释掉。因此,使用命令 Rails g devise:install 安装 devise 后,请转到 db/migrate 并找到 devise_create_users 迁移并取消注释以下行:
t.confirmable
和add_index :users, :confirmation_token, :unique =>正确。
完成后,迁移您的数据库。
现在转到您的用户模型并检查 devise 是否具有 :confirmable 属性,如果没有添加它,您就已准备就绪。
Devise creates the mailer and email templates for you, so you don't have to worry about that. However, if you want to change the email templates, install devise views using the command:
This will add a new "devise" folder in your views. You can find all the email templates in the mailer folder under views/devise.
Use the confirmable attribute to send confirmation emails to users post registration.
By default, this attribute is commented out. So, once you install devise using the command rails g devise:install, go to db/migrate and locate the devise_create_users migration and uncomment the following lines:
t.confirmable
andadd_index :users, :confirmation_token, :unique => true
.Once done, migrate your database.
Now go to your user model and check if devise has the :confirmable attribute, if not add it and you are all set.