本文介绍了创建帖子后,使用Action Mailer向用户发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个User模型(带有Devise)和一个属于该用户的Post模型。我使用在创建帐户后向用户发送电子邮件

I have a User model (with Devise), and a Post model that belongs to the user. I used this railscast (pro) to send the User an email after creating an account

我创建了一个 NewPostMailer

I created a "NewPostMailer"

这是我的邮件:

class NewPostMailer < ActionMailer::Base
  default :from => "[email protected]"

  def new_post_email(user)
    @user = user
    @url = "http://localhost.com:3000/users/login"
    mail(:to => user.email, :subject => "New Post")
   end
end

我的posts_controller:

My posts_controller:

  def create
    @post= Post.new(params[:post])

    respond_to do |format|
      if @deal.save
        NewPostMailer.new_post_confirmation(@user).deliver
        format.html { redirect_to @post, notice: 'Post was successfully created.' }

post.rb

  after_create :send_new_post_email

  private
  def send_new_post_email
    NewPostMailer.new_post_email(self).deliver
  end

在用户创建帖子后,向用户发送电子邮件时我需要更改什么。谢谢。

What do I have to change to send the User an email after he creates a Post. Thanks.

推荐答案

创建另一个邮件程序()

Create another mailer (http://railscasts.com/episodes/206-action-mailer-in-rails-3)

 class YourMailerName < ActionMailer::Base
     default :from => "[email protected]"

   def post_email(user)
    mail(:to => "#{user.name} <#{user.email}>", :subject => "Registered")
   end
 end

在您的发布模型

 after_create :send_email

 def send_email
   YourMailerName.post_email(self.user).deliver
 end

发送电子邮件非常缓慢,因此请考虑将其放入后台工作。

Sending an email is very slow so think about putting this in a background job.

这篇关于创建帖子后,使用Action Mailer向用户发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:08