SHARE
TWEET

Untitled

a guest Nov 6th, 2018 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.application import MIMEApplication
  6.  
  7.  
  8. def sendmail(smtp_server, port, username, password, from_addr, to_addr, subject, content, content_type='plain', cc_addr=None, bcc_addr=None, attachment=None):
  9.     """
  10.     smtp_sever: 填SMTP服务器地址,string
  11.     port: 填服务器端口,int
  12.     username: 邮箱登录名,string
  13.     password: 邮箱登录密码,string
  14.     from_addr:发件人地址,只能填一个地址,string
  15.     to_addr: 收件人地址,可多个,string、list、tuple均可
  16.     subject: 邮件主题,string
  17.     content: 邮件正文,string
  18.     content_type: 邮件正文类型,只有两个选择:plain(默认)或html。string
  19.     cc_addr: 抄送人地址,可多个,string、list、tuple均可,默认为None
  20.     bcc_addr: 密送人地址,可多个,string、list、tuple均可,默认为None
  21.     attachment: 邮件附件路径,可多个,string、list、tuple均可,默认为None
  22.     """
  23.  
  24.     msg = MIMEMultipart()
  25.  
  26.     # 处理发件人
  27.     from_show_addr = from_addr
  28.     from_addr = str(from_addr)
  29.     msg['From'] = from_show_addr
  30.  
  31.     # 处理收件人
  32.     if isinstance(to_addr, list):
  33.         to_show_addr = ','.join(to_addr)
  34.         to_addr = to_addr
  35.     elif isinstance(to_addr, tuple):
  36.         to_show_addr = ','.join(to_addr)
  37.         to_addr = list(to_addr)
  38.     elif isinstance(to_addr, str):
  39.         if ',' in to_addr:
  40.             to_addr.replace(' ', '')
  41.             to_show_addr = to_addr
  42.             to_addr = to_addr.split(',')
  43.         else:
  44.             to_show_addr = to_addr
  45.             to_addr = [to_addr]
  46.     msg['To'] = to_show_addr
  47.  
  48.     # 处理标题
  49.     subject = str(subject)
  50.     msg['Subject'] = subject
  51.  
  52.     # 处理抄送人
  53.     if cc_addr is not None:
  54.         if isinstance(cc_addr, list):
  55.             cc_show_addr = ','.join(cc_addr)
  56.             to_addr = to_addr + cc_addr
  57.         elif isinstance(cc_addr, tuple):
  58.             cc_show_addr = ','.join(cc_addr)
  59.             to_addr = to_addr + list(cc_addr)
  60.         elif isinstance(cc_addr, str):
  61.             if ',' in cc_addr:
  62.                 cc_addr.replace(' ', '')
  63.                 cc_show_addr = cc_addr
  64.                 cc_addr = cc_addr.split(',')
  65.                 to_addr = to_addr + cc_addr
  66.             else:
  67.                 cc_show_addr = cc_addr
  68.                 to_addr.append(cc_addr)
  69.         msg['Cc'] = cc_show_addr
  70.  
  71.     # 处理密送人
  72.     if bcc_addr is not None:
  73.         if isinstance(bcc_addr, list):
  74.             to_addr = to_addr + bcc_addr
  75.         elif isinstance(bcc_addr, tuple):
  76.             to_addr = to_addr + list(bcc_addr)
  77.         elif isinstance(bcc_addr, str):
  78.             if ',' in bcc_addr:
  79.                 bcc_addr.replace(' ', '')
  80.                 bcc_addr = bcc_addr.split(',')
  81.                 to_addr = to_addr + bcc_addr
  82.             else:
  83.                 to_addr.append(bcc_addr)
  84.  
  85.     # 处理正文
  86.     if content_type.lower() == 'plain':
  87.         msg.attach(MIMEText(content, 'plain', 'UTF-8'))
  88.     elif content_type.lower() == 'html':
  89.         msg.attach(MIMEText(content, 'html', 'UTF-8'))
  90.  
  91.     # 处理附件
  92.     if attachment is not None:
  93.         if isinstance(attachment, list) or isinstance(attachment, tuple):
  94.             for each_file in attachment:
  95.                 if os.path.isfile(each_file):
  96.                     with open(each_file, 'rb') as f:
  97.                         part = MIMEApplication(f.read(), Name=os.path.basename(each_file))
  98.                     part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(each_file)
  99.                     msg.attach(part)
  100.         elif isinstance(attachment, str):
  101.             if os.path.isfile(attachment):
  102.                 with open(attachment, 'rb') as f:
  103.                     part = MIMEApplication(f.read(), Name=os.path.basename(attachment))
  104.                 part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(attachment)
  105.                 msg.attach(part)
  106.  
  107.     server = smtplib.SMTP(smtp_server, int(port))
  108.     server.starttls()
  109.     server.login(username, password)
  110.     server.sendmail(from_addr, to_addr, msg.as_string())
  111.     print('Sent E-mail successfully!')
  112.     server.quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top