Record your first successful Python instance and send mail using a python proxy.
There are three ways, the first two are ordinary text files to send mail, the third is to send mail as an attachment!
The following are specific Python content:
#!/usr/bin/python#-*- coding: utf-8 -*-import smtplib from email.mime.text import mimetext from email.mime.multipart import mimemultipartfrom email.header import header sender = ' [Email protected] ' to1 = ' [email protected] ' to = [' [email Protected] ", " [email protected] "] # of multiple recipients subject = ' From python email test ' smtpserver = ' smtp.126.com ' username = ' Wangyi ' password = ' 123456 ' mail _postfix = ' 126.com ' contents = ' This is a Python test mail, receive do not reply, thank you!! ' attachment_1 = '/tmp/test.txt ' # first Method-Plain text form of mail Def send_mail (to_list, sub, Content): me = "Hello" + "" msg = mimetext (content, _subtype= ' plain ', _charset= ' utf-8 ') msg[' Subject '] = subject msg[' from '] = me msg[' to '] = ";". Join (to_list) try: server = smtplib. SMTP () server.connect (smtpserver) server.login (username, Password) server.sendmail (me, to_list, msg.as_string ()) server.close () return true except exception, e: print str (e) return falseif __name__ == ' __main__ ': if send_mail (to, subject, contents):p rint "Mail sent successfully! " else:print "Failed!!!" # the second Method-mail # msg = mimetext in plain text form (' Message content ', ' _subtype file format ', ' character set ') Chinese required parameter ' utf-8 ', single-byte characters do not require msg = mimetext ( Contents, _subtype= ' plain ', _charset= ' utf-8 ') msg[' Subject '] = subjectmsg[' from '] = sendermsg[' to '] = to1try: smtp = smtplib. SMTP () smtp.connect (smtpserver, 25) smtp.login (Username, password) smtp.sendmail (Sender, to1, msg.as_string ()) Smtp.quit () print "Send successfully!" "except: print " Failed "# third Method-Send attachment # Create an instance with an attachment Msg = mimemultipart () # Construction Attachment 1att1 = mimetext (open (attachment_1, ' RB '). Read (), ' base64 ', ' utf-8 ') att1 ["Content-type"] = ' Application/octet-stream ' att1["content-disposition"] = "Attachment; filename= "Test.txt" #这里的filename是附件的名字可以自定义msg. Attach (ATT1) # plus mail header msg[' from '] = sendermsg[' to '] = to1msg[' Subject '] = subject# send mail try: server = smtplib. SMTP () server.connect (smtpserver) server.login (username, Password) server.sendmail (sender, to1, msg.as_string ()) server.quit () print "sent successfully" except: print "failed"
This article is from "Walker--->" blog, please be sure to keep this source http://liumissyou.blog.51cto.com/4828343/1728392
Python Send mail