Sending mail from Python using SMTP

后端 未结 13 825
半阙折子戏
半阙折子戏 2020-11-28 17:27

I\'m using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I\'m missing ?

from smtplib import SM         


        
13条回答
  •  感情败类
    2020-11-28 18:13

    See all those lenghty answers? Please allow me to self promote by doing it all in a couple of lines.

    Import and Connect:

    import yagmail
    yag = yagmail.SMTP('john@doe.net', host = 'YOUR.MAIL.SERVER', port = 26)
    

    Then it is just a one-liner:

    yag.send('foo@bar.com', 'hello', 'Hello\nThis is a mail from your server\n\nBye\n')
    

    It will actually close when it goes out of scope (or can be closed manually). Furthermore, it will allow you to register your username in your keyring such that you do not have to write out your password in your script (it really bothered me prior to writing yagmail!)

    For the package/installation, tips and tricks please look at git or pip, available for both Python 2 and 3.

提交回复
热议问题