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
}
}
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.
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 ?
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;
Email on Member Approval.
Can we send email when user check "Is Approved" checkbox from backoffice.
Any alternate or suggession.
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
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
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
Thanks for sharing this Paul !
I hit an issue when registering a new user it threw an object reference not set on this line:
I bodged around it with a try catch ... is there a better way to mitigate this ?
Thanks, Kyle
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
Hope this is a little nicer than a try catch.
Paul
Excellent spot on thanks Paul
Thanks Paul
is working on a reply...