DotNetNukeŽ ADefSignUp: Email Sign-Up and Verification Module

This program allows you to create a sign-up list of verified email addresses. The Sign-up module can be for anything such as "Sign-up for our Beta program".

A DotNetNuke portal administrator can place an instance of the module on a page and customize the sign-up message and the email verification message. The captured data can also be exported.

Portal visitors can sign up by entering their name and email.

They are sent an email that they use to confirm their email address.

Clicking on the link will confirm their membership

The Code

View.ascx.cs:

namespace ADefSignUp
{
    public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase,
        DotNetNuke.Entities.Modules.IActionable
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadSettings();

                if (Request.QueryString["key"] != null)
                {
                    ProcessKey(Convert.ToString(Request.QueryString["key"]));
                }
            }
        }

        #region LoadSettings
        private void LoadSettings()
        {
            ADefSignUpDALDataContext db = new ADefSignUpDALDataContext();

            ADefSignUp_Setting objSetting = 
                (from ADefSignUp_Settings in db.ADefSignUp_Settings
                 where ADefSignUp_Settings.ModuleID == ModuleId
                 select ADefSignUp_Settings).FirstOrDefault();

            if (objSetting == null)
            {
                objSetting = new ADefSignUp_Setting();
                objSetting.ModuleID = ModuleId;
                objSetting.SignupEmailMessage = 
                    "Navigate to the link below to verify your Sign-up";
                objSetting.SignupMessage = 
                    "Enter your name and email below to Sign-up";

                db.ADefSignUp_Settings.InsertOnSubmit(objSetting);
                db.SubmitChanges();
            }

            txtSignupMessage.Text = objSetting.SignupMessage;
        }
        #endregion

        #region btnSubmit_Click
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if(!IsValidEmailAddress(txtEmail.Text.Trim()))
            {
                lblResult.Text = "Must use a valid email";
                return;
            }

            ADefSignUpDALDataContext db = new ADefSignUpDALDataContext();

            try
            {
                // Get email message
                ADefSignUp_Setting objSetting = 
                    (from ADefSignUp_Settings in db.ADefSignUp_Settings
                     where ADefSignUp_Settings.ModuleID == ModuleId
                     select ADefSignUp_Settings).FirstOrDefault();

                // Create text
                string strPassword = Utility.GetRandomPassword();

                string strLinkUrl =
                    Utility.FixURLLink(
                    DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID,
                    "", "mid=" + ModuleId.ToString(),
                    String.Format(@"&key={0}", strPassword)),
                    PortalSettings.PortalAlias.HTTPAlias);

                string strSubject =
                    String.Format("{0} Sign-up confirmation", 
                    this.PortalSettings.PortalName);

                // Insert user into database
                string strBody = objSetting.SignupEmailMessage;
                strBody = strBody + Environment.NewLine;
                strBody = strBody + Environment.NewLine;
                strBody = strBody + String.Format("{0}", strLinkUrl);

                ADefSignUp_Signup Signup = new ADefSignUp_Signup();
                Signup.Name = txtName.Text.Trim();
                Signup.Email = txtEmail.Text.Trim();
                Signup.ModuleID = ModuleId;
                Signup.Password = strPassword;
                Signup.CreatedDate = DateTime.Now;

                db.ADefSignUp_Signups.InsertOnSubmit(Signup);
                db.SubmitChanges();

                // Send email
                DotNetNuke.Services.Mail.Mail.SendMail(
                    PortalSettings.Email, txtEmail.Text.Trim(), "",
                    strSubject, strBody, "", "HTML", "", "", "", ""
                    );

                // Hide form and show confirmation
                pnlNameAndEmail.Visible = false;
                pnlSubmisionConfimation.Visible = true;
            }
            catch (Exception ex)
            {
                lblResult.Text = ex.Message;
            }
        }
        #endregion

        #region ProcessKey
        private void ProcessKey(string Key)
        {
            pnlNameAndEmail.Visible = false;
            pnlSubmisionConfimation.Visible = false;
            pnlSignUpConfimation.Visible = true;

            ADefSignUpDALDataContext db = new ADefSignUpDALDataContext();

            ADefSignUp_Signup objSignUp = 
                (from ADefSignUp_Signups in db.ADefSignUp_Signups
                 where ADefSignUp_Signups.Password == Key
                 where ADefSignUp_Signups.ConfirmedDate == null
                 select ADefSignUp_Signups).FirstOrDefault();

            if (objSignUp == null)
            {
                lblSignupConfirmation.Text = 
                    "This Sign-up Confimation is incorrect or already used.";
            }
            else
            {
                objSignUp.ConfirmedDate = DateTime.Now;
                db.SubmitChanges();

                lblSignupConfirmation.Text = 
                    "Your sign-up membership has been confirmed.";
            }
        }
        #endregion

        #region IsValidEmailAddress
        // From: 
        http://bytes.com/topic/c-sharp/answers/225262-code-validating-email-address-c-validate-email       
        public static bool IsValidEmailAddress(string sEmail)
        {
            if (sEmail == null)
            {
                return false;
            }
            else
            {
                return Regex.IsMatch(sEmail,
                    @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
                    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|
                    [a-zA-Z]{2})$",
                    RegexOptions.IgnorePatternWhitespace);
            }
        } 
        #endregion

        // IActionable

        #region IActionable Members
        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Actions =
                    new DotNetNuke.Entities.Modules.Actions.ModuleActionCollection();
                Actions.Add(
                    GetNextActionID(),
                    "Administration",
                    DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, 
                    "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);

                return Actions;
            }
        }
        #endregion
    }
}

