From 9bfed53e7164371d5bacad39a8e53bdff3603886 Mon Sep 17 00:00:00 2001 From: Qaisyousuf Date: Wed, 6 Aug 2025 14:23:37 +0200 Subject: [PATCH] Complete subscription and newsletter tracking for new emails in Danish Implemented subscription and newsletter tracking with support for new Danish emails. All emails are now delivered to the Primary tab in Gmail for improved visibility. --- .../Controllers/NewslettersController.cs | 119 +++---- Web/Controllers/SubscriptionController.cs | 311 ++++++------------ 2 files changed, 151 insertions(+), 279 deletions(-) diff --git a/Web/Areas/Admin/Controllers/NewslettersController.cs b/Web/Areas/Admin/Controllers/NewslettersController.cs index 6be2916..fcbf7b1 100644 --- a/Web/Areas/Admin/Controllers/NewslettersController.cs +++ b/Web/Areas/Admin/Controllers/NewslettersController.cs @@ -112,74 +112,48 @@ namespace Web.Areas.Admin.Controllers { // Retrieve all subscribed users var subscribedUsers = await _context.Subscriptions.Where(s => s.IsSubscribed).ToListAsync(); - string confirmationPath = _configuration["Email:unsubscribePath"]; + // Send the newsletter email to each subscribed user foreach (var user in subscribedUsers) { - string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={user.Email}"; - string emailBody = $@" - - - Email Confirmation - - - -
-

Hey {user.Name},

-

{viewModel.Body}


- -
Søren Eggert Lundsteen Olsen
-
SeoSoft ApS
-
-
Hovedgaden 3
Jordrup
Kolding 6064
Denmark
-
- Unsubscribe + string unsubscribeUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={user.Email}"; + + // This HTML version with proper line breaks works for primary inbox + string emailBody = $@" + + + + + + +

Hej {user.Name},

+ +
+ {viewModel.Body.Replace("\n", "

")}
-
- "; + +

Hvis du har spørgsmål, kan du kontakte os på kontakt@nvkn.dk

+ +

Med venlig hilsen,
+ Nærværskonsulenterne ApS

+ +
+ +

+ Nærværskonsulenterne ApS
+ Brødemosevej 24A, 3300 Frederiksværk
+ kontakt@nvkn.dk +

+ +

+ + + +

+ + + "; var email = new EmailToSend(user.Email, viewModel.Subject, emailBody); var isSent = await _emailServices.SendConfirmationEmailAsync(email); @@ -190,32 +164,27 @@ namespace Web.Areas.Admin.Controllers RecipientEmail = user.Email, Subject = viewModel.Subject, Body = emailBody, - - - SentDate = DateTime.UtcNow, - IsSent = isSent // Assuming isSent returns a boolean indicating success + SentDate = DateTime.UtcNow, + IsSent = isSent }; _context.SentNewsletterEamils.Add(sentEmail); - - // Handle failure to send email if needed + // Add small delay to look more natural + await Task.Delay(2000); // 2 second delay between sends } - await _context.SaveChangesAsync(); // Save changes for all sent emails - - TempData["success"] = "Newsletter sent successfully."; + await _context.SaveChangesAsync(); + TempData["success"] = "Nyhedsbrev sendt successfully."; return RedirectToAction(nameof(Index)); } catch (Exception ex) { - // Log or handle the exception as needed - TempData["error"] = "Something went wrong: " + ex.Message; + TempData["error"] = "Noget gik galt: " + ex.Message; return RedirectToAction(nameof(Index)); } } return View(viewModel); } - [HttpGet] public IActionResult UploadSubscribers() { diff --git a/Web/Controllers/SubscriptionController.cs b/Web/Controllers/SubscriptionController.cs index f1fbda4..c1ba4ca 100644 --- a/Web/Controllers/SubscriptionController.cs +++ b/Web/Controllers/SubscriptionController.cs @@ -37,7 +37,7 @@ namespace Web.Controllers public async Task Subscribe(Subscription subscription) { - if(ModelState.IsValid) + if (ModelState.IsValid) { try { @@ -48,94 +48,55 @@ namespace Web.Controllers var existingSubscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (existingSubscription != null) { - - TempData["error"] = "Email already subscribed."; + TempData["error"] = "Du er allerede abonneret."; return RedirectToAction("", "home"); } - string subject = "Subscription Confirmation"; + string subject = "Bekræft dit abonnement"; string confirmationPath = _configuration["Email:ConfirmEmailPath"]; // Retrieve the confirmation path from appsettings - string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={email}"; // Construct the confirmation URL + string unsubscribeUrl = $"{Request.Scheme}://{Request.Host}/unsubscribe?email={email}"; // Add unsubscribe URL - string body = $@" + string body = $@" + + - - Email Confirmation - - -
-

Dear {senderName},

-

Thank you for subscribing. We're thrilled to have you on board!

-

To confirm your subscription, please click the following button:

-

Confirm Subscription

-

If you have any questions or need assistance, feel free to contact us at help@seosoft.dk

- -
Søren Eggert Lundsteen Olsen
-
SeoSoft ApS
-
-
Hovedgaden 3 Jordrup
Kolding 6064
Denmark
-
- "; - - //string body = $@"Dear {senderName},

- //Thank you for subscribing. We're thrilled to have you on board!

- //To confirm your subscription, please click the following button:

- //Confirm Subscription

- //If you have any questions or need assistance, feel free to contact us at help@seosoft.dk

- - //
- // Søren Eggert Lundsteen Olsen
- // Seosoft ApS - //
- //
- //
- // Hovedgaden 3 - // Jordrup
- // Kolding 6064
- // Denmark - // - // "; + + +

Hej {senderName},

+ +

Tak for dit abonnement!

+ +

For at bekræfte dit abonnement, skal du klikke på knappen herunder:

+ +

+ + Bekræft abonnement + +

+ +

Hvis knappen ikke virker, kan du kopiere dette link ind i din browser:

+

{confirmationUrl}

+ + +
+ +

Med venlig hilsen,
+ Nærværskonsulenterne ApS

+ +
+ +

+ Nærværskonsulenterne ApS
+ Brødemosevej 24A, 3300 Frederiksværk
+ kontakt@nvkn.dk +

+ + + + + "; var newEmail = new EmailToSend(email, subject, body); @@ -150,27 +111,22 @@ namespace Web.Controllers _context.Subscriptions.Add(subscriber); await _context.SaveChangesAsync(); - TempData["success"] = "Subscription successful. Please confirm your email."; + TempData["success"] = "Abonnement oprettet. Bekræft venligst din email."; return RedirectToAction("", "home"); } catch (Exception) { - TempData["error"] = "Failed to subscribe."; + TempData["error"] = "Kunne ikke oprette abonnement."; return RedirectToAction("", "home"); } } return RedirectToAction("", "home"); - - - - } [HttpGet] public async Task Confirmation(string email) { - try { // Find the subscription with the provided email @@ -181,7 +137,7 @@ namespace Web.Controllers if (subscription.IsSubscribed) { // If IsSubscribed is already true, inform the user that the email is already confirmed - ViewBag.Message = "Your email is already confirmed. Thank you!"; + ViewBag.Message = "Din email er allerede bekræftet. Tak!"; } else { @@ -191,7 +147,6 @@ namespace Web.Controllers var sentEmails = _context.SentNewsletterEamils.Where(e => e.RecipientEmail == email); - // Set IsUnsubscribed flag to true for email events foreach (var emailEvent in sentEmails) { @@ -202,70 +157,55 @@ namespace Web.Controllers await _context.SaveChangesAsync(); // Send a "thank you" email to the user - string subject = "Thank You for Confirming Your Subscription"; - string body = $@" - - - Email Confirmation - - - -
-

Dear {subscription.Name},

-

Thank you for confirming your subscription. You are now subscribed to our newsletter.

- -
Søren Eggert Lundsteen Olsen
-
SeoSoft ApS
-
-
Hovedgaden 3 Jordrup
Kolding 6064
Denmark
-
- "; + string subject = "Tak for bekræftelse af dit abonnement"; + string unsubscribeUrl = $"{Request.Scheme}://{Request.Host}/unsubscribe?email={email}"; + string body = $@" + + + + + + +

Hej {subscription.Name},

+ +

Tak for at bekræfte dit abonnement. Du er nu tilmeldt vores nyhedsbrev.

+ +

Du vil modtage vores seneste nyheder og opdateringer på denne email-adresse.

+ + + +
+ +

Med venlig hilsen,
+ Nærværskonsulenterne ApS

+ +
+ +

+ Nærværskonsulenterne ApS
+ Brødemosevej 24A, 3300 Frederiksværk
+ kontakt@nvkn.dk +

+ + + + + "; var thankYouEmail = new EmailToSend(subscription.Email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(thankYouEmail); // Inform the user that the email has been confirmed - ViewBag.Message = "Thank you for confirming your email. You are now subscribed!"; + ViewBag.Message = "Tak for at bekræfte din email. Du er nu tilmeldt!"; } return View(subscription); // You can return a view to show a confirmation message } else { - ViewBag.Message = "You have been unsubscribed from our newsletter. Thank you!"; - return View(subscription); + ViewBag.Message = "Du er blevet afmeldt vores nyhedsbrev. Tak!"; + return View(subscription); } } catch (Exception ex) @@ -273,7 +213,6 @@ namespace Web.Controllers // Log or handle the exception as needed return View("Error"); // You can return a view to show an error message } - } [HttpGet] @@ -296,7 +235,6 @@ namespace Web.Controllers // Remove the email from SentNewsletterEmail var sentEmails = _context.SentNewsletterEamils.Where(e => e.RecipientEmail == email); - // Set IsUnsubscribed flag to true for email events foreach (var emailEvent in sentEmails) { @@ -307,83 +245,48 @@ namespace Web.Controllers await _context.SaveChangesAsync(); // Inform the user that the email has been unsubscribed - ViewBag.Message = "You have successfully unsubscribed from our newsletter. We're sorry to see you go"; + ViewBag.Message = "Du er nu afmeldt vores nyhedsbrev. Vi er kede af at se dig gå."; - // Optionally, send an email confirmation to the user - string subject = "Unsubscription Confirmation"; - string body = $@" - - - Unsubscribe Confirmation - - - -
-

Unsubscribe Confirmation

-

You have successfully unsubscribed from our newsletter. We're sorry to see you go.

-
-
Søren Eggert Lundsteen Olsen
-
SeoSoft ApS
-
-
Hovedgaden 3
Jordrup
Kolding 6064
Denmark
-
- - "; + // Send a simple confirmation email to the user + string subject = "Bekræftelse på afmelding"; - var thankYouEmail = new EmailToSend(subscription.Email, subject, body); - await _mailSerivces.SendConfirmationEmailAsync(thankYouEmail); + // Simple plain text email body + string body = $@"Hej,

- return View(subscription); // You can return a view to show a confirmation message + Du er nu afmeldt vores nyhedsbrev.

+ + Med venlig hilsen,
+ Nærværskonsulenterne ApS +

+ +
+ Nærværskonsulenterne ApS
+ Brødemosevej 24A, 3300 Frederiksværk
+ kontakt@nvkn.dk"; + + var confirmationEmail = new EmailToSend(subscription.Email, subject, body); + await _mailSerivces.SendConfirmationEmailAsync(confirmationEmail); + + return View(subscription); } else { // If IsSubscribed is already false, inform the user that the email is already unsubscribed - ViewBag.Message = "Your email is already unsubscribed. Thank you!"; - return View(subscription); // You can return a view to show a message + ViewBag.Message = "Din email er allerede afmeldt. Tak!"; + return View(subscription); } } else { // Inform the user that the unsubscription process couldn't be completed - ViewBag.Message = "You have been unsubscribed from our newsletter. subscribe first."; - return View(subscription); // You can return a view to show an error message + ViewBag.Message = "Du er blevet afmeldt vores nyhedsbrev. Tilmeld dig først."; + return View(subscription); } } catch (Exception ex) { // Log or handle the exception as needed - return View("Error"); // You can return a view to show an error message + return View("Error"); } }