Skip to content

Allows users to submit rfc822 formatted email addresses #577

Closed
@mbernier

Description

@mbernier
Contributor

Issue Summary

Similar to: sendgrid/sendgrid-python#348

Where the user can submit email addresses as:
example@example.com
or
example example <example@example.com>

Activity

brandon93s

brandon93s commented on Oct 22, 2017

@brandon93s
Contributor

Looks like there is already an implementation in MailHelper that is available to consumers, but not called automatically anywhere.

The constructor for EmailAddress could be updated to call this method, adding support:

/// <summary>
/// Initializes a new instance of the <see cref="EmailAddress"/> class.
/// </summary>
/// <param name="email">The email address of the sender or recipient.</param>
/// <param name="name">The name of the sender or recipient.</param>
public EmailAddress(string email, string name = null)
{
    if (name == null)
    {
        var address = MailHelper.StringToEmailAddress(email);
        this.Email = address.Email;
        this.Name = address.Name;
    }
    else
    {
        this.Email = email;
        this.Name = name;
    }
}

Thoughts?

Niladri24dutta

Niladri24dutta commented on Apr 4, 2018

@Niladri24dutta
Contributor

@thinkingserious I can submit a PR with the change suggested above regarding MailHelper.StringToEmailAddress. Let me know if it's fine.

thinkingserious

thinkingserious commented on Apr 5, 2018

@thinkingserious
Contributor

That would be great, thanks @Niladri24dutta!

Niladri24dutta

Niladri24dutta commented on Apr 16, 2018

@Niladri24dutta
Contributor

@thinkingserious just an observation regarding the above approach , MailHelper.StringToEmailAddress calls the EmailAddress class constructor internally . If we use the MailHelper.StringToEmailAddress method in the constructor of EmailAddress class then it may create 2 issues -

  • there will be a cyclic dependency
  • The constructor of EmailAddress class will be called infinite times if the name is null and the email address is matched with RFC2822 format.

To overcome this have created a new overload of the EmailAddress class constructor which accepts a email address and a boolean flag to denote whether email address is in RFC2822 format like below

public EmailAddress(string email, bool isRFC2822Address)

Let me know your view on this before I proceed.

Here is my working branch

https://github.com/Niladri24dutta/sendgrid-csharp/tree/RFC822CompliantEmailSupport

thinkingserious

thinkingserious commented on Apr 16, 2018

@thinkingserious
Contributor

How about factoring out this part:

var email = match.Groups[EmailGroup].Value.Trim();
var name = match.Groups[NameGroup].Value.Trim();

Then you could just call the factored method internally without the cyclic dependency.

With Best Regards,

Elmer

Niladri24dutta

Niladri24dutta commented on Apr 17, 2018

@Niladri24dutta
Contributor

@thinkingserious Then,do we need to create a new helper method in MailHelper class to include the above code you mentioned or should we refactor the existing StringToEmailAddress method. Because I can see when the email id is not matched then also the EmailAddress class constructor is called as seen below -

var match = Rfc2822Regex.Match(rfc2822Email);
    if (!match.Success)
    {
        return new EmailAddress(rfc2822Email);
     }

https://github.com/sendgrid/sendgrid-csharp/blob/master/src/SendGrid/Helpers/Mail/MailHelper.cs#L139

However,we can change the return type of the StringToEmailAddress to return a Dictionary<string> which can contain the email and name as key value pair.

thinkingserious

thinkingserious commented on Apr 18, 2018

@thinkingserious
Contributor

I think you can encapsulate that check into a utility function, for example, maybe isRfc2822Email(email). We should try to not change StringToEmailAddress's function signature to avoid any breakage. Unless you want to overload the function.

thinkingserious

thinkingserious commented on Mar 10, 2021

@thinkingserious
Contributor

Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @thinkingserious@mbernier@brandon93s@Niladri24dutta

        Issue actions

          Allows users to submit rfc822 formatted email addresses · Issue #577 · sendgrid/sendgrid-csharp