python: sending a mail, fails when inside a “with” block
问题 I am wondering why this code test = smtplib.SMTP('smtp.gmail.com', 587) test.ehlo() test.starttls() test.ehlo() test.login('address','passw') test.sendmail(sender, recipients, composed) test.close() works, but when written like this with smtplib.SMTP('smtp.gmail.com', 587) as s: s.ehlo() s.starttls() s.ehlo() s.login('address','passw') s.sendmail(sender, recipients, composed) s.close() it fails with the message Unable to send the email. Error: <class 'AttributeError'> Traceback (most recent