Python emails and python emails
1 #-*-coding: UTF-8-*-2 ''' 3 Send txt text mail 4 http://www.cnblogs.com/liu-ke 5 ''' 6 import smtplib 7 from email. mime. text import MIMEText 8 mailto_list = ['***@**. * ** '] 9 mail_host = "smtp. ****. com "# Set Server 10 mail_user = "***@**. ** "# username 11 mail_pass =" ********* "# password 12 mail_postfix = "***. com "# The suffix of the sender is 13 14 def send_mail (to_list, sub, content ): 15 me = "hello" + "<" + mail_user + "@" + mail_postfix + ">" 16 msg = MIMEText (c Ontent, _ subtype = 'plain ', _ charset = 'gb2312 ') 17 msg ['subobject'] = sub 18 msg ['from'] = me 19 msg ['to'] = ";". join (to_list) 20 try: 21 server = smtplib. SMTP () 22 server. connect (mail_host) 23 server. login (mail_user, mail_pass) 24 server. sendmail (me, to_list, msg. as_string () 25 server. close () 26 return True 27 failed t Exception, e: 28 print str (e) 29 return False 30 if _ name _ = '_ main __': 31 if send _ Mail (mailto_list, "hello", "hello world! "): 32 print" sent successfully "33 else: 34 print" failed to send"
1 #-*-coding: UTF-8-*-2 ''' 3 send html text mail 4 http://www.cnblogs.com/liu-ke 5 ''' 6 import smtplib 7 from email. mime. text import MIMEText 8 mailto_list = ["*****"] 9 mail_host = "smtp. ***. com "# Set Server 10 mail_user =" ***** "# username 11 mail_pass =" ***** "# password 12 mail_postfix = "***. com "# The suffix of the sender is 13 14 def send_mail (to_list, sub, content): # to_list: recipient; sub: topic; content: email content 15 me = "hello" + "<" + mail_user + "@" + mail_postfix + ">" # Here, hello can be set as needed. After receiving the email, 16 msg = MIMEText (content, _ subtype = 'html', _ charset = 'gb2312 ') # create an instance according to the settings, here we set the email To html format 17 msg ['subobject'] = sub # Set the topic 18 msg ['from'] = me 19 msg ['to'] = ";". join (to_list) 20 try: 21 s = smtplib. SMTP () 22 s. connect (mail_host) # connect to the smtp server for 23 s. login (mail_user, mail_pass) # log on to the server for 24 s. sendmail (me, to_list, msg. as_string () # Send email for 25 s. close () 26 return True 27 failed t Exception, e: 28 print str (e) 29 return False 30 if _ name _ = '_ main __': 31 if send_mail (mailto_list, "hello", "<a href = 'HTTP: // www.cnblogs.com/xiaowuyi'> Xiao Wuyi </a>"): 32 print "sent successfully ", mailto_list 33 else: 34 print "failed to send"