• 关于 PHPMailer 邮件发送类的使用心得(含多文件上传)


    This is important for send mail

    • PHPMailer 核心文件

      • class.phpmailer.php

      • class.phpmaileroauth.php

      • class.phpmaileroauthgoogle.php

      • class.pop3.php

      • class.smtp.php

      • get_oauth_token.php

      • PHPMailerAutoload.php

    • 在使用PHPMailer之前,首先查看PHP环境扩展中是否开启了socketopenssl扩展,如若没有开启,请在php.ini配置文件中开启两者扩展支持

    • 大多数主流邮箱都支持SMTP协议,除去QQ邮箱,在作为三方代发邮箱时,需要开启SMTP服务支持,并需要授权码登录使用邮箱。对于其他像163、sina等邮箱直接使用代发邮箱账号和密码即可。

      • code of demo

      • <?php
        // 引入PHPMailer核心类文件(SMTP协议方式)
        require_once('class.phpmailer.php');
        require_once('class.smtp.php');
        
        $mail = new PHPMailer();
        
        // 是否开启debug调试模式,默认为false,非开发环境建议关闭
        $mail->SMTPDebug = 0;
        
        // 使用SMTP鉴权方式发送邮件(通用必写方法)
        $mail->isSMTP();
        
        // 一旦使用SMTP鉴权方式,必须为True(通用必写方法)
        $mail->SMTPAuth = true;
        
        /**
         * 以下开始分为QQ邮箱和非QQ邮箱方式,以sina邮箱为例
         * QQ邮箱方式需要使用ssl方式加密登录,登录密码使用所
         * 给授权码,在邮箱设置中的账号中,开启IMAP/SMTP服务
         * 并验证密保成功,会给出授权码
         */
        // 连接的SMTP服务器主机地址(QQ)
        $mail->HOST = 'smtp.qq.com';
        // 设置使用ssl加密方式登录鉴权(QQ),其他邮箱请注释或者令属性值为空
        $mail->SMTPSecure = 'ssl';
        // 设置ssl连接SMTP服务器远程连接端口号(QQ)
        $mali->Port = '465';
        // 设置发送邮件编码
        $mail->CharSet = 'UTF-8';
        // 设置发送人昵称
        $mail->FromName = '发件人昵称';
        // 设置发件人邮箱账户
        $mail->Usename = '10011@qq.com';
        // QQ邮箱使用获取到的授权码作为账户的登录密码,其他邮箱使用邮箱密码登录即可
        $mail->Password = '***********';
        //发件人邮箱地址,同发件人邮箱
        $mail->From = '10011@qq.com';
        // 设置邮件正文是否为HTML编码
        $mail->isHTML(true);
        // 添加收件人邮箱地址,如若需要群发多人,可多次调用此方法即可
        $mail->addAddress('20011@163.com');
        //$mail->addAddress('30011@sina.com');
        // 设置邮件主题
        $mail->Subject = '邮件主题';
        // 设置邮件正文(可使用定界符来定义大量正文内容)
        $mail->Body = <<< EOR
        <h1>Test mail</h1>
        EOR;
        /* 设置邮件附件,此方法两个参数,分别是附件的位置路径(绝对或者相对路径)以及附件的命名,可  多次调用此方法,添加多个附件 */
        $mail->addAttachment($path,$name);
        //$mail->addAttachment($path,$name);
        // 发送邮件并返回 bool
        $status = $mail->send();
        // 根据返回 bool 值进行判断操作
        if($status){
            #code ...
            $mail->smtpClose();
        }else{
            echo 'Send Mail Error! Error Message is '.$mail->ErrorInfo;
        }
      • 常见的(SMTP、POP3)邮箱服务器以及端口
        <?php
        /**
         * 此处列举的各大主流或者常见的邮箱服务器如果在连接时出现错误,请注册相应邮箱,在其邮箱
         * 设置中查看相应邮箱服务器的主机地址以及端口
         */ 
        sina.com
        POP3服务器地址:pop3.sina.com.cn(端口:110)
        SMTP服务器地址:smtp.sina.com.cn(端口:25) 
            
        sina.cn
        POP3服务器地址:pop3.sina.com(端口:110)      ------- > pop.sina.com
        SMTP服务器地址:smtp.sina.com(端口:25)
            
        sinaVIP
        POP3服务器:pop3.vip.sina.com (端口:110)
        SMTP服务器:smtp.vip.sina.com (端口:25)
        
        sohu.com
        POP3服务器地址:pop3.sohu.com(端口:110)
        SMTP服务器地址:smtp.sohu.com(端口:25)
         
         126邮箱
          POP3服务器地址:pop.126.com(端口:110)
          SMTP服务器地址:smtp.126.com(端口:25)
        
          139邮箱
          POP3服务器地址:POP.139.com(端口:110)
          SMTP服务器地址:SMTP.139.com(端口:25)
              
          163.com
          POP3服务器地址:pop.163.com(端口:110)
          SMTP服务器地址:smtp.163.com(端口:25)
           
          QQ邮箱
          POP3服务器地址:pop.qq.com(端口:110)
          SMTP服务器地址:smtp.qq.com(端口:ssl/465|Tls/587)
        
          QQ企业邮箱
          POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
          SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:ssl/465|Tls/587)
        
          yahoo.com
          POP3服务器地址:pop.mail.yahoo.com
          SMTP服务器地址:smtp.mail.yahoo.com
        
          yahoo.com.cn
          POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
          SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587
        
          HotMail
          POP3服务器地址:pop3.live.com(端口:995)
          SMTP服务器地址:smtp.live.com(端口:587)
        
          gmail(google.com)
          POP3服务器地址:pop.gmail.com(SSL启用端口:995)
          SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587263.net
          POP3服务器地址:pop3.263.net(端口:110)
          SMTP服务器地址:smtp.263.net(端口:25263.net.cn
          POP3服务器地址:pop.263.net.cn(端口:110)
          SMTP服务器地址:smtp.263.net.cn(端口:25)
        
          x263.net
          POP3服务器地址:pop.x263.net(端口:110)
          SMTP服务器地址:smtp.x263.net(端口:25)
        
          21cn.com
          POP3服务器地址:pop.21cn.com(端口:110)
          SMTP服务器地址:smtp.21cn.com(端口:25)
        
          Foxmail
          POP3服务器地址:POP.foxmail.com(端口:110)
          SMTP服务器地址:SMTP.foxmail.com(端口:25)
        
          china.com
          POP3服务器地址:pop.china.com(端口:110)
          SMTP服务器地址:smtp.china.com(端口:25)
        
          tom.com
          POP3服务器地址:pop.tom.com(端口:110)
          SMTP服务器地址:smtp.tom.com(端口:25)
        
          etang.com
          POP3服务器地址:pop.etang.com(端口:110)
          SMTP服务器地址:smtp.etang.com(端口:25)    
      • 关于表单多文件上传并发送邮箱 demo

      • <html>
            <head>
                <title></title>
            </head>
            <body>
                <form method="POST" enctype="multipart/form-data" action="form_test.php">
                    <input type="file" name="upload[]" multiple="multiple" />
                    <button type="submit">提交</button>
                </form>
            </body>
        </html>
      • 多文件选择上传,表单提交 $_FILES 数据形式打印,如图
      • form_test.php
      • <?php
        header("content-type:text/html;charset=utf-8");
        if($_FILES['upload']['error'][0] == 4){
            echo("<script type='text/javascript'> alert('请上传文件'); window.history.back();</script>");
            exit;
        }else{
            // 设置文件保存目录
            $uploaddir = "../upload/file/"; 
            require_once 'upload_img.php';
            $FJ = array();
            for($i=0; $i<count($_FILES['upload']['name']); $i++) {
        
                if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_uploaded_file($_FILES['upload']['tmp_name'][$i])) 
                {
                    //判断文件类型
                    if(!in_array(strtolower(fileext($_FILES['upload']['name'][$i])),$type)) 
                    { 
                        $text=implode(",",$type); 
                        $page_result=$text;
                        echo("<script type='text/javascript'> alert('请上传格式为 ".$page_result." 的图片'); window.history.back();</script>");
                        exit;
                    }else{
                        //生成目标文件的文件名 
                        $filename=explode(".",$_FILES['upload']['name'][$i]);
                        do 
                        { 
                            $filename[0]=random(10);
                            $name=implode(".",$filename); 
                            $uploadfile=$uploaddir.$name; 
                        } 
                        while(file_exists($uploadfile)); 
                        if (move_uploaded_file($_FILES['upload']['tmp_name'][$i],$uploadfile)) 
                        {   
                               
                            $FJ[] = $uploadfile;
                        }
                    }
                }else{
                    echo("<script type='text/javascript'> alert('上传失败,请重试'); window.history.back();</script>");
                    exit;
                } 
            }
        }
        
        // 发送邮件(QQ)
        require_once("class.phpmailer.php");
        require_once("class.smtp.php");
        
        $mail = new PHPMailer();
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->Host = 'smtp.qq.com';
        $mail->Port = '465';
        $mail->CharSet = 'UTF-8';
        $mail->FromName = 'Form Data';
        $mail->Username = '发送人邮箱账户';
        $mail->Password = '发件人账户授权码';
        $mail->From = '发件人邮箱账户';
        $mail->isHTML(true);
        $mail->addAddress('收件人邮箱');
        $mail->Subject = 'mail title';
        $mail->Body = '<h1>Form Data<h1>';
        
        // 根据当前脚本文件位置获取所需目录绝对地址
        $Active_path = dirname(dirname(__FILE__));
        
        // 循环拼接附件绝对路径并调用附件添加方法加入邮件附件中
        for ($i=0; $i < count($FJ) ; $i++) { 
            if(file_exists($FJ[$i])){
                $FJ[$i] = $Active_path.'/'.str_replace('../', '', $FJ[$i]);
                $mail->addAttachment($FJ[$i]);
            }else{
                continue;
            }    
        }
        
        $status = $mail->send();
        if($status){
            // 发送成功,根据需要是否将上传附件文件删除,上传失败亦然
            for ($i=0; $i < count($FJ) ; $i++) { 
                if(file_exists($FJ[$i])){
                    unlink($FJ[$i]);
                }    
            }
            echo("<script type='text/javascript'> alert('send mail success!'); window.history.back();</script>");
            exit;
        }else{
            for ($i=0; $i < count($FJ) ; $i++) { 
                if(file_exists($FJ[$i])){
                    unlink($FJ[$i]);
                }
            }
             echo("<script type='text/javascript'> alert('send mail fail,please try again!Error message: '".$mail->ErrorInfo."'); window.history.back();</script>");
            exit;
        }
      • upload_img.php
      • <?php 
        //设置允许上传文件的类型
        $type=array("jpg","gif","bmp","jpeg","png"); 
        
        //获取文件后缀名函数 
        function fileext($filename) 
        { 
            return substr(strrchr($filename, '.'), 1); 
        } 
        
        //生成随机文件名函数 
        function random($length) 
        { 
            $hash = 'SC-'; 
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; 
            $max = strlen($chars) - 1; 
            mt_srand((double)microtime() * 1000000); 
            for($i = 0; $i < $length; $i++) 
            { 
                $hash .= $chars[mt_rand(0, $max)]; 
            } 
            return $hash; 
        } 
        
        // 获取不同比例的缩略图
        function ResizeImage($uploadfile,$maxwidth,$maxheight,$name)
        {
            //取得当前图片大小
            $width = imagesx($uploadfile);
            $height = imagesy($uploadfile);
            $i=0.5;
            //生成缩略图的大小
            if(($width > $maxwidth) || ($height > $maxheight))
            {
                /*
                $widthratio = $maxwidth/$width;
                $heightratio = $maxheight/$height;
                
                if($widthratio < $heightratio)
                {
                    $ratio = $widthratio;
                }
                else
                {
                     $ratio = $heightratio;
                }
                
                $newwidth = $width * $ratio;
                $newheight = $height * $ratio;
                */
                $newwidth = $width * $i;
                $newheight = $height * $i;
                if(function_exists("imagecopyresampled"))
                {
                    $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
                    imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                }
                else
                {
                    $uploaddir_resize = imagecreate($newwidth, $newheight);
                    imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                }
                
                ImageJpeg ($uploaddir_resize,$name);
                ImageDestroy ($uploaddir_resize);
            }
            else
            {
                ImageJpeg ($uploadfile,$name);
            }
        }
        
        // 此处注释内容为图片文件的比例缩放
        // if($_FILES["filename"]['size'])
        // {
        //     if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg")
        //     {
        //         //$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
        //         $im = imagecreatefromjpeg($uploadfile);
        //     }
        //     elseif($file_type == "image/x-png")
        //     {
        //         //$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
        //         $im = imagecreatefromjpeg($uploadfile);
        //     }
        //     elseif($file_type == "image/gif")
        //     {
        //         //$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
        //         $im = imagecreatefromjpeg($uploadfile);
        //     }
        //     else//默认jpg
        //     {
        //         $im = imagecreatefromjpeg($uploadfile);
        //     }
        //     if($im)
        //     {
        //         ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
            
        //         ImageDestroy ($im);
        //     }
        // } 
  • 相关阅读:
    CSS
    版权动态日记页脚 JS
    smoothscroll
    JavaScript String
    JavaScript Document对象
    【网易官方】极客战记(codecombat)攻略-森林-漫漫长路long-road
    【网易官方】极客战记(codecombat)攻略-森林-看门狗guard-dog
    【网易官方】极客战记(codecombat)攻略-森林-去拿过来go-fetch
    【网易官方】极客战记(codecombat)攻略-森林-宠物小测试pet-quiz
    【网易官方】极客战记(codecombat)攻略-森林-博士猫咪phd-kitty
  • 原文地址:https://www.cnblogs.com/liwei-17/p/8988232.html
Copyright © 2020-2023  润新知