Some self-promotion here, but I feel on a valid ground.
You would literally only need this code to do exactly what you wrote:
import yagmail
yag = yagmail.SMTP('[email protected]')
yag.send('[email protected]', subject = None, contents = 'Hello')
Or a one liner:
yagmail.SMTP('[email protected]').send('[email protected]', None, 'Hello world.')
What is nice is that I propose to use keyring to store your password, so you never have a risk of people seeing your password in your script.
You can set this up by running once in your interpreter:
import yagmail
yagmail.register("[email protected]", "mypassword")
and exit. Then you can just use:
import yagmail
yagmail.SMTP("[email protected]") # without password
If you add .yagmail with "[email protected]" in your home dir, then you can just do: yagmail.SMTP()
, but that's rather pointless by now.
Warning: If you get serious about sending a lot of messages, better set up OAuth2, yagmail can help with that.
yagmail.SMTP("[email protected]", oauth2_file="/path/to/save/creds.json")
The first time ran, it will guide you through the process of getting OAuth2 credentials and store them in the file so that next time you don't need to do anything with it.
Do you suspect someone found your credentials? They'll have limited permissions, but you better invalidate their credentials through gmail.
For the package/installation please look at git or readthedocs, available for both Python 2 and 3.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…