# ActionMailer

**URL:** <https://www.ruby-forum.com/t/actionmailer/135810>\
**Category:** Rails\
**Created:** [April 25, 2008, 6:25am UTC](https://www.ruby-forum.com/t/actionmailer/135810 "2008-04-25T06:25:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 6:25am UTC](https://www.ruby-forum.com/t/actionmailer/135810/1 "2008-04-25T06:25:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 11:51am UTC](https://www.ruby-forum.com/t/actionmailer/135810/2 "2008-04-25T11:51:44Z")

</div>

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/](http://www.yoursite.com/process_activation/)  
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.

On Apr 25, 6:25 am, Saurav C. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/actionmailer/135810/3 "2008-04-25T12:39:15Z")

</div>

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 O. wrote:

> 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/](http://www.yoursite.com/process_activation/)  
> 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.
> 
> On Apr 25, 6:25ï¿½am, Saurav C. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/actionmailer/135810/4 "2008-04-25T20:50:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/actionmailer/135810/5 "2008-04-25T12:46:24Z")

</div>

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.

On Apr 25, 2:50 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/actionmailer/135810/6 "2008-04-25T22:12:31Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![prionko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@prionko](https://www.ruby-forum.com/u/prionko)\
**Post date:** [April 25, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/actionmailer/135810/7 "2008-04-25T22:54:55Z")

</div>

> 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

Bro, it’s simple question. What do you call for after\_create or  
after\_save? is it called trigger? if yes, can i write trigger in modul?  
or helper?

Thanks,  
Reint
