func (email *Email) Send(client *SMTPClient) not goroutine safe
rongfengliang opened this issue · 3 comments
i do some test with keepalive,but got some wrong (smtp server with mailhog )
2020/11/22 01:42:01 [SMTP 172.21.0.1:40294] [PROTO: RCPT] In RCPT state
2020/11/22 01:42:01 [SMTP 172.21.0.1:40294] [PROTO: RCPT] Got unknown command for RCPT state: '&{MAIL FROM:<dalong@qq.com> MAIL FROM:<dalong@qq.com>}'
2020/11/22 01:42:01 [SMTP 172.21.0.1:40294] Sent 26 bytes: '500 Unrecognised command\r\n'
2020/11/22 01:42:01 [SMTP 172.21.0.1:40294] Received 31 bytes: 'RCPT TO:<dalong@qq.com>\r\nRSET\r\n'
some links https://github.com/rongfengliang/golang-email-learning/blob/master/main.go#L15
The keep-alive implementation is not safe. You need to make your program safe.
A suggestion is open a connection, and use a goroutine to send a NOOP command every X seconds to avoid disconnection from server.
Commands to SMTP server is one command at time by connection, so use the NOOP and send emails if you are sure the client is not used by another goroutine. You can reset the time for NOOP if you send an email.
Also. If you use the keep-alive implementation and you not send NOOP or emails in a short time, the server will disconnect you, and you need to reconnect again before send an email.
@xhit tks!