Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Faisal Mushtaq 19 posts 119 karma points
    Feb 13, 2015 @ 09:27
    Faisal Mushtaq
    1

    Email on Member Approval.

    Can we send email when user check "Is Approved" checkbox from backoffice. 

    Any alternate or suggession.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 13, 2015 @ 10:10
    Jan Skovgaard
    0

    Hi Faisal

    Yeah it should be possible to hook into the save event using the Umbraco API somehow...

    I was not able to find the member events though but perhaps you can figure it out by reading these links https://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService https://our.umbraco.org/documentation/Reference/Events-v6/

    /Jan

  • Paul 10 posts 103 karma points
    May 05, 2015 @ 22:11
    Paul
    103

    Hi,

    I recently had a very similar task and after a little digging around came up with the below class to do the job. It hooks into the member save event and should trigger an email at the appropriate point. Hopefully it will prove useful for anyone with the same problem in the future.

    Paul

    using System.Collections.Generic;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    
    public class MemberEvents : IApplicationEventHandler
    {
    
        private static UmbracoHelper _umbHelper;
        private static UmbracoHelper umbHelper
        {
            get
            {
                if (_umbHelper != null)
                    return _umbHelper;
    
                //Ensure we have an umbraco context
                if (UmbracoContext.Current == null)
                    UmbracoContext.EnsureContext(new HttpContextWrapper(HttpContext.Current), ApplicationContext.Current, true);
    
                _umbHelper = new UmbracoHelper(UmbracoContext.Current);
    
                return _umbHelper;
            }
        }
    
        // Register the save members event we need to hook into
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            Umbraco.Core.Services.MemberService.Saving += this.MemberService_Saving;
        }
    
        // Before a member is saved
        internal void MemberService_Saving(IMemberService sender, Umbraco.Core.Events.SaveEventArgs<IMember> e)
        {
            foreach (IMember member in e.SavedEntities)
            {
                //Member is not approved, dont send an email at all
                if (!member.IsApproved)
                    continue;
    
                //Pull the old approval state from the member service (this is the value before the save has updated the cache)
                bool oldValue = ApplicationContext.Current.Services.MemberService.GetById(member.Id).IsApproved;
    
                //Member wasn't approved before save but is now
                if (oldValue != member.IsApproved)
                {
                    //Code to send email goes here
                }
            }
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // unused
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // unused
        }
    }
    
  • Sadique 1 post 71 karma points
    Dec 04, 2017 @ 05:55
    Sadique
    0

    Hi Paul,

    Can you please let me know, where we need to put above code in our project so that member should be able to revive email after approval from Admin section.

    Thanks Sadique

  • Kyle 24 posts 63 karma points
    Jul 29, 2015 @ 18:40
    Kyle
    0

    Thanks for sharing this Paul !

    I hit an issue when registering a new user it threw an object reference not set on this line:

    //Pull the old approval state from the member service (this is the value before the save has updated the cache)
                    bool oldValue = ApplicationContext.Current.Services.MemberService.GetById(member.Id).IsApproved;
    

    I bodged around it with a try catch ... is there a better way to mitigate this ?

    Thanks, Kyle

  • Paul 10 posts 103 karma points
    Jul 30, 2015 @ 07:59
    Paul
    0

    Hi Kyle,

    I also hit this problem when we went though testing and the solution was to add a quick check that the member is actually in the cache. As you say this can happen when the user initially registers but since they will not be approved at this point anyway we wont need to send an email.

    I replaced the line you highlighted with this snippet of code

                var currentMemberValues = ApplicationContext.Current.Services.MemberService.GetById(member.Id);
    
                if (currentMemberValues == null)
                    continue;
    
                //Pull the old approval state from the member service, this is the value before the save.
                bool oldValue = currentMemberValues.IsApproved;
    

    Hope this is a little nicer than a try catch.

    Paul

  • Kyle 24 posts 63 karma points
    Jul 30, 2015 @ 16:12
    Kyle
    0

    Excellent spot on thanks Paul

  • Faisal Mushtaq 19 posts 119 karma points
    Jul 31, 2015 @ 06:47
    Faisal Mushtaq
    0

    Thanks Paul

Please Sign in or register to post replies

Write your reply to:

Draft