Пример #1
0
        public virtual async Task SendEmailFromTemplateAsync(string templateFileName, string subject, string destination, object model, string from = null)
        {
            var settings = await _settingService.LoadSettingAsync <SiteSettings>();

            var    serverRoot     = HttpContext.Current.Server.MapPath("~").TrimEnd('\\') + "\\EmailTemplates\\";
            var    currentLangIso = _workContext.CurrentLanguage;
            string templateFile;

            if (File.Exists(serverRoot + templateFileName + "." + currentLangIso.IsoCode + ".html"))
            {
                templateFile = serverRoot + templateFileName + "." + currentLangIso.IsoCode + ".html";
            }
            else
            if (File.Exists(serverRoot + templateFileName + ".html"))
            {
                templateFile = serverRoot + templateFileName + ".html";
            }
            else
            if (File.Exists(templateFileName))
            {
                templateFile = templateFileName;
            }
            else
            {
                throw new FileNotFoundException("Template file not found!\n\n" + serverRoot + templateFileName + "." +
                                                currentLangIso.IsoCode + ".html\n" + serverRoot + templateFileName +
                                                ".html\n" + templateFileName);
            }

            var socialMediaAccounts = _socialAccountsService.GetAsEnumerable();
            var rootModel           = new
            {
                Model            = model,
                SiteName         = settings.GetLocalized(p => p.SiteName),
                SiteUrl          = settings.SiteUrl,
                SiteLogo         = settings.GetLocalized(p => p.SiteLogoEmailHeader).GetAbsoluteUrl(),
                SiteEmailAddress = settings.SiteEmailAddress,
                FavIcon          = settings.GetLocalized(p => p.FavIcon).GetAbsoluteUrl(),
                Title            = subject,
                DateNowYear      = DateTime.Now.Year,
                DateNowMonth     = DateTime.Now.Month,
                DateNowDay       = DateTime.Now.Day,
                DateNowHour      = DateTime.Now.Hour,
                DateNowMinute    = DateTime.Now.Minute,
                DateNowSecond    = DateTime.Now.Second,
                SocialAccounts   = socialMediaAccounts
                                   .ToList().Select(p => new { SocialNetworkIconUrl = p.GetLocalized(x => x.SocialNetworkIconUrl).GetAbsoluteUrl(),
                                                               SocialNetworkName    = p.GetLocalized(x => x.SocialNetworkName),
                                                               YourAccountUrl       = p.GetLocalized(x => x.YourAccountUrl) }),
            };

            await SendEmailAsync(_templateEngine.CompileTemplateFromFile(templateFile, rootModel), subject, destination, from);
        }