mocktools / go-smtp-mock

SMTP mock server written on Golang. Mimic any 📤 SMTP server behavior for your test environment with fake SMTP server.
MIT License
123 stars 17 forks source link

[BUG] Email address with name is considered invalid #153

Open codedge opened 1 year ago

codedge commented 1 year ago

New bug checklist

Bug description

I am trying to send an email with this sender address John Doe<john.doe@example.com>. I think this should also be a valid email address format to be supported by smtpmock.

Currently I am getting back this error: SMTP response: 501 MAIL FROM requires valid email address

This applies to both, sender and receiver address.

Complete output when running smtpmock, including the stack trace and command used
server := smtpmock.New(smtpmock.ConfigurationAttr{
  PortNumber: 2525,
  LogToStdout:       true,
  LogServerActivity: true,
})

if err := server.Start(); err != nil {
  t.Fatal(err)
}
INFO: 2023/02/18 22:03:23.182796 SMTP response: 220 Welcome
truetime="2023-02-18T22:03:23+01:00" level=info msg="Sending mail to SMTP"
INFO: 2023/02/18 22:03:23.182969 SMTP request: EHLO localhost
INFO: 2023/02/18 22:03:23.183380 SMTP response: 250 Received
INFO: 2023/02/18 22:03:23.183462 SMTP request: MAIL FROM:<John Doe<john.doe@example.com>>
INFO: 2023/02/18 22:03:23.183590 SMTP response: 501 MAIL FROM requires valid email address
time="2023-02-18T22:03:23+01:00" level=error msg="MAIL FROM requires valid email address"
bestwebua commented 1 year ago

@codedge Thanks! It is out of minimal scope. But I agree, seems we should implement it. https://www.rfc-editor.org/rfc/rfc5322#section-3.4

cengizhancaliskan commented 9 months ago

Hi,

I encouraged the same problem. What is the priority of this one? @bestwebua

Can it be solved by updating the emailRegexPattern in consts.go?

More failing cases (Should be valid):

John <john@mail.com> <john@mail.com>

bestwebua commented 9 months ago

Hello, @cengizhancaliskan! Thanks for your interest! Yes, it can be solved here:

  1. Main regex, emailRegexPattern: https://github.com/mocktools/go-smtp-mock/blob/d29f933cc7f5f26966000fb5e2fe5cca8f1c165a/consts.go#L56

Also we need to update handlerMailfrom and handlerRcptto dependent methods:

  1. handlerMailfrom#mailfromEmail, composite regex validMailromComplexCmdRegexPattern:

https://github.com/mocktools/go-smtp-mock/blob/d29f933cc7f5f26966000fb5e2fe5cca8f1c165a/consts.go#L68

https://github.com/mocktools/go-smtp-mock/blob/d29f933cc7f5f26966000fb5e2fe5cca8f1c165a/handler_mailfrom_test.go#L218

  1. handlerRcptto#rcpttoEmail, composite regex validRcpttoComplexCmdRegexPattern:

https://github.com/mocktools/go-smtp-mock/blob/d29f933cc7f5f26966000fb5e2fe5cca8f1c165a/consts.go#L69

https://github.com/mocktools/go-smtp-mock/blob/d29f933cc7f5f26966000fb5e2fe5cca8f1c165a/handler_rcptto_test.go#L319