[Python] python程序发送email的代码 →→→→→进入此内容的聊天室

来自 , 2019-09-01, 写在 Python, 查看 106 次.
URL http://www.code666.cn/view/96ba4a06
  1. #account setup,sharejs.com 提醒您修改成自己的用户名、密码和服务器地址
  2. username = '***';
  3. password = '***';
  4. server = 'smtp.gmail.com:587';
  5.  
  6. #imports
  7. from time import sleep;
  8. import smtplib;
  9. from email.mime.application import MIMEApplication
  10. from email.mime.text import MIMEText;
  11. from email.mime.multipart import MIMEMultipart;
  12.  
  13.  
  14. # create msg - MIME* object
  15. # takes addresses to, from cc and a subject
  16. # returns the MIME* object
  17. def create_msg(to_address,
  18.                from_address='',
  19.                cc_address='',
  20.                bcc_address='',
  21.                subject=''):
  22.    
  23.     msg = MIMEMultipart();
  24.     msg['Subject'] = subject;
  25.     msg['To'] = to_address;
  26.     msg['Cc'] = cc_address;
  27.     msg['From'] = from_address;
  28.     return msg;
  29.  
  30. # sharejs.com send an email
  31. # takes an smtp address, user name, password and MIME* object
  32. # if mode = 0 sends to and cc
  33. # if mode = 1 sends to bcc
  34. def send_email(smtp_address, usr, password, msg, mode):
  35.     server = smtplib.SMTP(smtp_address);
  36.     server.ehlo();
  37.     server.starttls();
  38.     server.ehlo();
  39.     server.login(username,password);
  40.     if (mode == 0 and msg['To'] != ''):
  41.         server.sendmail(msg['From'],(msg['To']+msg['Cc']).split(","), msg.as_string());
  42.     elif (mode == 1 and msg['Bcc'] != ''):
  43.         server.sendmail(msg['From'],msg['Bcc'].split(","),msg.as_string());
  44.     elif (mode != 0 and mode != 1):
  45.         print 'error in send mail bcc'; print 'email cancled'; exit();
  46.     server.quit();
  47.  
  48. # compose email
  49. # takes all the details for an email and sends it
  50. # address format: list, [0] - to
  51. #                       [1] - cc
  52. #                       [2] - bcc
  53. # subject format: string
  54. # body format: list of pairs [0] - text
  55. #                            [1] - type:
  56. #                                        0 - plain
  57. #                                        1 - html
  58. # files is list of strings
  59. def compose_email(addresses, subject, body, files):
  60.  
  61.     # addresses
  62.     to_address = addresses[0];
  63.     cc_address = addresses[1];
  64.     bcc_address = addresses[2];
  65.  
  66.     # create a message
  67.     msg = create_msg(to_address, cc_address=cc_address , subject=subject);
  68.  
  69.     # add text
  70.     for text in body:
  71.         attach_text(msg, text[0], text[1]);
  72.  
  73.     # add files
  74.     if (files != ''):
  75.         file_list = files.split(',');
  76.         for afile in file_list:
  77.             attach_file(msg, afile);
  78.  
  79.     # send message
  80.     send_email(server, username, password, msg, 0);
  81.  
  82.     # check for bcc
  83.     if (bcc_address != ''):
  84.         msg['Bcc'] = bcc_address;
  85.         send_email(server, username, password, msg, 1);
  86.        
  87.     print 'email sent'
  88.  
  89. # attach text
  90. # attaches a plain text or html text to a message
  91. def attach_text(msg, atext, mode):
  92.     part = MIMEText(atext, get_mode(mode));
  93.     msg.attach(part);
  94.  
  95. # util function to get mode type
  96. def get_mode(mode):
  97.     if (mode == 0):
  98.         mode = 'plain';
  99.     elif (mode == 1):
  100.         mode = 'html';
  101.     else:
  102.         print 'error in text kind'; print 'email cancled'; exit();
  103.     return mode;
  104.  
  105. # attach file
  106. # takes the message and a file name and attaches the file to the message
  107. def attach_file(msg, afile):
  108.     part = MIMEApplication(open(afile, "rb").read());
  109.     part.add_header('Content-Disposition', 'attachment', filename=afile);
  110.     msg.attach(part);
  111.  
  112. #to be tested...
  113. compose_email(['cpt@thelivingpearl.com','',''],
  114.               'test v.5.0',
  115.               [['some text goes here...\n',0]],
  116.               '');
  117.              
  118. #compose_email can take the following arguments:
  119. #       1. to recipients (separated by a comma)
  120. #       2. cc recipients (separated by a comma)
  121. #       3. bcc recipients (separated by a comma)
  122. #       4. subject
  123. #       5. a list with message and mode (plain txt or html)
  124. #       6. files to be attached
  125.  
  126. #//python/8978

回复 "python程序发送email的代码"

这儿你可以回复上面这条便签

captcha