Yii register User with Email

Ill introduce how to make a simple email registration with Yii Forms and using an email extension for yii.
If you haven’t installed any email extensions check this post Yii email extension

Step 1 – Create the user table

Step 2 – Create the form using Gii

If you dont know what gii is check here Gii
To create the form you will need the user model which also can be created with gii, as long as you have the table in your database

  1. After creating the form a register.php will be created under views
  2. Put that register.php under site folder
  3. Also a code will be shown when you create the form, thats the action for the form
  4. Copy and paste that code in to your SiteController.php

Step 3 – Add a link to register.php

Find and change the part in main.php under views/layouts to look like this

Step 4 – Controller Register Action

For simplicity I’ll ask for a username and email to register, Adding other fields is pretty easy with Yii.

Step 5 – Activation Page and Action

Notice that these fields are hidden and will get their values from the Url, on submit the activation action will be called

  • f

    its work or not?

    • metin

      well yes and no, the implementation is ok but the email plugin used to send emails is outdated and will not work with yii version 1.1.14
      I’m going to update this post as soon I have some free time.
      What you could do is replace the plugin for sending emails with a working one.

      • Fatih

        can u please tell me did u fixed the email plugin ?

  • Fatih

    This is a great tutorial on yii register form .it’s outdated but we just need pick the idea of making registeration well thanks for writing this tutorial …

    • metin

      That’s the purpose of my tutorials, glad I could help you.

  • dff


  • Naveed

    Thanks dear for such a great solution.
    Dear I am new to yii and now working on my first project in which I also want to implement the user registration section. But I want that when a new user is registered then instead of sending an activation email, the admin ( from the backend of the site ) should review the the user information and then approve or reject the user request, just like in the comment section that when a new comment is posted then the admin approve or dis-approve the comment.
    Dear here is my email naveedmarwat1@gmail.com
    And if you know the answer of my problem then please send the answer on my email. I will be very thankful to you.

    Thanks and Regards. Naveed

    • metin

      that is actually easier than doing it with email,
      you can automatically create a view and controller with gii for the JUser table and use the update operation to activate it (just change the activated field)

  • herry

    Thanku very much Metin,Finnaly got it finding it for whole day….Great Job ………Love You bro <3.It works for me.

  • Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Many thanks

    • metin

      First of all it takes a while, second choose simple titles and use keywords in your content which you would enter in a search engine.

  • Shozin

    Did you include the JUser class anywhere?

    • metin

      no I didn’t

  • Excellent post! We are linking to this great article on our website.

    Keep up the great writing.

  • Hi there would you mind letting me know which hosting company you’re using?
    I’ve loaded your blog in 3 different browsers and I must say
    this blog loads a lot quicker then most. Can you recommend a good web hosting provider at a honest price?
    Cheers, I appreciate it!

    • metin

      Honestly most of the popular hosts will be okay, I think its the theme, I created this theme with bootstrap, removed any unnecessary javascript css, did every template I need (home, post, page, category, widgets, comments) from scratch they are all Identical by the way. It is not a 100% functional wordpress theme it has only the essentials and my templates have much less function calls than a regular wordpress template. I was planning on sharing it, when I finish tag and archive templates.

  • I couldn’t resist commenting. Very well written!

  • nkhan

    nice dear. greate tut. I just want to know that, I want to set the condition on the link activation. mean when a user register himself then the activation link is sent to the email. but that activation link should work for a specific time e.g. 2 days (or 48 hours) and when a user click on that link after 2 days, then that link should be expire and also that user record should also be deleted from the database.

    • metin

      you just have to persist the link with the date of creation in your databank or somewhere else. that way you can calculate how much time has passed since its creation and check it when you receive a request for activation

  • nkhan

    thanks dear for your reply, I am using cdbcriteria to check the date condition and also have created_date field in db. will you please tell me the syntax of how to set the condition on that date field, and also if the link expires then how to delete that user account automatically. I mean what will be their syntax.

    • metin

      I can’t remember the syntax right now it has been a while since I worked with Yii.
      I don’t really think you will need a condition for you criteria.

      Do it like this,

      1.first a user is created you save the created_date, like you are already doing
      2. when an activation is requested, you receive the email, with that email get the user from databank, no condition needed since you are using his/her email to query
      3.then get created_date from your user object and check the difference with your current system date.
      and that is all

      note: if you want to be more specific, when a user is created you can also save the region, then according to the region you can add or subtract hours to check the time difference

  • Hi great website! Does running a blog like this require a lot of work?
    I’ve no knowledge of computer programming hoowever I had been hoping to start
    my own blog in the near future. Anyway, should you have any recommendations oor techniques for new blog owners please
    share. I know this is offf topic nevertheless I simply needed to ask.

    • metin

      It doesn’t take much work to use wordpress, which is the blog website/software I’m using. Search for wordpress.

  • Pingback: Google()

Recent Posts

Recent Comments