Copied to clipboard

Flag this post as spam?

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


  • Mads 9 posts 29 karma points
    Jan 03, 2015 @ 15:54
    Mads
    0

    Notify members by email, when new member is created

    I'm still new to Umbraco, and I'm trying to wrap my head around some of the basics still.

    Currently, I'm building a website, where my clients have a bunch of customers, which will be created as members on the website, so they can access parts of the website which are closed to the public.

    My issue is, that I don't receive an email with login-credentials when I create a new member, using my own email address. I figure this is:

    1. Because the member-system in Umbraco don't send out email with credentials per default
    2. I messed something up in my web.config file or I need to do something in my web.config file.
    Now, I'm pretty sure I've got a working mailserver referenced. It's a public SMTP-server on the webhotel, where the site is hosted, with no authentication needed, running on port 25 per default.
    My question is: Is there some configuration I need to do, to get the built in membership-functionality to send out emails to the members that are being created, or do I need to tamper with umbracos default functionality, and write a class that does this?
    I hope my question doesn't cause too much confusion. Please ask if you need me to ellaborate on something. Thanks!

  • Gus Braz 14 posts 38 karma points
    Feb 23, 2015 @ 04:56
    Gus Braz
    0

    I have the same question! My mail server is working as I get notifications for the Umbraco Forms submitted! I just need to email new members once their logins are created in the backoffice. 

    Does anybody know how to do it in Umbraco 7.2.1?

    Thanks in advance,

    Gus 

     

     

  • Gus Braz 14 posts 38 karma points
    Feb 25, 2015 @ 06:28
    Gus Braz
    0

    I have done it.

    First I created a class RegisterEvents:

    public class RegisterEvents : ApplicationEventHandler

    {

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,

    ApplicationContext applicationContext)

    {

    MemberService.Saved += MemberService_Saved;

    }

     

    private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)

    {

    foreach (var member in e.SavedEntities)

    {

    if (member.RawPasswordValue == null)

    continue;

     

    var mail = new MailHelper.MailVariables

    {

    From = ConfigurationManager.AppSettings["from-email"],

    ReplyTo = ConfigurationManager.AppSettings["reply-to"],

    Bcc = ConfigurationManager.AppSettings["monitoremail"],

    FromName = "Steve Waugh Foundation",

    Content = BuildEmailBody(member.Name, member.Username, member.RawPasswordValue, ConfigurationManager.AppSettings["login-url"], member.Email),

    Subject = "Your Login Details - Steve Waugh Foundation",

    To = member.Email,

    ToName = member.Name

    };

     

    if (MailHelper.SendMail(mail))

    {

    LogHelper.Info<MailHelper>(string.Format("New Member Notification sent to {0}", member.Name));

    }

     

     

    /*if (member.IsNewEntity())

    {

    //This is a brand new member object

    //Trigger indexing

    }

    else

    {

    var dirtyMember = (ICanBeDirty) member;

    var dirtyProperties = member.Properties.Where(x => x.IsDirty()).ToList();

    if (dirtyMember.IsDirty() || dirtyProperties.Count() > 1)

    {

    //More then one property or the member object itself is dirty

    //so we know that its not only LastLoginDate that is changed

    }

    }*/

    }

    }

     

    private string BuildEmailBody(string name, string username, string password, string loginLink, string emailTo)

    {

    var ldReplacements = new ListDictionary

    {

    { "<%Name%>", name },

    { "<%Username%>", username }, 

    { "<%Password%>", password },

    { "<%Link%>", loginLink }

    };

     

    var mailDefinition = new MailDefinition

    {

    BodyFileName = "/Email/Templates/memberlogin.html",

    From = ConfigurationManager.AppSettings["from-email"]

    };

     

    var mailMessage = mailDefinition.CreateMailMessage(emailTo, ldReplacements, new Control());

    mailMessage.IsBodyHtml = true;

     

    return mailMessage.Body;

    }

    Using this MailHelper class:

    public class MailHelper

    {

    public static bool SendMail(MailVariables mailVariables)

    {

    try

    {

    var mailMsg = new System.Net.Mail.MailMessage

    {

    From = new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From), HttpUtility.HtmlEncode(mailVariables.FromName)),

    Subject = mailVariables.Subject,

    Body = mailVariables.Content,

    IsBodyHtml = true

    };

     

    mailMsg.To.Add(new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.To), HttpUtility.HtmlEncode(mailVariables.ToName)));

    mailMsg.Bcc.Add(new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.Bcc), "Monitor Email"));

     

    if (mailVariables.ReplyTo != null)

    mailMsg.ReplyToList.Add(new System.Net.Mail.MailAddress(mailVariables.ReplyTo));

     

    var smtpClient = new System.Net.Mail.SmtpClient { EnableSsl = mailVariables.EnableSsl };

     

    // If PickupDirectory is relative, change it to absolute

    if (smtpClient.PickupDirectoryLocation != null && System.IO.Path.IsPathRooted(smtpClient.PickupDirectoryLocation) == false)

    smtpClient.PickupDirectoryLocation = HttpContext.Current.Server.MapPath(smtpClient.PickupDirectoryLocation);

     

    smtpClient.Send(mailMsg);

     

    return true;

    }

    catch (Exception ex)

    {

    LogHelper.Error<MailHelper>("Error creating or sending contact mail, check if the is a mailFrom property has a value.", ex);

    }

     

    return false;

    }

     

    public class MailVariables

    {

    public string Content { get; set; }

    public string Subject { get; set; }

    public string To { get; set; }

    public string Bcc { get; set; }

    public string ToName { get; set; }

    public string From { get; set; }

    public string FromName { get; set; }

    public string ReplyTo { get; set; }

    public bool EnableSsl { get; set; }

    }

    }

    Note that the only way for me to get the password (in the body of the email message) was to not encrypt the password. For that I changed the passwordFormat in the web.config from "Hashed" to "Clear":

    <membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">

    <providers>

    <clear />

    <add name="UmbracoMembershipProvider" type="Umbraco.Web.Security.Providers.MembersMembershipProvider, Umbraco" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="4" useLegacyEncoding="true" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Member" passwordFormat="Clear" />

    <add name="UsersMembershipProvider" type="Umbraco.Web.Security.Providers.UsersMembershipProvider, Umbraco" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="4" useLegacyEncoding="true" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Clear" />

    </providers>

    </membership>

    Cheers,

    Gus

     

     

     

     

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies