Python 3.4 send mail,pythonsend

來源:互聯網
上載者:User

Python 3.4 send mail,pythonsend

#coding=utf-8#Python 3.4  https://docs.python.org/3.4/library/#IDE:Visual Studio 2015  Window10import atexitimport osimport unicodedataimport sysimport timeimport unicodedataimport winsoundimport codeimport codecsimport mathimport csvimport base64import reimport smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipartfrom email.header import Header  from email.mime.image import MIMEImagefrom email.mime.base import MIMEBase  from email.utils import COMMASPACE, formatdate  import emailimport os.path  import sys   import mimetypes   import configparser import string  #https://docs.python.org/3.4/library/email.html#https://docs.python.org/3/library/email-examples.htmlinifile='F:/Python/055.JPG'  #config=ConfigParser.ConfigParser()  #config.read(inifile)  #os.remove(inifile) #移除檔案 subject=Header("締友電腦有限公司銷售報告","utf-8")  #郵件標題ReplyToName="geovindu@dusystem.com"  ReplyToMail="geovindu@dusystem.com"  To="463588883@qq.com" bcc="geovindu@163.com"; cc="geovindu@jw28.com";COMMASPACE = ', ';file_name=inifile #"file_name"  From = "%s<geovindu@dusystem.com>" % Header("締友電腦資訊有公司塗聚文","utf-8")  #寄件者和姓名server = smtplib.SMTP("mail.dusystem.com",25)  server.login("geovindu@dusystem.com","888") #僅smtp伺服器需要驗證時    # 構造MIMEMultipart對象做為根容器  main_msg =  MIMEMultipart("alternative");  #alternative   related  2.x版本的email.MIMEMultipart.MIMEMultipart()# 構造MIMEText對象做為郵件顯示內容並附加到根容器  text_msg =MIMEText("xxx幫你轉寄的郵件", _subtype='html',_charset="utf-8")  #郵件內容  2.x版本的 email.MIMEText.MIMETextmain_msg.attach(text_msg)  # 構造MIMEBase對象做為檔案附件內容並附加到根容器  #圖片未顯示,附件不同#fp = open(file_name, 'rb')    #msgImage = MIMEImage(fp.read())    #fp.close()        #msgImage.add_header('Content-ID', '<image1>')    #main_msg.attach(msgImage)     ## 設定附件頭  #basename = os.path.basename(file_name)  #file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改郵件標頭  file_msg = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')    #發檔案file_msg["Content-Type"] = 'application/octet-stream'    file_msg["Content-Disposition"] = 'attachment; filename="055.JPG"' main_msg.attach(file_msg)  # 設定根容器屬性  main_msg['From'] = From  if ReplyToMail!='none':      main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)  main_msg['To'] = To;  main_msg['Subject'] = subject;  main_msg['Cc']=cc;main_msg['Bcc'] = bcc;  #這無效main_msg['Date'] =formatdate(localtime=True) # 得到格式化後的完整文本  fullText = main_msg.as_string()  # 用smtp發送郵件  try:      server.sendmail(From, To.split(';'), fullText)  finally:      server.quit()      #os.remove(file_name)  print("發送郵件成功");

  上發圖片

下為發檔案

