Learn a scripting language, just in order to expand their own path and try to solve the problem more convenient channels, not to make a living, said to be interested also can not, let me for the code dance This life, all my life!
The first PYTHON3 blog, still pretty excited, write bad place please leave a comment, humbly accept everyone's opinion.
Design
工欲善其事, its prerequisite, learn python also need a tiny minute of preparation: Find a good teacher, I chose Liao Xuefeng python tutorial, written very vivid and detailed; Install the appropriate version of Python, like the avant-garde students directly the latest Python can be, Do not love to toss, please choose python2.x version; Programming tools do not care, Pycharm will only define variables can not play out the power AH (it seems that I, the beginning of learning to engage in pycharm and Webstorm).
code based on the python3.4.1 version, Feast begins: Source greeting you sir.
1ImportSmtplib, email
2 fromEmail.mime.textImportMimetext
3MSG2 = Mimetext ('Hello, send first email','Plain','Utf-8')
4FROM_ADDR = input ('From :')
5Password = input ('Password:')
6Smtp_server = input ('SMTP Server:')
7TO_ADDR = input ('To :')
8Server = Smtplib. SMTP (Smtp_server, 25)
9 Server.set_debuglevel (1)
TenServer.connect (Smtp_server, 25)
OneServer.helo ()
AServer.ehlo ()
-Server.login (from_addr, password)
-Server.sendmail (FROM_ADDR, [to_addr], msg2.as_string ())
theServer.quit ()
Detailed implementation
Send protocol as SMTP, dependent on two libraries for smtplib and email
Mimetext is used to create text-type messages, with the following parameters: Body, subtype, encoding format. The second parameter may be confusing, what is the subtype? The default fill plain, the third parameter according to the official manual explanation only ' ASCII ' and ' utf-8 ' two formats, do not understand, the character set should not have many kinds? Gb2312/en_us. Utf8/en_zh. UTF8 and Unicode, among other formats.
To the end of the connection SMTP need to fill in: User name, password, smtpserver name (QQ SMTP is smtp.qq.com), recipient's e-mail address, from the input obtained.
SMTP send process: Initialize SMTP object, connect, helo, EHLO (Helo and EHLO are optional content in SMTP protocol, provide check before login, QQ request to send this message), login, SendMail, quit. The basic principle is to verify the identity after the connection is established, log in and send the message, and send out the quit.
Script implementation and function are very simple, provide everyone python3.x version of the message sent reference, I can not brazen continue to cheat words.
"PYTHON3" SMTP Send mail