0

After creating a user by admin, a welcome mail should be sent to the user's email address with login credentials i.e. username (user's email) and password.

Can you help me in the smtp settings as well?

Thank You!

mbuechmann
  • 5,413
  • 5
  • 27
  • 40
  • add your code to the question, please – DaFois Nov 15 '18 at 09:09
  • 2
    This is **typically** a bad idea. Not always, but usually. Presumably you want the admin to change their password after first logging in (otherwise their password is in plain-text in an email!!) -- which begs the question, why bother sending them a password in the first place? By default, devise will send a link with an "invitation token" which only grants initial login once, and then requires the user to set a password. – Tom Lord Nov 15 '18 at 09:32
  • Emailing passwords to users in plain text is not a great security practice. You should consider creating a random password for the user, then using Devise's recoverable module to generate a password reset token. You can then send the user a link to a page that allows them to create their own password using the devise token. – A_rayB Nov 15 '18 at 09:38
  • 1
    @A_rayB Creating a user with a random password, then "recovering the account", is a weird workaround. There are better libraries for doing this, such as [`devise_invitable`](https://github.com/scambra/devise_invitable). – Tom Lord Nov 15 '18 at 13:21
  • @TomLord yeah you are totally correct. It's been a while since I've had to implement that workflow and I'd forgotten about devise_invitable. – A_rayB Nov 15 '18 at 13:53

1 Answers1

0

I needed this functionality once and got it working by creating a method in my mailer

def welcome_email(user,password)
        @user = user
        @password = password
        mail(to: @user.email, subject: 'Welcome Email')
    end

In my welcome_email.html.erb :

<p>Dear <%= @user.name %>,</p>
<p>  Welcome to this site. Your account has successfully been created. </p>
<p> Please Login to your account</a> using these credentials: </p>
<ul>
  <li>Username: <%= @user.email %></li>
  <li>Password: <%= @password %></li>
</ul>

And call this method from your devise users/registration_controller in create action after creating the user.

MyMailer.welcome_email(resource,params[:user][:password]).deliver_later

you might have to change the code according to your needs, but you'll get an idea as from where to start.

For SMTP settings, see this

Saqib Shahzad
  • 982
  • 12
  • 28