示例#1
0
        public IActionResult SubscribeUser()
        {
            string SubscriberEmail = HttpContext.Request.Form["SubscriberEmail"];

            if (!string.IsNullOrEmpty(SubscriberEmail))
            {
                try
                {
                    if (_context.Subscribers.Any(s => s.SubscriberEmail == SubscriberEmail))
                    {
                        string Link = functions.GetSiteLookupData("AppDomain") + "/Subscriber/";
                        TempData["SuccessMessage"] = $@"Thank you for confirming your subscription. <br/> You can manage your subscriptions preferences <a href=" + Link + " target='_blank'>here</a>";
                        return(RedirectToAction("Index", "Home"));
                    }

                    //add subscriber
                    functions.AddSubscriber(SubscriberEmail);

                    //set email data
                    string SubscriberID        = _context.Subscribers.Where(s => s.SubscriberEmail == SubscriberEmail).FirstOrDefault().SubscriberID;
                    string UnsubscribeLinkData = _systemConfiguration.emailUnsubscribeLink.Replace("#Subscriber#", SubscriberID);
                    UnsubscribeLinkData = _systemConfiguration.emailUnsubscribeLink.Replace("#Email#", SubscriberEmail);

                    //email subscriber
                    string   ToName            = SubscriberEmail.Split("@")[0];
                    string[] MessageParagraphs = { "Hi there",
                                                   "First off, I’d like to extend a warm welcome and ‘thank you’ for subscribing to the " + functions.GetSiteLookupData("SiteName") + " newsletter.",
                                                   "The " + functions.GetSiteLookupData("SiteName") + " news blog endeavors to send you only the best content.",
                                                   "To get you started on the right path, included in this email is a link to setup your subscription notifications to better suit your interest.",
                                                   "In the mean time, we would be sending you only the most news information.",
                                                   "If you have any questions or comments about the content you’re receiving please email us at " + functions.GetSiteLookupData("SupportEmail") + " and we will respond to your inquiry promptly.",
                                                   // "You can unsubscribe to the newsletter by clicking this <a href="+UnsubscribeLink+">link</a>"
                    };

                    string PreHeader       = "New subscription notification.";
                    bool   Button          = true;
                    int    ButtonPosition  = 6;
                    string ButtonLink      = functions.GetSiteLookupData("AppDomain") + "/Subscriber/?ID=" + SubscriberID;
                    string ButtonLinkText  = "Setting up email automation";
                    string Closure         = _systemConfiguration.emailClosure;
                    string Company         = _systemConfiguration.emailCompany;
                    string UnsubscribeLink = UnsubscribeLinkData;
                    string MessageBody     = EmailFormating.FormatEmail(MessageParagraphs, PreHeader, Button, ButtonPosition, ButtonLink, ButtonLinkText, Closure, Company, UnsubscribeLink);

                    string FromEmail = _systemConfiguration.smtpEmail;
                    string ToEmail   = SubscriberEmail;
                    string Subject   = "New Subscriber Email";

                    //Get smtp details
                    string smtpEmail   = _systemConfiguration.smtpEmail;
                    string smtpPass    = _systemConfiguration.smtpPass;
                    string displayName = _systemConfiguration.emailDisplayName;
                    string smtpHost    = _systemConfiguration.smtpHost;
                    int    smtpPort    = _systemConfiguration.smtpPort;

                    EmailService.SendEmail(FromEmail, ToEmail, Subject, MessageBody, smtpEmail, smtpPass, displayName, smtpHost, smtpPort);
                    TempData["SuccessMessage"] = @"Thank You For Subscribing! An email notification has been sent to " + SubscriberEmail + " with futher details on setting up your preferences.";
                }
                catch (Exception ex)
                {
                    //Log Error
                    _logger.LogInformation("Subscription Error: " + ex.ToString());
                    TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email.";
                }
            }
            return(RedirectToAction("Index", "Home"));
        }