码迷,mamicode.com
首页 > Web开发 > 详细

基于PHP自带的mail函数实现发送邮件以及带有附件的邮件功能

时间:2018-03-27 01:49:15      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:_id   ===   this   encode   addition   服务   subject   send   tran   

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\n";
        if (empty($content_type)) $content_type = "text/plain";

        if (is_array($attache)) {
            $boundary = "===" . md5(uniqid("")) . "===";
            $head .= "Mime-Version:   1.0\nContent-Type:   multipart/mixed;   boundary=\"";
            $head .= "$boundary\"\n\nThis   is   a   multi-part   message   in   MIME   format.\n\n";
            $head .= "--$boundary\n";
            $head .= "Content-Type:   $content_type\n";
            $head .= "\n$message\n\n";

            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\n";
                $head .= "Content-Type:   application/octet-stream;   name=\"" . basename($val);
                $head .= "\"\nContent-Transfer-Encoding:   BASE64\n";
                $head .= "Content-Disposition:   attachment;   filename=\"" . basename($val);
                $head .= "\"\n\n" . $contents . "\n\n";
            }
            $head .= "--" . $boundary . "--\n\n";
        } else {
            if (!empty($content_type)) {
                $head .= "Content-Type:  $content_type\n";
                $head .= "\n$message\n";
            }
        }
        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(
                    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" . "\n";// 头部信息设置
        $headers .= "MIME-Version: 1.0" . "\n";
        $headers .= "Content-type: text/html; charset=uft-8" . "\r\n";
        $headers .= "Content-Transfer-Encoding: 8bit";
        $rst = $this->sendMail($to, $subject, $message, $from, '', $attache);
        @unlink($file_name);
        var_dump($rst);
    }
}
//测试
$mailObj = new SendMailApi();
$rst = $mailObj->sendMailTest();

测试效果

技术分享图片

基于PHP自带的mail函数实现发送邮件以及带有附件的邮件功能

标签:_id   ===   this   encode   addition   服务   subject   send   tran   

原文地址:https://www.cnblogs.com/weblm/p/8654766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!