rails中发邮件

作者: kamionayuki | 来源:发表于2015-05-30 10:21 被阅读582次
  • 生成邮件相关文件
rails g mailer UserMailer accout_activation password_reset
  • 开发环境中的邮件配置
    config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
host = 'localhost:3000'
config.action_mailer.default_url_options = {host: host}
  • controller
    app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'
end

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer
  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.account_activation.subject
  #
  def account_activation user
    @greeting = "Hi"
    @user = user

    mail to: user.email, subject: "Account activation"
  end

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset user
    @greeting = "Hi"
    @user = user
    mail to: user.email, subject: "Password reset"
  end
end

这样就可以在controllers/models/views通过调用account_activation(user)来发送邮件

UserMailer.account_activation(User.first).deliver_now
  • view
    在发邮件之前,需要先渲染出邮件的内容,邮件有两个模板视图:html和text。与其它的views视力一样,也可以在邮件的视图中使用嵌入式ruby。

app/views/usermailer/account_activation.html.erb

<p>
  <%= "#{@greeting}, #{@user.name}" %>
  Welcome! Click on the link below to activate your account:
</p>

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, email: @user.email)  %>

or copy the link below

<%= edit_account_activation_url(@user.activation_token, email: @user.email)  %>

app/views/usermailer/account_activation.text.erb

<%="#{@greeting}, #{@user.name}" %>
Welcome! Click on the link below to activate your account:
<%= edit_account_activation_url(@user.activation_token, email: @user.email)  %>
  • 预览邮件内容
    test/mailer/preview/user_mailer_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview

  # Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
  def account_activation
    user = User.first
    user.activation_token = User.new_token
    UserMailer.account_activation(user)
  end

  # Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset
  def password_reset
    user = User.first
    user.password_reset_token = User.new_token
    UserMailer.password_reset(user)
  end

end

注意其中的两行:

# Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
# Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset

只需要复制http://localhost:3000/rails/mailers/user_mailer/account_activation就可以预览邮件内容了

相关文章

  • rails中发邮件

    生成邮件相关文件 开发环境中的邮件配置config/environments/development.rb con...

  • rails发邮件

    执行rails g mailer TestMailer 在config/environments文件夹下。修改de...

  • rails 发送邮件

    app/mailer 文件夹创建mailer.rb文件 config/intializers 创建setup_ma...

  • Rails 发送邮件

    首先需要在 config/application.rb 中配置发送邮件的相关配置 使用脚手架工具生成邮件视图模板,...

  • rails mailer

    归纳一下怎么用rails的mailer来发送邮件 首先先创建Mailer文件 rails g mailer Use...

  • Java中发送邮件

    电子邮件的应用非常广泛,例如在某网站注册了一个账户,自动发送一封欢迎邮件,通过邮件找回密码,自动批量发送活动信息等...

  • Java中发送邮件

    电子邮件的应用非常广泛,例如在某网站注册了一个账户,自动发送一封欢迎邮件,通过邮件找回密码,自动批量发送活动信息等...

  • Rails 使用 Action Mailer 邮件功能中文编码问

    最近在学习使用rails 的 action mailer实现邮件的发送,当邮件渲染的模版中包含中文字符时,会由于编...

  • 在django中发送邮件

    在Django中发送邮件很简单,只需要使用django.core.mail中的send_mail函数即可实现。具体...

  • Django 中发送邮件通知

    前言 最近在做公司运维平台中一个有关服务器权限申请的 app,其中在申请进度状态被改变时需要邮件形式提醒有关人员及...

网友评论

    本文标题:rails中发邮件

    本文链接:https://www.haomeiwen.com/subject/scarqttx.html