hermanho / postal.aspnetcore

Email sending for asp.net mvc using the view engine system to render emails.
http://aboutcode.net/postal
MIT License
26 stars 9 forks source link

Support Connection String #8

Closed the-avid-engineer closed 4 years ago

the-avid-engineer commented 5 years ago

In my application, instead of storing all of the connection data as a structured object in appsettings, I'm just storing it like most connections strings:

smtp://{username}:{password}@{host}:{port}?enableSsl={bool}

and parsing it with this:

var match = Regex.Match(smtpConnectionString, @"^smtp://((?<username>[^:@]+):(?<password>[^:@]+)@)?(?<host>[^:@,/?#]+)(:(?<port>\d+))?(\?(?<option>[^&;]+=[^&;]+)((&|;)(?<option>[^&;]+=[^&;]+))*)?$");

if (!match.Success)
{
    var message = string.Format("The connection string '{0}' is not valid.", smtpConnectionString);
    throw new Exception(message);
}

string host = default;
int? port = default;
string username = default;
string password = default;
bool enableSsl = default;

match.Groups.ForEach(group =>
{
    if (group.Name == "host")
    {
        host = group.Value;
    }
    else if (group.Name == "port")
    {
        port = int.Parse(group.Value);
    }
    else if (group.Name == "username")
    {
        username = group.Value;
    }
    else if (group.Name == "password")
    {
        password = group.Value;
    }
    else if (group.Name == "option")
    {
        foreach (var capture in group.Captures.ToArray())
        {
            var parts = capture.Value.Split('=');

            if (parts[0] == "enableSsl")
            {
                enableSsl = bool.Parse(parts[1]);
            }
        }
    }
});

Note that Port is optional, and later on if I had a null port, I would just use new SmtpClient(host)

It would be very nice if this was turned into a static method on EmailServiceOptions, such as public static EmailServiceOptions FromSmtpConnectionString(string smtpConnectionString);

I can add a pull request if this seems like a useful feature.

the-avid-engineer commented 5 years ago

(the regex came from mongodb's connection string parser, watered-down a little bit)

hermanho commented 5 years ago

Since EmailServiceOptions is implemented by options pattern, you can assign the values by any logic.

Configure simple options with a delegate

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2#configure-simple-options-with-a-delegate

Parsing the option from a connections strings format would be a great idea. I need time to think and implement whether a static method approach is fine.

PR is welcome :)

the-avid-engineer commented 5 years ago

Ugh, this is kind of annoying - some of the RegularExpression features I'm using aren't available in .NET Standard 2.0 (but are available to .NET Core) - I will try to work around the missing pieces

the-avid-engineer commented 4 years ago

I've had a branch for this for a while, kind of forgot about it, but I can't publish it to this repo (never contributed to a public repo before.. not sure if there's something I'm supposed to do.)

hermanho commented 4 years ago

You can make a pull request.

Here is a guideline https://guides.github.com/activities/forking/

the-avid-engineer commented 4 years ago

Pull Request!

hermanho commented 4 years ago

@the-avid-engineer Thank you very much

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.