Export to Excel code:

        #region CVS Exporter
        protected void lnkExport_Click(object sender, EventArgs e)
        {
            ADefSignUpDALDataContext db = new ADefSignUpDALDataContext();

            List<ADefSignUp_Signup> colSignups = 
                (from ADefSignUp_Signups in db.ADefSignUp_Signups
                 where ADefSignUp_Signups.ModuleID == ModuleId
                 select ADefSignUp_Signups).ToList();

            WriteToCSV(colSignups);
        }

        // from http://wiki.asp.net/page.aspx/401/export-to-csv-file/
        public static void WriteToCSV(List<ADefSignUp_Signup> SignUps)
        {
            string attachment = "attachment; filename=SignUpList.csv";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");
            WriteColumnName();

            foreach (ADefSignUp_Signup SignUp in SignUps)
            {
                WriteUserInfo(SignUp);
            }
            HttpContext.Current.Response.End();
        }

        private static void WriteUserInfo(ADefSignUp_Signup SignUp)
        {
            StringBuilder stringBuilder = new StringBuilder();
            AddComma(SignUp.Name, stringBuilder);
            AddComma(SignUp.Email, stringBuilder);
            AddComma(SignUp.CreatedDate.ToShortDateString(), stringBuilder);
            AddComma((SignUp.ConfirmedDate.HasValue) ?
                SignUp.ConfirmedDate.Value.ToShortDateString()
                : "", stringBuilder);
            HttpContext.Current.Response.Write(stringBuilder.ToString());
            HttpContext.Current.Response.Write(Environment.NewLine);
        }

        private static void AddComma(string value, StringBuilder stringBuilder)
        {
            stringBuilder.Append(value.Replace(',', ' '));
            stringBuilder.Append(", ");
        }

        private static void WriteColumnName()
        {
            string columnNames = "Name, Email, CreatedDate, ConfirmedDate";
            HttpContext.Current.Response.Write(columnNames);
            HttpContext.Current.Response.Write(Environment.NewLine);
        }
        #endregion

Download the code: ADefSignUp_01.00.00_Install.zip (install and source)
Note: If using DotNetNuke 5.1 or lower, install and run LinqPrep first.

[Back to: The ADefWebserver DotNetNuke HELP WebSite]


DotNetNukeŽ is a registered trademark of the DotNetNuke Corporation