aboutsummaryrefslogtreecommitdiffstats
path: root/session.go
diff options
context:
space:
mode:
authorLibravatarLibravatar Simon Ser <[email protected]> 2019-12-16 13:07:35 +0100
committerLibravatarLibravatar Simon Ser <[email protected]> 2019-12-16 13:07:35 +0100
commit622f00fe063c454b4080ad47e8732bd0e9848e82 (patch)
tree85f2eea7329f867ca359d4f9513ab2cd6789ca46 /session.go
parentd01c85616a5de48896910de75c6a4533c6e71015 (diff)
downloadalps-622f00fe063c454b4080ad47e8732bd0e9848e82.tar.gz
alps-622f00fe063c454b4080ad47e8732bd0e9848e82.tar.bz2
alps-622f00fe063c454b4080ad47e8732bd0e9848e82.zip
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.
Diffstat (limited to 'session.go')
-rw-r--r--session.go22
1 files changed, 15 insertions, 7 deletions
diff --git a/session.go b/session.go
index d75be10..f89c785 100644
--- a/session.go
+++ b/session.go
@@ -76,21 +76,29 @@ func (s *Session) DoIMAP(f func(*imapclient.Client) error) error {
return f(s.imapConn)
}
-// ConnectSMTP connects to the upstream SMTP server and authenticates this
-// session.
-func (s *Session) ConnectSMTP() (*smtp.Client, error) {
+// DoSMTP executes an SMTP operation on this session. The SMTP client can only
+// be used from inside f.
+func (s *Session) DoSMTP(f func(*smtp.Client) error) error {
c, err := s.manager.dialSMTP()
if err != nil {
- return nil, err
+ return err
}
+ defer c.Close()
auth := sasl.NewPlainClient("", s.username, s.password)
if err := c.Auth(auth); err != nil {
- c.Close()
- return nil, AuthError{err}
+ return 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.