zoukankan      html  css  js  c++  java
  • 基于PHP自带的mail函数实现发送邮件以及带有附件的邮件功能

    PHPmail函数简介

    bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
    
    

    其中: $to 必需。规定邮件的接收者
    $subject 必需。规定邮件的主题。该参数不能包含任何换行字符。
    $message 必需。规定要发送的消息。
    $additional_headers 规定额外的报头,比如 From, Cc 以及 Bcc等
    $additional_parameters 规定 sendmail 程序的额外参数

    参考代码(Linux服务器下测试有效)

    <?php
    
    class SendMailApi
    {
        /**
         * @param  $to 收件人
         * @param  $subject 邮件主题
         * @param  $message 发送的消息
         * @param  $from 发件人
         * @param  $content_type 类型
         * @param  $attache 附件
         */
        public function sendMail($to, $subject, $message, $from, $content_type, $attache = array())
        {
            if (!empty($from)) $head = "From:   $from
    ";
            if (empty($content_type)) $content_type = "text/plain";
    
            if (is_array($attache)) {
                $boundary = "===" . md5(uniqid("")) . "===";
                $head .= "Mime-Version:   1.0
    Content-Type:   multipart/mixed;   boundary="";
                $head .= "$boundary"
    
    This   is   a   multi-part   message   in   MIME   format.
    
    ";
                $head .= "--$boundary
    ";
                $head .= "Content-Type:   $content_type
    ";
                $head .= "
    $message
    
    ";
    
                while (list($key, $val) = each($attache)) {
                    $fd = fopen("$val", "r") or die("unable to open file$val");
                    $contents = chunk_split(base64_encode(fread($fd, filesize("$val"))));
                    fclose($fd);
                    $head .= "--$boundary
    ";
                    $head .= "Content-Type:   application/octet-stream;   name="" . basename($val);
                    $head .= ""
    Content-Transfer-Encoding:   BASE64
    ";
                    $head .= "Content-Disposition:   attachment;   filename="" . basename($val);
                    $head .= ""
    
    " . $contents . "
    
    ";
                }
                $head .= "--" . $boundary . "--
    
    ";
            } else {
                if (!empty($content_type)) {
                    $head .= "Content-Type:  $content_type
    ";
                    $head .= "
    $message
    ";
                }
            }
            return mail($to, $subject, "", $head);
        }
        public function sendMailTest()
        {
    
            $to = "xxxxx@xxxx.com";         // 邮件接收者
            $subject = "test";                // 邮件标题
            $from = "xxx@xxxxx.com";   // 邮件发送者
            $subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
            $order_bn = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $memer_id = self::mt_rand(1000000,9999999);
            $ship_mobile = '139xxxxxxxx';
            //产生随机数据
            $res = array(
                0 =>
                    array(
                        'order_bn' => $order_bn,
                        'member_id' => $memer_id ,
                        'ship_mobile' => $ship_mobile,
                    ),
                1 =>
                    array(
                        'order_bn' => $order_bn,
                        'member_id' => $memer_id,
                        'ship_mobile' => $ship_mobile,
                    ),
                2 =>
                    array(
                        'order_bn' => $order_bn,
                        'member_id' => $memer_id,
                        'ship_mobile' => $ship_mobile,
                    ),
                3 =>
                    array(
                        'order_bn' => $order_bn,
                        'member_id' => $memer_id,
                        'ship_mobile' => $ship_mobile,
                    ),
                4 =>
                    array(
                        'order_bn' => $order_bn,
                        'member_id' => $memer_id,
                        'ship_mobile' => $ship_mobile,
                    ),
                5 =>
                    array(
                        'order_bn' => $order_bn,
                        'member_id' => $memer_id,
                        'ship_mobile' => $ship_mobile,
                    ),
                6 =>
                    array(
                            'order_bn' => $order_bn,
                            'member_id' => $memer_id,
                            'ship_mobile' => $ship_mobile,
                        ));
            $csv_header = array('订单号', '会员id', '手机号');
            $file_name = date("Y-m-d") . ".csv";
            $fp = fopen("$file_name", 'a');
            // 处理头部标题
            $header = implode(',', $csv_header) . PHP_EOL;
            // 处理内容
            $content = '';
            foreach ($res as $k => $v) {
                $content .= implode(',', $v) . PHP_EOL;
            }
            // 拼接
            $csv = $header . $content;
            // 写入并关闭资源
            fwrite($fp, $csv);
            fclose($fp);
            //添加附件
            $attache = array($file_name);
            $message = " This is a test";  // 邮件正文
            $headers = "From: $from" . "
    ";// 头部信息设置
            $headers .= "MIME-Version: 1.0" . "
    ";
            $headers .= "Content-type: text/html; charset=uft-8" . "
    ";
            $headers .= "Content-Transfer-Encoding: 8bit";
            if(filter_var($to, FILTER_VALIDATE_EMAIL)&&filter_var($from, FILTER_VALIDATE_EMAIL)){
                $rst = $this->sendMail($to, $subject, $message, $from, '', $attache);
             }
            @unlink($file_name);
            var_dump($rst);
        }
    }
    //测试
    $mailObj = new SendMailApi();
    $rst = $mailObj->sendMailTest();
    
    

    测试效果

  • 相关阅读:
    【PHP设计模式 08_CeLue.php】策略模式
    【PHP设计模式 07_ZeRenLian.php】责任链模式
    【PHP设计模式 06_GuanChaZhe.php】观察者模式
    【PHP设计模式 05_DanLi.php】单例模式
    【PHP设计模式 04_GongChang.php】 工厂方法
    【PHP设计模式 03_JianDanGongChang.php】 简单工厂
    【PHP设计模式 02_JieKou.php】面向接口开发
    区块链学习-开始
    erc721-165学习
    cryptopunks的代码解释
  • 原文地址:https://www.cnblogs.com/weblm/p/8654766.html
Copyright © 2011-2022 走看看