9

I'm using Devise for authentication, and I'm confused on how to set up mail along with it. Should you still be creating your own mailer and initializer file, or should you be sending all mail through Devise? Where do you go in Devise to create the email template and the method for sending the email?

I realize this is kind of a broad question, so essentially I'm asking what is the best way to set up mail with Devise?

Also, if you wanted to send an email to a user after they have confirmed their email, how would you do this?

Justin Meltzer
  • 13,318
  • 32
  • 117
  • 182

2 Answers2

4

Devise does create it's own Mailer--if you take a look on GitHub https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb you can see that it comes with a bunch of methods already packaged in.

You can go ahead and generate the views for these methods using the command

rails g devise views

and then edit them.

If you would like to send additional email messages you should create your own Mailer to do so. I would recommend http://edgeguides.rubyonrails.org/action_mailer_basics.html . It's a pretty good overview of how to set up a mailer from the ground up.

Sid
  • 441
  • 4
  • 13
0

Devise creates the mailer and email templates for you, so you don't have to worry about that. However, if you want to change the email templates, install devise views using the command:

rails g devise:views

This will add a new "devise" folder in your views. You can find all the email templates in the mailer folder under views/devise.

Use the confirmable attribute to send confirmation emails to users post registration. By default, this attribute is commented out. So, once you install devise using the command rails g devise:install, go to db/migrate and locate the devise_create_users migration and uncomment the following lines:

t.confirmable and
add_index :users, :confirmation_token, :unique => true.
Once done, migrate your database.

Now go to your user model and check if devise has the :confirmable attribute, if not add it and you are all set.

rb512
  • 6,880
  • 3
  • 36
  • 55
  • but my main question is how do you deliver the email after the user has been confirmed – Justin Meltzer Jun 06 '11 at 14:29
  • i didn't quite get it. did you mean sending a welcome mail? if yes this might help: [link](http://stackoverflow.com/questions/4140378/how-can-i-send-a-welcome-email-to-newly-registered-users-in-rails-using-devise) – rb512 Jun 06 '11 at 15:07
  • 1
    yes, a welcome email. It looks like this might do it? `def confirm!` `UserMailer.deliver_welcome_alert(self).deliver` `super` `end` – Justin Meltzer Jun 06 '11 at 15:19
  • Would I still need to create an initializer file if I'm using something like the SendGrid Heroku addon? – Justin Meltzer Jun 06 '11 at 15:22
  • I haven't used sendgrid and am not sure what it does, but if your concern is configuring email accounts (sendmail or smtp) then you don't need any initializer. – rb512 Jun 06 '11 at 19:04