Rails Test "11" Add mail Send program and test mail send program

Source: Internet
Author: User

When it comes to testing the mail-sending program, we first want the system to send mail. Let's first add the ability to send mail to the system.

Action Mailer in Rails 3 is a good video tutorial that you can refer to.

There is a more detailed introduction of http://guides.rubyonrails.org/action_mailer_basics.html.

Add Mail Send Program

Send mail to ensure that we have the target mailbox, so our users have a property: email.

Or take my blog project as a practice project.

We send him an e-mail when the user registers.

First we create an initialization file

CONFIG/INITIALIZERS/SETUP_MAIL.RB, initializes the mailbox-related information.

Actionmailer::base.smtp_settings = { 
  : Address              => "smtp.163.com", 
  :p ort                 =>, 
  :d omain               => "163.com", 
  : User_name            => "woaiguanshui2012", 
  :p assword =>             "123456asdf", 
  : Authentication       => "plain", 
  : Enable_starttls_auto => True
}

We use 163 of the mailbox as a mail sending server, we need to have a registered user above, here I register a woaiguanshui201, the password is 123456ASDF.

Create a mail-sending program

Then create a mailer program with the Rails G Mailer command.

Rails G Mailer User_mailer

command creates a app/mailers/user_mailer.rb file.

Class Usermailer < actionmailer::base 
  default:from => "woaiguanshui2012@163.com"
End

Add a method inside to send a message to the user when they sign up.

Class Usermailer < actionmailer::base 
  default:from => "woaiguanshui2012@163.com"
  def registration_ Confirmation (user) 
    mail (: To => user.email,: Subject => "registered") 
  end 

Create a message content template

The content of the message can also be implemented through the template, and the controller action should have a view. Our Mailer method also needs a view to do the template.

Create the App/views/user_mailer/registration_confirmation.text.erb file, and write the following in the file.

Thank for registering!

Calling the mail-sending program

Up and down the last step, that is, after the user registered successfully, call this Mailer program Registration_confirmation method, you can send our predefined template file content.

We modify the user registration method, Userscontroller create method.

def create 
    @user = User.new (Params[:user]) 
     
    if @user. Save 
      usermailer.registration_confirmation (@user). Deliver 
     
      Flash[:notice] = "Sign Up successfully!"
      Signin (@user) 
      redirect_to root_path 
    else
      flash.now[:notice] = "Sign Up failed!"
      Render:new
    End 
  

Usermailer.registration_confirmation (@user). Deliver

The above line is our new addition, is the e-mail use.

This time to start our service Rails s, then register a user, and then look at the user's mailbox, you should have a letter from the woaiguanshui2012@163, the title is registered, the content is thank for registering!.

This means that our mail-sending programs are working properly.

Enrich Message Content Templates

We can also enrich the content of the message, such as adding user information. Modify the Registration_confirmation.text.erb file.

Hi, <%= @user. Nickname%> 
     
Thank for registering! 
     
Welcome to come back!

This requires us to add a returned variable @user to the Registration_comfirmation method.

def registration_confirmation (user) 
    @user = user 
   mail (: To => user.email,: Subject => "registered") 
 End

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.