欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Linux 发送邮件,抄送+附件,多收件人

shiping1 的头像

随人Linux自带了功能强大的sendmail服务器,但是这样发送的邮件往往被视为垃圾邮件。以下代码可以登录你的邮箱发送邮件,并且可以添加抄送人,添加附件,可以发给多个收件人。其中:

邮件的正文在/app/opt/oracle/mail.txt 中 ,/app/opt/oracle/report.zip是的附件。

 

[c-sharp] view plaincopy
  1. DECLARE  
  2.   pass VARCHAR2(20) := 'Z1234';--????  
  3. BEGIN  
  4.   FOR c IN (select a.username from dba_users a,icaps_user b where 'Z'||b.username=a.username ) LOOP  
  5.     EXECUTE IMMEDIATE 'alter user ' || c.username || ' identified by ' || pass;  
  6.   END LOOP;  
  7. END;  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. #!/usr/bin/env python  
  14. import smtplib,mimetypes  
  15. from email import Encoders  
  16. from email.MIMEBase import MIMEBase  
  17. from email.MIMEText import MIMEText  
  18. from email.MIMEMultipart import MIMEMultipart  
  19. import email  
  20. import time  
  21.   
  22. femail='xxx@xxxx.com'  
  23. temail =['yyyyy@yyyy.com','sssss@ssss.com']  
  24. ccmail=['zzzzz@zzzzz.com','ggggg@ggggg.com']  
  25.   
  26. to_string =''  
  27. for item in temail:  
  28.     to_string += item +','  
  29.   
  30.   
  31. cc_string =''  
  32. for item1 in ccmail:  
  33.     cc_string += item1 +','  
  34.   
  35. msg=MIMEMultipart()  
  36. msg['From'] = femail  
  37. msg['To'] = to_string  
  38. subject="Cash flow reports for all hotels"  
  39. f = file('/app/opt/oracle/mail.txt')  
  40. # if no mode is specified, 'r'ead mode is assumed by default  
  41. #while True:  
  42. #    line = f.readline()  
  43. #    if len(line) == 0: # Zero length indicates EOF  
  44. #        break  
  45. #    content=line  
  46. #    print line,  
  47.  
  48.     # Notice comma to avoid automatic newline added by Python  
  49. content=f.read()  
  50. f.close() # close the file   
  51. #print content  
  52. msg['Subject'] =  subject  
  53. msg['Reply-To'] = femail  
  54. #msg['Cc'] = cc_string  
  55. msg['Date'] = time.ctime(time.time())  
  56.   
  57. msg['X-Priority'] =  '''3'''  
  58. msg['X-MSMail-Priority'] =  '''Normal'''  
  59. print content  
  60. dfff="du -h"  
  61. #body=email.MIMEText.MIMEText('''"dd"+content"dd"''',_subtype='html',_charset='UTF-8')  
  62. #msg.attach(body)  
  63. msgText=MIMEText(content,'plain','')  
  64. msg.attach(msgText)  
  65.   
  66. filename = r'/app/opt/oracle/report.zip'  
  67. fp = open(filename,'rb')  
  68. ctype,encoding = mimetypes.guess_type(filename)  
  69. if ctype is None or encoding is not None:  
  70.         ctype = 'application/octet-stream'  
  71. maintype,subtype = ctype.split('/',1)  
  72. m = MIMEBase(maintype,subtype)  
  73. m.set_payload(fp.read())  
  74. fp.close()  
  75. Encoders.encode_base64(m)  
  76. m.add_header('Content-disposition','attachment',filename='report.zip')    
  77. msg.attach(m)                                                       
  78.   
  79. s = smtplib.SMTP('mail.e5systems.com')  
  80. #Sing in email with password  
  81. s.login(femail,'xxxxxxx')  
  82. s.sendmail(femail,temail,msg.as_string())  
  83. print 'Mailing...'  
  84. s.close()  

来自 http://blog.csdn.net/honglei915/article/details/6447401

普通分类: