New to Python, GMail SMTP error

前端 未结 2 1774
逝去的感伤
逝去的感伤 2021-02-07 08:28

I am writing a simple sendmail function to myself and I keep getting this error:

NameError: name \'SMTPException\' is not defined

What is wrong with my code? Any

2条回答
  •  时光取名叫无心
    2021-02-07 08:51

    That misspelling occurred many times to me as well! One way to circumvent this "problem", is to use yagmail.

    Jokes aside, I recently created yagmail to make it easier to send emails.

    For example:

    import yagmail
    yag = yagmail.SMTP('user@gmail.com', 'password')
    yag.send(contents = "Hello!")
    

    It uses several shortenings here, for example when To is not defined, it will send a mail to the same email who registered on the server. Also the port and host are the default, which makes it very concise.

    In fact, since it seems you want to close the connection immediately, you can even use this one-liner:

    yagmail.SMTP('user@gmail.com', 'password').send(contents = "Hello!")
    

    For security, you can keep your password in the keyring (see documentation) such that you do not have to keep your personal password in your scripts, very important! It'll even save you more precious screen-estate.

    Going all-in with the package (@gmail.com is default), you can get away with the following:

    yagmail.SMTP('user').send('', 'Hello!')
    

    Good luck.

提交回复
热议问题