public void SendFor(User user, Event @event, BookingTemplate template) { if (!_env.IsProduction()) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"Email are skiped out of production mode {template.ToString()}"); Console.ResetColor(); return; } ThreadPool.QueueUserWorkItem(new WaitCallback((a) => { string mailContent = _generator.GenerateFor(template, ref user, ref @event); var message = new EmailMessage(); message.ToAddresses.Add(new EmailAddress() { Name = user.FullName, Address = user.Email }); message.FromAddresses.Add(new EmailAddress() { Name = _emailConfiguration.SenderName, Address = _emailConfiguration.SenderEmail }); message.Content = mailContent; message.Subject = "Ynov Event Informations"; SendInternal(message); })); }
/// <summary> /// Generate a string[] from a template file located in templates path /// </summary> /// <returns>The from razor file as string.</returns> /// <param name="template">Template name without extension.</param> public string[] GenerateFor(BookingTemplate template, ref User[] users, ref Event @event) { string templaseAsUtf8String = null; string path = Path.Combine(Environment.ContentRootPath, Configuration.MailerPath, template.ToString().ToLower() + FILE_EXT); string[] templates = new string[users.Length]; using (var file = File.Open(path, FileMode.Open)) { using (var stream = new StreamReader(file)) { string fileContent = stream.ReadToEnd(); if (string.IsNullOrEmpty(fileContent)) { throw new TemplateEmptyException("File content is null or empty"); } templaseAsUtf8String = fileContent; } } for (int i = 0; i < users.Length; i++) { templates[i] = Format(fileContent: templaseAsUtf8String, template: template, user: ref users[i], @event: ref @event); } return(templates); }
private string Format(BookingTemplate template, string fileContent, ref User user, ref Event @event) { var content = fileContent; var root = Configuration.ServerDomain; string mainIconPath = null; content = content.Replace("{{facebook_url}}", Configuration.FacebookUrl); content = content.Replace("{{facebook_icon}}", Path.Combine(root, "images/facebook.svg")); content = content.Replace("{{instagram_url}}", Configuration.InstagramUrl); content = content.Replace("{{instagram_icon}}", Path.Combine(root, "images/instagram.svg")); content = content.Replace("{{twitter_url}}", Configuration.TwitterUrl); content = content.Replace("{{twitter_icon}}", Path.Combine(root, "images/twitter.svg")); content = content.Replace("{{ynov_url}}", Configuration.YnovUrl); content = content.Replace("{{logo}}", Path.Combine(root, "images/logo.png")); content = content.Replace("{{user_firstname}}", user.FirstName); content = content.Replace("{{event_name}}", @event.Title); mainIconPath = "images/" + template.ToString().ToLower() + ".png"; switch (template) { case BookingTemplate.AWAY: break; case BookingTemplate.PRESENT: content = content.Replace("{{jurypoint_number}}", (@event.JuryPoint ?? 0).ToString()); break; case BookingTemplate.RECALL: string remain = TimeFormatter.GetTimeWindowFrom(DateTime.Now, (DateTime)@event.OpenAt); content = content.Replace("{{remaining_hours}}", remain); AddEventInfos(ref content, ref @event); break; case BookingTemplate.AUTO_VALIDATED: mainIconPath = "images/validate.png"; AddEventInfos(ref content, ref @event); break; case BookingTemplate.SUBSCRIPTION_VALIDATED: mainIconPath = "images/validate.png"; AddEventInfos(ref content, ref @event); break; case BookingTemplate.PENDING_VALIDATION: content = content.Replace("{{subscribed_at}}", DateTime.Now.ToString()); break; } content = content.Replace("{{main_icon}}", Path.Combine(root, mainIconPath)); content = content.Replace(System.Environment.NewLine, ""); return(content); }