search
HomeBackend DevelopmentPHP TutorialSample code on how PHP calls Python to quickly send high-concurrency emails

1 Introduction

When sending emails in PHP, you usually encapsulate a PHP SMTP email class to send emails. However, the underlying socket programming of PHP is very inefficient compared to python. CleverCode has also written a crawler written in python to crawl web pages, and a crawler written in php to crawl web pages. I found that although PHP's curl is used to crawl web pages, it involves timeouts, simultaneous crawling by multiple threads, etc. I have to say that python is much more efficient in network programming than PHP.

When PHP sends emails, the SMTP class written by itself has relatively low sending efficiency and speed. Especially when sending a large number of emails with attached reports concurrently. The efficiency of php is very low. It is recommended to use php to call python to send emails.

2 Program

2.1 python program

The php program and the python file must be in the same encoding. If they are all gbk numbers, or encoded in UTF-8 at the same time, otherwise garbled characters will easily appear. Python mainly uses the email module to send emails. The python files and php files here are all gbk encoded, and the header content and body content of the email sent are also gbk encoded.

#!/usr/bin/python
# -*- coding:gbk -*- 
"""
   邮件发送类
"""
# mail.py
#
# Copyright (c) 2014 by http://blog.csdn.net/CleverCode
#
# modification history:
# --------------------
# 2014/8/15, by CleverCode, Create

import threading
import time
import random
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Utils, Encoders
import mimetypes
import sys
import smtplib
import socket
import getopt
import os

class SendMail:
    def init(self,smtpServer,username,password):
        """
        smtpServer:smtp服务器,        
        username:登录名,
        password:登录密码
        """
        self.smtpServer = smtpServer        
        self.username = username
        self.password = password
    
    def genMsgInfo(self,fromAddress,toAddress,subject,content,fileList,\
            subtype = 'plain',charset = 'gb2312'):
        """
        组合消息发送包
        fromAddress:发件人,
        toAddress:收件人,
        subject:标题,
        content:正文,
        fileList:附件,
        subtype:plain或者html
        charset:编码
        """
        msg = MIMEMultipart()
        msg['From'] = fromAddress
        msg['To'] = toAddress  
        msg['Date'] = Utils.formatdate(localtime=1)
        msg['Message-ID'] = Utils.make_msgid()
        
        #标题
        if subject:
            msg['Subject'] = subject
        
        #内容        
        if content:
            body = MIMEText(content,subtype,charset)
            msg.attach(body)
        
        #附件
        if fileList:
            listArr = fileList.split(',')
            for item in listArr:
                
                #文件是否存在
                if os.path.isfile(item) == False:
                    continue
                    
                att = MIMEText(open(item).read(), 'base64', 'gb2312')
                att["Content-Type"] = 'application/octet-stream'
                #这里的filename邮件中显示什么名字
                filename = os.path.basename(item) 
                att["Content-Disposition"] = 'attachment; filename=' + filename
                msg.attach(att)
        
        return msg.as_string()                
                                                  
    def send(self,fromAddress,toAddress,subject = None,content = None,fileList = None,\
            subtype = 'plain',charset = 'gb2312'):
        """
        邮件发送函数
        fromAddress:发件人,
        toAddress:收件人,
        subject:标题
        content:正文
        fileList:附件列表
        subtype:plain或者html
        charset:编码
        """
        try:
            server = smtplib.SMTP(self.smtpServer)
            
            #登录
            try:
                server.login(self.username,self.password)
            except smtplib.SMTPException,e:
                return "ERROR:Authentication failed:",e
                            
            #发送邮件
            server.sendmail(fromAddress,toAddress.split(',') \
                ,self.genMsgInfo(fromAddress,toAddress,subject,content,fileList,subtype,charset))
            
            #退出
            server.quit()
        except (socket.gaierror,socket.error,socket.herror,smtplib.SMTPException),e:
            return "ERROR:Your mail send failed!",e
           
        return 'OK'


def usage():
    """
    使用帮助
    """
    print """Useage:%s [-h] -s <smtpServer> -u <username> -p <password> -f <fromAddress> -t <toAddress>  [-S <subject> -c
        <content> -F <fileList>]
        Mandatory arguments to long options are mandatory for short options too.
            -s, --smtpServer=  smpt.xxx.com.
            -u, --username=   Login SMTP server username.
            -p, --password=   Login SMTP server password.
            -f, --fromAddress=   Sets the name of the "from" person (i.e., the envelope sender of the mail).
            -t, --toAddress=   Addressee&#39;s address. -t "test@test.com,test1@test.com".          
            -S, --subject=  Mail subject.
            -c, --content=   Mail message.-c "content, ......."
            -F, --fileList=   Attachment file name.            
            -h, --help   Help documen.    
       """ %sys.argv[0]
        
def start():
    """
    
    """
    try:
        options,args = getopt.getopt(sys.argv[1:],"hs:u:p:f:t:S:c:F:","--help --smtpServer= --username= --password= --fromAddress= --toAddress= --subject= --content= --fileList=",)
    except getopt.GetoptError:
        usage()
        sys.exit(2)
        return
    
    smtpServer = None
    username = None
    password = None
               
    fromAddress = None    
    toAddress = None    
    subject = None
    content = None
    fileList = None
    
    #获取参数   
    for name,value in options:
        if name in ("-h","--help"):
            usage()
            return
        
        if name in ("-s","--smtpServer"):
            smtpServer = value
        
        if name in ("-u","--username"):
            username = value
        
        if name in ("-p","--password"):
            password = value
        
        if name in ("-f","--fromAddress"):
            fromAddress = value
        
        if name in ("-t","--toAddress"):
            toAddress = value
        
        if name in ("-S","--subject"):
            subject = value
        
        if name in ("-c","--content"):
            content = value
        
        if name in ("-F","--fileList"):
            fileList = value
    
    if smtpServer == None or username == None or password == None:
        print &#39;smtpServer or username or password can not be empty!&#39;
        sys.exit(3)
           
    mail = SendMail(smtpServer,username,password)
    
    ret = mail.send(fromAddress,toAddress,subject,content,fileList)
    if ret != &#39;OK&#39;:
        print ret
        sys.exit(4)
    
    print &#39;OK&#39;
    
    return &#39;OK&#39;    
     