#coding=utf-8#Python 3.4  https://docs.python.org/3.4/library/#IDE:Visual Studio 2015  Window10import atexit #類庫import osimport unicodedataimport sysimport timeimport unicodedataimport winsoundimport codeimport codecsimport mathimport csvimport base64import reimport smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipartfrom email.header import Header  from email.mime.image import MIMEImagefrom email.mime.base import MIMEBase  from email.encoders import encode_base64from email.mime.application import MIMEApplication from email.utils import COMMASPACE, formatdate  import emailimport os.path  import sys   import mimetypes   import configparser import string  import mimetypesimport base64;#https://docs.python.org/3.4/library/email.html#https://docs.python.org/3/library/email-examples.htmlinifile=u'F:/Python/工應用文寫作.doc'  #config=ConfigParser.ConfigParser()  #config.read(inifile)  #os.remove(inifile) #移除檔案 subject=Header("締友電腦有限公司銷售報告","utf-8")  #郵件標題ReplyToName="geovindu@dusystem.com"  ReplyToMail="geovindu@dusystem.com"  To="463588883@qq.com" bcc="geovindu@163.com"; cc="geovindu@jw28.com";COMMASPACE = ', ';file_name=inifile #"file_name"  From = "%s<geovindu@dusystem.com>" % Header("締友電腦資訊有公司塗聚文","utf-8")  #寄件者和姓名server = smtplib.SMTP("mail.dusystem.com",25)  server.login("geovindu@dusystem.com","888") #僅smtp伺服器需要驗證時    # 構造MIMEMultipart對象做為根容器  main_msg =  MIMEMultipart("alternative");  #alternative   related  2.x版本的email.MIMEMultipart.MIMEMultipart()# 構造MIMEText對象做為郵件顯示內容並附加到根容器  text_msg =MIMEText("請你及閱完文檔,提出建議", _subtype='html',_charset="utf-8")  #郵件內容  2.x版本的 email.MIMEText.MIMETextmain_msg.attach(text_msg)  # 構造MIMEBase對象做為檔案附件內容並附加到根容器  #圖片未顯示,附件不同#fp = open(file_name, 'rb')    #msgImage = MIMEImage(fp.read())    #fp.close()        #msgImage.add_header('Content-ID', '<image1>')    #main_msg.attach(msgImage)     ## 設定附件頭  #basename = os.path.basename(file_name)  #file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改郵件標頭  #圖片#file_image="F:/Python/055.jpg";#file_msg = MIMEText(open(file_image, 'rb').read(), 'base64', 'utf-8')    #發完檔案不見了#file_msg["Content-Type"] = 'application/octet-stream'     # 圖片(application/octet-stream)可以,但WORD不行#file_msg["Content-Disposition"] = 'attachment; filename="055.jpg"' #1 可以檔案名稱亂碼#contentType, encoding = mimetypes.guess_type(file_name)#mainType, subType = contentType.split('/', 1)  #file = open(file_name, 'rb')#attachment = MIMEBase(mainType, subType)#attachment.set_payload(file.read())  #encode_base64(attachment)  #file.close()   #basename = os.path.basename(file_name);#attachment.add_header('Content-Disposition', 'attachment',filename=basename)#attachment.add_header('Content-Disposition', 'attachment',filename=os.path.basename(file_name))  #file_name #2中檔案名稱亂碼 #file_msg = MIMEApplication(open(file_name, 'rb').read())    #發檔案  #file_msg.add_header('Content-Disposition', 'attachment', filename="應用文寫作.doc")  #main_msg.attach(file_msg)  #file_msg#3注意:指定att的編碼basename = os.path.basename(file_name) att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')  att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename=%s' % basename.encode("utf-8") main_msg.attach(att)  # 設定根容器屬性  main_msg['From'] = From  if ReplyToMail!='none':      main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)  main_msg['To'] = To;  main_msg['Subject'] = subject;  main_msg['Cc']=cc;main_msg['Bcc'] = bcc;  #這無效main_msg['Date'] =formatdate(localtime=True) # 得到格式化後的完整文本  fullText = main_msg.as_string()  # 用smtp發送郵件  try:      server.sendmail(From, To.split(';'), fullText)  finally:      server.quit()      #os.remove(file_name)  移除檔案print("發送郵件成功");

  

#4.中文無亂碼file_name=u'F:/Python/應用文寫作.doc'urlfilename="應用文寫作.doc"att = MIMEBase('application', 'octet-stream')  att.set_payload(open(file_name, 'rb').read())  att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', urlfilename) )  #gbk 收件時,名稱不亂碼。encode_base64(att) main_msg.attach(att) 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.