Tevin Thuku Tevin Thuku - 4 months ago 91
React JSX Question

Meteor accounts Email Verification

How do I send an email verification to a user with a custom message and a custom from parameter instead of the default one.

Meteor.methods({
sendveryficationmail: function (theuserId, themail) {
this.unblock();
Accounts.sendVerificationEmail(theuserId);
}
});


When I use that method I get the default meteor verification email sent. How do I change the parameters and where should I change them from?

Answer

You can create your own function to send custom HTML using templates, you can event pass simple HTML to instead of template file. Here is code:

'sendVerificationEmail':function (emailId) {
        Meteor.users.update({'emails.address': emailId}, {$unset: {'services.email.verificationTokens': 1}});
        var user = Meteor.users.findOne({"emails.address": emailId});
        if (user) {
                if( user.emails.find(email=> email.address === emailId ).verified ){
                    throw new Meteor.Error("Email already verified");
                } else {
                    var userInfo = user.profile;
                    var emailId = user.email[0].address;
                    Accounts.emailTemplates.siteName = "NJAL";
                    Accounts.emailTemplates.from = "myTest <community@myTest.com>";
                    Accounts.emailTemplates.verifyEmail.subject = function(user) {
                        return "Account Verification Required";
                    };
                    Accounts.emailTemplates.verifyEmail.html = function (user, url) {
                         SSR.compileTemplate( 'registartionEmail', Assets.getText( 'email_templates/registration_confirm.html' ) );
                        var res=url.split("/");
                      var emailData = {
                            "designer_name": userInfo.fname + " "+ userInfo.lname,
                            "url": "http://domain.com/pages/verify/?token="+res[res.length-2]+"/"+res[res.length-1],
                            "emailId": emailId,
                        };
                        return SSR.render( 'registartionEmail', emailData );
                    };
                    Accounts.sendVerificationEmail(user._id, emailId);
                }
        } else {        
            throw new Meteor.Error("Email does not exist");
        }
  },