if name == &#39;main&#39;:
    
    start()

2.2 Help on using python program

Enter the following command to output help on using this program

# python mail.py --help


2.3 php program

This program is mainly a php splicing command string , calling the python program. Note: To send emails using the program, you need to go to the email service provider and activate the stmp service function. For example, in QQ, you need to activate the SMTP function before you can use the program to send emails. Open as shown below.


php calling program is as follows:

<?php

/**
 * SendMail.php
 * 
 * 发送邮件类
 *
 * Copyright (c) 2015 by http://blog.csdn.net/CleverCode
 *
 * modification history:
 * --------------------
 * 2015/5/18, by CleverCode, Create
 *
 */
class SendMail{

    /**
     * 发送邮件方法
     *
     * @param string $fromAddress 发件人,&#39;clevercode@qq.com&#39; 或者修改发件人名 &#39;CleverCode<clevercode@qq.com>&#39;
     * @param string $toAddress 收件人,多个收件人逗号分隔,&#39;test1@qq.com,test2@qq.com,test3@qq.com....&#39;, 或者 &#39;test1<test1@qq.com>,test2<test2@qq.com>,....&#39;
     * @param string $subject 标题
     * @param string $content 正文
     * @param string $fileList 附件,附件必须是绝对路径,多个附件逗号分隔。&#39;/data/test1.txt,/data/test2.tar.gz,...&#39;
     * @return string 成功返回&#39;OK&#39;,失败返回错误信息
     */
    public static function send($fromAddress, $toAddress, $subject = NULL, $content = NULL, $fileList = NULL){
        if (strlen($fromAddress) < 1 || strlen($toAddress) < 1) {
            return &#39;$fromAddress or $toAddress can not be empty!&#39;;
        }
        // smtp服务器
        $smtpServer = &#39;smtp.qq.com&#39;;
        // 登录用户
        $username = &#39;clevercode@qq.com&#39;;
        // 登录密码
        $password = &#39;123456&#39;;
        
        // 拼接命令字符串,实际是调用了/home/CleverCode/mail.py
        $cmd = "LANG=C && /usr/bin/python /home/CleverCode/mail.py";
        $cmd .= " -s &#39;$smtpServer&#39;";
        $cmd .= " -u &#39;$username&#39;";
        $cmd .= " -p &#39;$password&#39;";
        
        $cmd .= " -f &#39;$fromAddress&#39;";
        $cmd .= " -t &#39;$toAddress&#39;";
        
        if (isset($subject) && $subject != NULL) {
            $cmd .= " -S &#39;$subject&#39;";
        }
        
        if (isset($content) && $content != NULL) {
            $cmd .= " -c &#39;$content&#39;";
        }
        
        if (isset($fileList) && $fileList != NULL) {
            $cmd .= " -F &#39;$fileList&#39;";
        }
        
        // 执行命令
        exec($cmd, $out, $status);
        if ($status == 0) {
            return &#39;OK&#39;;
        } else {
            return "Error,Send Mail,$fromAddress,$toAddress,$subject,$content,$fileList ";
        }
        return &#39;OK&#39;;
    }
}

2.3 Usage example

Compress excel into an attachment and send an email.

<?php

/**
 * test.php
 *
 * 压缩excel成附件,发送邮件
 *
 * Copyright (c) 2015 http://blog.csdn.net/CleverCode
 *
 * modification history:
 * --------------------
 * 2015/5/14, by CleverCode, Create
 *
 */
include_once (&#39;SendMail.php&#39;);

/*
 * 客户端类
 * 让客户端和业务逻辑尽可能的分离,降低页面逻辑和业务逻辑算法的耦合,
 * 使业务逻辑的算法更具有可移植性
 */
class Client{

    public function main(){
        
        // 发送者
        $fromAddress = &#39;CleverCode<clevercode@qq.com>&#39;;
        
        // 接收者
        $toAddress = &#39;all@qq.com&#39;;
        
        // 标题
        $subject = &#39;这里是标题!&#39;;
        
        // 正文
        $content = "您好:\r\n";
        $content .= "   这里是正文\r\n ";
        
        // excel路径
        $filePath = dirname(FILE) . &#39;/excel&#39;;
        $sdate = date(&#39;Y-m-d&#39;);
        $PreName = &#39;CleverCode_&#39; . $sdate;
        
        // 文件名
        $fileName = $filePath . &#39;/&#39; . $PreName . &#39;.xls&#39;;
        
        // 压缩excel文件
        $cmd = "cd $filePath && zip $PreName.zip $PreName.xls";
        exec($cmd, $out, $status);
        $fileList = $filePath . &#39;/&#39; . $PreName . &#39;.zip&#39;;
        
        // 发送邮件(附件为压缩后的文件)
        $ret = SendMail::send($fromAddress, $toAddress, $subject, $content, $fileList);
        if ($ret != &#39;OK&#39;) {
            return $ret;
        }
        
        return &#39;OK&#39;;
    }
}

/**
 * 程序入口
 */
function start(){
    $client = new Client();
    $client->main();
}

start();

?>

The above is the detailed content of Sample code on how PHP calls Python to quickly send high-concurrency emails. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.