Replace Session.ConnectSMTP with Session.DoSMTP
This gives more flexibility in Session for optimizations, e.g. keep the SMTP connection around for some time if possible.
This commit is contained in:
parent
d01c85616a
commit
622f00fe06
2 changed files with 19 additions and 16 deletions
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/emersion/go-imap"
|
"github.com/emersion/go-imap"
|
||||||
imapclient "github.com/emersion/go-imap/client"
|
imapclient "github.com/emersion/go-imap/client"
|
||||||
"github.com/emersion/go-message"
|
"github.com/emersion/go-message"
|
||||||
|
"github.com/emersion/go-smtp"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -257,7 +258,9 @@ func handleCompose(ectx echo.Context) error {
|
||||||
msg.Text = ctx.FormValue("text")
|
msg.Text = ctx.FormValue("text")
|
||||||
msg.InReplyTo = ctx.FormValue("in_reply_to")
|
msg.InReplyTo = ctx.FormValue("in_reply_to")
|
||||||
|
|
||||||
c, err := ctx.Session.ConnectSMTP()
|
err := ctx.Session.DoSMTP(func(c *smtp.Client) error {
|
||||||
|
return sendMessage(c, &msg)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(koushin.AuthError); ok {
|
if _, ok := err.(koushin.AuthError); ok {
|
||||||
return echo.NewHTTPError(http.StatusForbidden, err)
|
return echo.NewHTTPError(http.StatusForbidden, err)
|
||||||
|
@ -265,14 +268,6 @@ func handleCompose(ectx echo.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := sendMessage(c, &msg); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.Quit(); err != nil {
|
|
||||||
return fmt.Errorf("QUIT failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: append to IMAP Sent mailbox
|
// TODO: append to IMAP Sent mailbox
|
||||||
|
|
||||||
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
|
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
|
||||||
|
|
22
session.go
22
session.go
|
@ -76,21 +76,29 @@ func (s *Session) DoIMAP(f func(*imapclient.Client) error) error {
|
||||||
return f(s.imapConn)
|
return f(s.imapConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnectSMTP connects to the upstream SMTP server and authenticates this
|
// DoSMTP executes an SMTP operation on this session. The SMTP client can only
|
||||||
// session.
|
// be used from inside f.
|
||||||
func (s *Session) ConnectSMTP() (*smtp.Client, error) {
|
func (s *Session) DoSMTP(f func(*smtp.Client) error) error {
|
||||||
c, err := s.manager.dialSMTP()
|
c, err := s.manager.dialSMTP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
auth := sasl.NewPlainClient("", s.username, s.password)
|
auth := sasl.NewPlainClient("", s.username, s.password)
|
||||||
if err := c.Auth(auth); err != nil {
|
if err := c.Auth(auth); err != nil {
|
||||||
c.Close()
|
return AuthError{err}
|
||||||
return nil, AuthError{err}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c, nil
|
if err := f(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Quit(); err != nil {
|
||||||
|
return fmt.Errorf("QUIT failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close destroys the session. This can be used to log the user out.
|
// Close destroys the session. This can be used to log the user out.
|
||||||
|
|
Loading…
Reference in a new issue