kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

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

Open dimashi opened 7 years ago

dimashi commented 7 years ago

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.

DeoLeung commented 7 years ago

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?

kootenpv commented 7 years ago

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?

DeoLeung commented 7 years ago

@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

dimashi commented 7 years ago

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.

voidbar commented 7 years ago

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