I am trying to programming an authentication system with golang.
After user successfully sign up, it will send an email confirmation to the user.
I am thinking to execute the send mail function in a goroutine. The code of send mail function looks like
func Send(email, link string) error {
// Connect to the remote SMTP server.
c, err := smtp.Dial("127.0.0.1:2525")
if err != nil {
return err
}
// Set the sender and recipient.
c.Mail("sender@example.org")
c.Rcpt(email)
// Send the email body.
wc, err := c.Data()
if err != nil {
return err
}
defer wc.Close()
buf := bytes.NewBufferString("Please confirm this email " + link)
if _, err = buf.WriteTo(wc); err != nil {
return err
}
return nil
}
If here an error is going to occur in the goroutine and response is already done(the user receive the response), how can I then handle the error?
I'm assuming:
If this is the case:
You should probably record the failure or success of this send mail operation to some kind of stateful store (SQL DB, mongo, leveldb, etc), then when a user logs in (or navigates to a page) you can notify them their account still needs to be confirmed (assuming either the send operation OR the URL clicking the confirmation link never happened) and offer to resend the e-mail.
Another option would be to make response to submitting the e-mail address wait for the e-mail to be sent (don't start a goroutine), but that might not scale well if you have many signups.. AND you would still need to maintain the state of who as clicked the activation link anyway. So the first option is better.