@skirunman Here’s an example of how we send a Forgot Password email. Keep in mind we’ve actually got things all packaged into internal packages for accounts, mailer, etc. So, I’m extrapolating a bit, but it’s pretty basic. Just override the base account functionality where you need to do something different.
/server/accounts.coffee:
mailOpts = {}
# This is lifted from accounts-password (@1.1.1) so we can customize the email sent to use our Mandrill templates
#
# send the user an email with a link that when opened allows the user
# to set a new password, without the old password.
# @summary Send an email with a link the user can use to reset their password.
# @locus Server
# @param {String} userId The id of the user to send email to.
# @param {String} [email] Optional. Which address of the user's to send the email to. This address must be in the user's `emails` list. Defaults to the first email in the list.
#
Accounts.sendResetPasswordEmail = (userId, email)->
# Make sure the user exists, and email is one of their addresses.
user = Meteor.users.findOne userId
if !user
throw new Error("Can't find user")
# pick the first email if we weren't passed an email.
if !email && user.emails && user.emails[0]
email = user.emails[0].address
# make sure we have a valid email
if !email or !_.contains(_.pluck(user.emails or [], 'address'), email)
throw new Error('No such email for user.')
token = Random.secret()
whn = new Date()
tokenRecord =
token: token
email: email
when: whn
Meteor.users.update userId, $set: "services.password.reset": tokenRecord
# before passing to template, update user object with new token
Meteor._ensure(user, 'services', 'password').reset = tokenRecord
mailOpts.token = Meteor.absoluteUrl('reset/' + token)
# This sends the forgot-password template using our mailer
OUR.mailer.send "forgot-password", userId, null, email, mailOpts
return
Then in your mailer:
##################################################################
# Methods for
# Mailer via Mandrill
##################################################################
_sendTemplate = (template, options, vars = [])->
OUR.utils.log.info "Sending mail template", template, "to", options.email
vars = for opt, val of options
name: opt
content: val
Meteor.Mandrill.sendTemplate
template_name: template
template_content: [
{}
]
message:
global_merge_vars: [
name: "var1"
content: "Global Value 1"
]
merge_vars: [
rcpt: options.email
vars: vars
]
to: [
email: options.email
]
bcc_address: options.bcc_address
@OUR.utils.namespace.addTo "mailer",
send: (template, userId, ticket, email, options={})->
switch template
when 'forgot-password'
user = Meteor.users.findOne(userId)
return unless user
return unless user.emails[0].address == email
options.email = user.emails[0].address
options.fname = options.email
options.fname = user.profile.firstName if user.profile?.firstName?
_sendTemplate(template, options)
Meteor.startup ->
Meteor.Mandrill.config {
username: process.env.MANDRILL_USER,
key: process.env.MANDRILL_KEY
}
Meteor.methods
mailCurrentUser: (template, options={})->
OUR.mailer.send(template, @userId, null, null, options)
We have cases for all the various transactional mails we might send. New signup, forgot password, service related stuff, etc.