public async Task <string> GetContents(EmailTemplateOptions templateOptions) { if (!CanHandle(templateOptions)) { return(string.Empty); } var filename = templateOptions.File; var assembly = Assembly.GetExecutingAssembly(); var resourceName = assembly .GetManifestResourceNames() .SingleOrDefault(f => f.EndsWith(filename, StringComparison.OrdinalIgnoreCase)); if (string.IsNullOrWhiteSpace(resourceName)) { throw new FileNotFoundException( $"The embedded resource {filename} can't be found within {assembly.GetName().Name} assembly"); } await using var stream = assembly.GetManifestResourceStream(resourceName); if (stream == null) { throw new EndOfStreamException($"No stream for {resourceName}"); } using var streamReader = new StreamReader(stream); return(await streamReader.ReadToEndAsync()); }
public async Task <string> Generate(object data, Action <EmailTemplateOptions> options) { var templateOptions = new EmailTemplateOptions(); options(templateOptions); return(await Generate(data, templateOptions)); }
public async Task <string> GetContents(EmailTemplateOptions options) { var filePath = options.File; if (!Path.IsPathRooted(filePath)) { var buildDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); filePath = Path.Combine(buildDir ?? string.Empty, filePath.Replace("~/", string.Empty)); } var content = await _fileReader.ReadContents(filePath); return(content); }
public async Task <string> Generate(object data, EmailTemplateOptions options) { var provider = _templateProviders.FirstOrDefault(templateProvider => templateProvider.CanHandle(options)); if (provider == null) { return(string.Empty); } var templateContent = await provider.GetContents(options); var parsedContent = await _templateParser.Parse(templateContent, data); return(parsedContent); }
public async Task <EmailTemplate> Get(EmailTemplateOptions id) => await _context.EmailTemplate.FindAsync(id);
public bool CanHandle(EmailTemplateOptions options) { return(options.FileStorageType == FileStorageTypes.Local); }
public EmailService(IEmailFramework emailFramework, IOptions <EmailTemplateOptions> emailTemplateOptions, IHostEnvironment hostEnvironment) { _emailFramework = emailFramework; _emailTemplateOptions = emailTemplateOptions.Value ?? throw new ArgumentException("Email template options not provided"); _isDevelopment = hostEnvironment.IsDevelopment(); }