• PHP调用Python快速发送高并发邮件


    1 简介

           在PHP中发送邮件,通常都是封装一个php的smtp邮件类来发送邮件。但是PHP底层的socket编程相对于Python来说效率是非常低的。CleverCode同时写过用python写的爬虫抓取网页,和用php写的爬虫抓取网页。发现虽然用了php的curl抓取网页,但是涉及到超时,多线程同时抓取等等。不得不说python在网络编程的效率要比PHP好的多。

         PHP在发送邮件时候,自己写的smtp类,发送的效率和速度都比较低。特别是并发发送大量带有附件报表的邮件的时候。php的效率很低。建议可以使用php调用python的方式来发送邮件。

    2 程序

    2.1 python程序

          php的程序和python的文件必须是相同的编码。如都是gbk编号,或者同时utf-8编码,否则容易出现乱码。python发送邮件主要使用了email模块。这里python文件和php文件都是gbk编码,发送的邮件标题内容与正文内容也是gbk编码。

    [python] view plain copy
     
    1. #!/usr/bin/python  
    2. # -*- coding:gbk -*-   
    3. """ 
    4.    邮件发送类 
    5. """  
    6. # mail.py  
    7. #  
    8. # Copyright (c) 2014 by http://blog.csdn.net/CleverCode  
    9. #  
    10. # modification history:  
    11. # --------------------  
    12. # 2014/8/15, by CleverCode, Create  
    13.   
    14. import threading  
    15. import time  
    16. import random  
    17. from email.MIMEText import MIMEText  
    18. from email.MIMEMultipart import MIMEMultipart  
    19. from email.MIMEBase import MIMEBase  
    20. from email import Utils, Encoders  
    21. import mimetypes  
    22. import sys  
    23. import smtplib  
    24. import socket  
    25. import getopt  
    26. import os  
    27.   
    28. class SendMail:  
    29.     def __init__(self,smtpServer,username,password):  
    30.         """ 
    31.         smtpServer:smtp服务器,         
    32.         username:登录名, 
    33.         password:登录密码 
    34.         """  
    35.         self.smtpServer = smtpServer          
    36.         self.username = username  
    37.         self.password = password  
    38.       
    39.     def genMsgInfo(self,fromAddress,toAddress,subject,content,fileList,  
    40.             subtype = 'plain',charset = 'gb2312'):  
    41.         """ 
    42.         组合消息发送包 
    43.         fromAddress:发件人, 
    44.         toAddress:收件人, 
    45.         subject:标题, 
    46.         content:正文, 
    47.         fileList:附件, 
    48.         subtype:plain或者html 
    49.         charset:编码 
    50.         """  
    51.         msg = MIMEMultipart()  
    52.         msg['From'] = fromAddress  
    53.         msg['To'] = toAddress    
    54.         msg['Date'] = Utils.formatdate(localtime=1)  
    55.         msg['Message-ID'] = Utils.make_msgid()  
    56.           
    57.         #标题  
    58.         if subject:  
    59.             msg['Subject'] = subject  
    60.           
    61.         #内容          
    62.         if content:  
    63.             body = MIMEText(content,subtype,charset)  
    64.             msg.attach(body)  
    65.           
    66.         #附件  
    67.         if fileList:  
    68.             listArr = fileList.split(',')  
    69.             for item in listArr:  
    70.                   
    71.                 #文件是否存在  
    72.                 if os.path.isfile(item) == False:  
    73.                     continue  
    74.                       
    75.                 att = MIMEText(open(item).read(), 'base64', 'gb2312')  
    76.                 att["Content-Type"] = 'application/octet-stream'  
    77.                 #这里的filename邮件中显示什么名字  
    78.                 filename = os.path.basename(item)   
    79.                 att["Content-Disposition"] = 'attachment; filename=' + filename  
    80.                 msg.attach(att)  
    81.           
    82.         return msg.as_string()                  
    83.                                                     
    84.     def send(self,fromAddress,toAddress,subject = None,content = None,fileList = None,  
    85.             subtype = 'plain',charset = 'gb2312'):  
    86.         """ 
    87.         邮件发送函数 
    88.         fromAddress:发件人, 
    89.         toAddress:收件人, 
    90.         subject:标题 
    91.         content:正文 
    92.         fileList:附件列表 
    93.         subtype:plain或者html 
    94.         charset:编码 
    95.         """  
    96.         try:  
    97.             server = smtplib.SMTP(self.smtpServer)  
    98.               
    99.             #登录  
    100.             try:  
    101.                 server.login(self.username,self.password)  
    102.             except smtplib.SMTPException,e:  
    103.                 return "ERROR:Authentication failed:",e  
    104.                               
    105.             #发送邮件  
    106.             server.sendmail(fromAddress,toAddress.split(',')   
    107.                 ,self.genMsgInfo(fromAddress,toAddress,subject,content,fileList,subtype,charset))  
    108.               
    109.             #退出  
    110.             server.quit()  
    111.         except (socket.gaierror,socket.error,socket.herror,smtplib.SMTPException),e:  
    112.             return "ERROR:Your mail send failed!",e  
    113.              
    114.         return 'OK'  
    115.   
    116.   
    117. def usage():  
    118.     """ 
    119.     使用帮助 
    120.     """  
    121.     print """Useage:%s [-h] -s <smtpServer> -u <username> -p <password> -f <fromAddress> -t <toAddress>  [-S <subject> -c 
    122.         <content> -F <fileList>] 
    123.         Mandatory arguments to long options are mandatory for short options too. 
    124.             -s, --smtpServer=  smpt.xxx.com. 
    125.             -u, --username=   Login SMTP server username. 
    126.             -p, --password=   Login SMTP server password. 
    127.             -f, --fromAddress=   Sets the name of the "from" person (i.e., the envelope sender of the mail). 
    128.             -t, --toAddress=   Addressee's address. -t "test@test.com,test1@test.com".           
    129.             -S, --subject=  Mail subject. 
    130.             -c, --content=   Mail message.-c "content, ......." 
    131.             -F, --fileList=   Attachment file name.             
    132.             -h, --help   Help documen.     
    133.        """ %sys.argv[0]  
    134.           
    135. def start():  
    136.     """ 
    137.      
    138.     """  
    139.     try:  
    140.         options,args = getopt.getopt(sys.argv[1:],"hs:u:p:f:t:S:c:F:","--help --smtpServer= --username= --password= --fromAddress= --toAddress= --subject= --content= --fileList=",)  
    141.     except getopt.GetoptError:  
    142.         usage()  
    143.         sys.exit(2)  
    144.         return  
    145.       
    146.     smtpServer = None  
    147.     username = None  
    148.     password = None  
    149.                  
    150.     fromAddress = None      
    151.     toAddress = None      
    152.     subject = None  
    153.     content = None  
    154.     fileList = None  
    155.       
    156.     #获取参数     
    157.     for name,value in options:  
    158.         if name in ("-h","--help"):  
    159.             usage()  
    160.             return  
    161.           
    162.         if name in ("-s","--smtpServer"):  
    163.             smtpServer = value  
    164.           
    165.         if name in ("-u","--username"):  
    166.             username = value  
    167.           
    168.         if name in ("-p","--password"):  
    169.             password = value  
    170.           
    171.         if name in ("-f","--fromAddress"):  
    172.             fromAddress = value  
    173.           
    174.         if name in ("-t","--toAddress"):  
    175.             toAddress = value  
    176.           
    177.         if name in ("-S","--subject"):  
    178.             subject = value  
    179.           
    180.         if name in ("-c","--content"):  
    181.             content = value  
    182.           
    183.         if name in ("-F","--fileList"):  
    184.             fileList = value  
    185.       
    186.     if smtpServer == None or username == None or password == None:  
    187.         print 'smtpServer or username or password can not be empty!'  
    188.         sys.exit(3)  
    189.              
    190.     mail = SendMail(smtpServer,username,password)  
    191.       
    192.     ret = mail.send(fromAddress,toAddress,subject,content,fileList)  
    193.     if ret != 'OK':  
    194.         print ret  
    195.         sys.exit(4)  
    196.       
    197.     print 'OK'  
    198.       
    199.     return 'OK'      
    200.        
    201. if __name__ == '__main__':  
    202.       
    203.     start()  
    204.                

    2.2 python程序使用帮助

    输入以下命令,可以输出这个程序的使用帮助
    # python mail.py --help

    2.3 php程序

    这个程序主要是php拼接命令字符串,调用python程序。注意:用程序发送邮件,需要到邮件服务商,开通stmp服务功能。如qq就需要开通smtp功能后,才能用程序发送邮件。开通如下图。
     
    php调用程序如下:
    [php] view plain copy
     
    1. <?php  
    2.   
    3. /** 
    4.  * SendMail.php 
    5.  *  
    6.  * 发送邮件类 
    7.  * 
    8.  * Copyright (c) 2015 by http://blog.csdn.net/CleverCode 
    9.  * 
    10.  * modification history: 
    11.  * -------------------- 
    12.  * 2015/5/18, by CleverCode, Create 
    13.  * 
    14.  */  
    15. class SendMail{  
    16.   
    17.     /** 
    18.      * 发送邮件方法 
    19.      * 
    20.      * @param string $fromAddress 发件人,'clevercode@qq.com' 或者修改发件人名 'CleverCode<clevercode@qq.com>' 
    21.      * @param string $toAddress 收件人,多个收件人逗号分隔,'test1@qq.com,test2@qq.com,test3@qq.com....', 或者 'test1<test1@qq.com>,test2<test2@qq.com>,....' 
    22.      * @param string $subject 标题 
    23.      * @param string $content 正文 
    24.      * @param string $fileList 附件,附件必须是绝对路径,多个附件逗号分隔。'/data/test1.txt,/data/test2.tar.gz,...' 
    25.      * @return string 成功返回'OK',失败返回错误信息 
    26.      */  
    27.     public static function send($fromAddress, $toAddress, $subject = NULL, $content = NULL, $fileList = NULL){  
    28.         if (strlen($fromAddress) < 1 || strlen($toAddress) < 1) {  
    29.             return '$fromAddress or $toAddress can not be empty!';  
    30.         }  
    31.         // smtp服务器  
    32.         $smtpServer = 'smtp.qq.com';  
    33.         // 登录用户  
    34.         $username = 'clevercode@qq.com';  
    35.         // 登录密码  
    36.         $password = '123456';  
    37.           
    38.         // 拼接命令字符串,实际是调用了/home/CleverCode/mail.py  
    39.         $cmd = "LANG=C && /usr/bin/python /home/CleverCode/mail.py";  
    40.         $cmd .= " -s '$smtpServer'";  
    41.         $cmd .= " -u '$username'";  
    42.         $cmd .= " -p '$password'";  
    43.           
    44.         $cmd .= " -f '$fromAddress'";  
    45.         $cmd .= " -t '$toAddress'";  
    46.           
    47.         if (isset($subject) && $subject != NULL) {  
    48.             $cmd .= " -S '$subject'";  
    49.         }  
    50.           
    51.         if (isset($content) && $content != NULL) {  
    52.             $cmd .= " -c '$content'";  
    53.         }  
    54.           
    55.         if (isset($fileList) && $fileList != NULL) {  
    56.             $cmd .= " -F '$fileList'";  
    57.         }  
    58.           
    59.         // 执行命令  
    60.         exec($cmd, $out, $status);  
    61.         if ($status == 0) {  
    62.             return 'OK';  
    63.         } else {  
    64.             return "Error,Send Mail,$fromAddress,$toAddress,$subject,$content,$fileList ";  
    65.         }  
    66.         return 'OK';  
    67.     }  
    68. }  


     

    2.3 使用样例

    压缩excel成附件,发送邮件。
    [php] view plain copy
     
    1. <?php  
    2.   
    3. /** 
    4.  * test.php 
    5.  * 
    6.  * 压缩excel成附件,发送邮件 
    7.  * 
    8.  * Copyright (c) 2015 http://blog.csdn.net/CleverCode 
    9.  * 
    10.  * modification history: 
    11.  * -------------------- 
    12.  * 2015/5/14, by CleverCode, Create 
    13.  * 
    14.  */  
    15. include_once ('SendMail.php');  
    16.   
    17. /* 
    18.  * 客户端类 
    19.  * 让客户端和业务逻辑尽可能的分离,降低页面逻辑和业务逻辑算法的耦合, 
    20.  * 使业务逻辑的算法更具有可移植性 
    21.  */  
    22. class Client{  
    23.   
    24.     public function main(){  
    25.           
    26.         // 发送者  
    27.         $fromAddress = 'CleverCode<clevercode@qq.com>';  
    28.           
    29.         // 接收者  
    30.         $toAddress = 'all@qq.com';  
    31.           
    32.         // 标题  
    33.         $subject = '这里是标题!';  
    34.           
    35.         // 正文  
    36.         $content = "您好: ";  
    37.         $content .= "   这里是正文  ";  
    38.           
    39.         // excel路径  
    40.         $filePath = dirname(__FILE__) . '/excel';  
    41.         $sdate = date('Y-m-d');  
    42.         $PreName = 'CleverCode_' . $sdate;  
    43.           
    44.         // 文件名  
    45.         $fileName = $filePath . '/' . $PreName . '.xls';  
    46.           
    47.         // 压缩excel文件  
    48.         $cmd = "cd $filePath && zip $PreName.zip $PreName.xls";  
    49.         exec($cmd, $out, $status);  
    50.         $fileList = $filePath . '/' . $PreName . '.zip';  
    51.           
    52.         // 发送邮件(附件为压缩后的文件)  
    53.         $ret = SendMail::send($fromAddress, $toAddress, $subject, $content, $fileList);  
    54.         if ($ret != 'OK') {  
    55.             return $ret;  
    56.         }  
    57.           
    58.         return 'OK';  
    59.     }  
    60. }  
    61.   
    62. /** 
    63.  * 程序入口 
    64.  */  
    65. function start(){  
    66.     $client = new Client();  
    67.     $client->main();  
    68. }  
    69.   
    70. start();  
    71.   
    72. ?>  
     

    2.4 程序源码下载

  • 相关阅读:
    使用node 创建一个新项目
    安装node 及相关配置
    Java面试查漏补缺
    科学计算软件——Octave安装
    Wine——在Linux上运行Windows软件
    windows和linux中搭建python集成开发环境IDE——如何设置多个python环境
    Ubuntu安装Gnome3
    ubuntu自定义分辨率
    Ubuntu添加开机自动启动程序方法
    sublime Text3及其插件的使用
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/6600219.html
Copyright © 2020-2023  润新知