ActionMailer

Hi all,

I need a help. I have got a user registration form, where the user signs up and then an activation link is sent to the user email id for activating his/her account. Now I need to send an another email to the user, only after he logs in for the first time in my site, then the second email should be sent. Can anybody give me some suggestion on how to do it ??

NB: When the user clicks on the activation link, he is not directed to user home page, only the home page of my website is displayed.

Thanks Saurav

In your controller where you check the activation link, mark the user as activated, call a second ActionMailer, and then redirect.

In the following example the user gets an email with a link that points to www.yoursite.com/process_activation/<ACTIVATIONCODE> There is a "Mailman" model which is your ActionMailer.

def process_activation   if user = User.find_by_activation_code(params[:id])     user.active = true #or however you mark a user as fully active     user.save     Mailman.deliver_after_activation(user)     redirect_to... and return #this is where a validated user should go   else     flash[:error] = "Invalid Activation Code"     redirect_to... #or do whatever you want here when someone has an invalid code   end end

This isn't 1000% secure, it doesn't actually match the user with the code, etc. But it's good enough for me. If it's not good enough for you then it's easy enough to adapt.

Of course, you need a standard ActiveMailer model and view. 'user' will be passed to it.

Hi Andrew, Thanks for the interest. But this is not my problem. Rite now Im able to send the email once the user clicks on the activation mail. But what I really want is that the welcome email should go to the user only after he first logs into my website. Im using restul_authentication plugin

my user_observer.rb file is class UserObserver < ActiveRecord::Observer   def after_create(user)     UserMailer.deliver_signup_notification(user)   end

  def after_save(user)

    UserMailer.deliver_activation(user) if user.pending?

  end end

So when a user signs up initially the mail having the activation link is being sent. When the user clicks on the activation link, another email is sent to the user. But I want this second email to sent only after he logs in the site for the first time. I m stuck here.

Hi Andrew, Thanks for the interest.Actually this is not the problem Im facing. Rite now Im using restful authentication plugin, and with this Im able to send the email after user has clicked on the activation link. But what I really want is that no email should go to the user after he activates himself.What I really want is that only after the activation when the user first logs into my site, then only an email should be sent to him. I think some sort of tracker is needed for the user login. But Im not really understanding on how to do it

Andrew Ohnstad wrote:

I was thinking that you could have an initial_login boolean field in your users table that could initially be set to false. Each time a user logs in, you could call a method that sees if the user has logged in before (i.e., is initial_login still false?), and if not, sends the user an email and sets initial_login to true.

-Kyle

You could also use a 'last_logged_in_at' field that's null by default. The email gets fired in response to the first login (field null=>value) and you also get a field that helps you understand how much your users are using the system (and the chance to send "You've been away for a while..." emails).