FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

SaveToSender path is OS specific

Open toddb opened this issue 5 years ago • 1 comments

When using on MacOS on dotnetcore, SaveToSender doesn't find save to the directory provided because it has a windows specific directory delimiter ''

see https://github.com/lukencode/FluentEmail/blob/master/src/FluentEmail.Core/Defaults/SaveToDiskSender.cs#L35

Example usage: new SaveToDiskSender(Directory.GetCurrentDirectory()) (used in tests)

where current directory is bin > Debug > netcoreapp2.1
or

bin
  Debug
    netcoreapp2.1

Expected

Email should save to into netcoreapp2.1

Actual

Emails is saved to Debug folder with prefix netcoreapp2.1\

Proposed solution

Don't string concatenate filepaths. Use library code Path.Combine see here

 var filename = Path.Combine(_directory.TrimEnd('\\'), $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{random.Next(1000)}");

toddb avatar Mar 21 '20 04:03 toddb

@stefangrosaru

I'm not sure what you have done. However, you don't disable saving because you have to inject it in the first place. I have mine all dependency injected and below is an extract that might help you.

I only use this adapter in tests. So, it's nuisance but not critical and I output the folder name so that later on when I forget I will find them again.

Otherwise, I'd create your own implementation and register (or inject on new).

I hope that helps

        public static IServiceCollection RegisterEmailServices(
            this IServiceCollection services,
            IConfiguration configuration,
            bool isDevelopment = false)
        {
            var config = new EmailConfig();
            configuration.GetSection(EmailConfig.Key).Bind(config);

            // for now add these to be logged later
            services.AddSingleton(config);
            
            // wire up the ISender
            if (!isDevelopment)
            {
                services.AddSesSender(config.DefaultSesArnEmail
                    .ThrowArgumentNullExceptionIfNull("SES Email ARN must be specified"));
            }
            else
            {
                // 
                // In development, do not send out an emails
                // 
                var currentDirectory = Directory.GetCurrentDirectory();
                return services.AddSingleton<ISender>(new SaveToDiskSender(currentDirectory));
            }

            // 
            // 
            // WARNING: do not use Razor templating. It does not work with AWS or Azure Lambda functions
            // 
            // Razor Light does not support these environments. See https://github.com/toddams/RazorLight/issues/259
            // 
            services.TryAdd(
                ServiceDescriptor.Singleton<ITemplateRenderer, HandlebarsRenderer>(sp =>
                    new HandlebarsRenderer(Handlebars.Create())));
            
            // the extension methods didn't arrive until 2.5 and we aren't using DI 2.1.1
            // see FluentEmailSmtpBuilderExtensions
            services
                .TryAdd(ServiceDescriptor.Singleton<IFluentEmail>(x =>
                    new Email(
                        x.GetService<ITemplateRenderer>(),
                        x.GetService<ISender>(),
                        config.DefaultFromEmail,
                        config.DefaultFromName)));

            return services;
        }
    }

toddb avatar Mar 25 '20 22:03 toddb