yagmail icon indicating copy to clipboard operation
yagmail copied to clipboard

If connection is closed there is no sense to retry without corrective action

Open dimashi opened this issue 7 years ago • 5 comments

I created connection obj, send email and kept connection obj long enough, so connection was probably closed by server. When this object was used to send email, I saw output in console "[yagmail] connection closed, call connect()" and this message appeared 3 times. yagmail has retry, which is nice, but for this type of error there is no sense to retry without reconnecting, or if user/password are not available for reconnect, it should fail after first attempt.

dimashi avatar Mar 11 '17 00:03 dimashi

similar situation here:

client = yagmail.SMTP(...)
# keep it long enough
client.send(...)

# 2017-03-16 14:27:36 [yagmail] [ERROR] : Connection unexpectedly closed
# 2017-03-16 14:27:39 [yagmail] [ERROR] : please run connect() first
# 2017-03-16 14:27:45 [yagmail] [ERROR] : please run connect() first

any suggestion?

DeoLeung avatar Mar 16 '17 06:03 DeoLeung

Hey guys, I'm sorry for the slow response. Indeed, I did not run into this situation myself.

One thing you could do is connect yourself, and run client.send_unsent().

Just to make sure, what would be the way you would provide the password the second time?

kootenpv avatar Mar 16 '17 08:03 kootenpv

@kootenpv It's okay to provide the password in my case

but I don't see this library has a connect method, do you mean login?

also the retry logic seems to already suppress the real failing reason. does checking the return values of send to be False and re-connect sufficient? https://github.com/kootenpv/yagmail/blob/master/yagmail/yagmail.py#L121

DeoLeung avatar Mar 16 '17 08:03 DeoLeung

This is what I did: create client object once and keep it, because I want to keep unsent emails to resend in my send_email function: client.login(password) # I assume if password is None it will read it from keychain client.send client.close()

Unfortunately I cannot use "with yagmail.SMTMP(..) as client:" because it will delete whole client with its unsent list.

I would propose the following model:

client - long lived object with unsent list. It does not represent connection new object connection/session:

with client.connect(...) as connected_session: connected_session.send(....) # sends new email and tried to resend unsent emails.

dimashi avatar Mar 16 '17 13:03 dimashi

@dimashi that would be superb. I encountered the same problem in my production environment. Your solution seems to be fairly suitable.

voidbar avatar Jun 19 '17 19:06 voidbar