• 基于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数组根据某一个键值,把相同键值的合并生成一个新的二维数组
    首次备案图文引导
    阿里云域名备案之如何填写真实性核验单
    国际域名和境外域名能否提交备案
    sublime如何实现函数折叠
    怎样实现给DEDE的栏目增加栏目图片(2)
    深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)
    HBase零基础高阶应用实战(CDH5、二级索引、实践、DBA)
    大数据就是这么任性第一季数据结构和算法(一线经验、权威资料、知识新鲜、实践性强、全程源码)
    Cloudera Hadoop 5& Hadoop高阶管理及调优课程(CDH5,Hadoop2.0,HA,安全,管理,调优)
  • 原文地址:https://www.cnblogs.com/weblm/p/8654766.html
Copyright © 2020-2023  润新知