0

0

利用php中mail函数发送带有附件的邮件

php中文网

php中文网

发布时间:2016-05-25 16:49:24

|

1262人浏览过

|

来源于php中文网

原创

mail函数,发送邮件

语法: mail(to,subject,message,headers,parameters)

to 规定邮件的接收者 

subject 规定邮件的主题。该参数不能包含任何换行字符 

message 规定要发送的消息 

立即学习PHP免费学习笔记(深入)”;

headers 规定额外的报头,比如 From, Cc 以及 Bcc 

parameters 规定 sendmail 程序的额外参数。 

碰到的主要问题是乱码问题,刚开始是某些客户端接收邮件时好(比如QQ邮箱,估计带自动那个识别编码)的有些不foxmail、ipad显示乱码,解决方式正确的设置这个mail的headers就行了,下面是我使用的完美的无乱码的例子。

在PHP中配置php.ini文件过程分为两个步骤:

1.先找到你放置所有PHP,Apache,MySQL文件的地方,在PHP文件夹里你可以发现有一个文件:php.ini,打开后,找到mail function地方,将原来的配置代码改为如下(仅对windows系统):

[mail function] 
; For Win32 only. 
SMTP =smtp.sohu.com     
mtp_port=25 
; For Win32 only.

sendmail_from = 填上你的电子邮件全称。 

此处为以sohu的邮件服务器设置,如果你用163的邮箱,则设置为:smtp.163.com

2.在C盘搜索php.ini,选择不是快捷方式的那一个php.ini,应该在C/WINDOWS里面的,打开它,如上面一样修改它,保存,设置完后,记得重启Apache服务器,然后mail()函数就可以用了,示例代码如下:

上面函数不可以带附件了,下面我们升级一下,代码如下:

PHPMailer
PHPMailer

PHPMailer - 一个功能齐全的用于PHP的电子邮件创建和传输类。支持UTF-8内容以及8位、base64、二进制和quoted-printable编码。通过SMTPS和SMTP+STARTTLS传输提供LOGIN、PLAIN、CRAM-MD5和XOAUTH2机制的SMTP身份验证。自动验证电子邮件地址。许多PHP开发人员需要从他们的代码中发送电子邮件。唯一直接支持此功能的PHP函数是mail()。然而,它不提供任何帮助来使用流行的功能,如加密、身份验证、HTML消息和附件。正确格式化电子邮件是令人

下载
getEOT(); //生成结尾换行符
        $this->getUniq_id();
        $this->header = '';
        $this->attach = '';
        $this->cc = '';
        $this->msg = '';
    }
    public function getFromaddr() {
        return $this->fromaddr;
    }
    public function setFromaddr($fromaddr) {
        $this->fromaddr = $fromaddr;
    }
    public function getTopic() {
        return $this->topic;
    }
    public function getToaddr() {
        return $this->toaddr;
    }
    public function getCc() {
        return $this->cc;
    }
    public function getContent() {
        return $this->content;
    }
    public function getAttach() {
        return $this->attach;
    }
    public function setTopic($topic) {
        $this->topic = mb_convert_encoding(trim($topic) , 'UTF-8', 'auto');
    }
    public function setToaddr($toaddr) {
        $this->toaddr = trim($toaddr);
    }
    public function setCc($cc) {
        $this->cc = trim($cc);
    }
    public function setContent($content) {
        $this->content = mb_convert_encoding(trim($content) , 'UTF-8', 'auto');
    }
    public function setAttach($attach) {
        $this->attach = trim($attach);
    }
    public function getDomain() {
        return $this->domain;
    }
    public function setDomain($domain) {
        $this->domain = $domain; //输入的值为'@domain.com'
        
    }
    /*
     * 根据系统类型设置换行符
    */
    private function getEOT() {
        if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) {
            $this->eol = "rn";
        } elseif (strtoupper(substr(PHP_OS, 0, 3) == 'MAC')) {
            $this->eol = "r";
        } else {
            $this->eol = "n";
        }
    }
    private function getBoundary() {
        $this->boundary = '--' . substr(md5(time() . rand(1000, 2000)) , 0, 16);
    }
    private function getUniq_id() {
        $this->uniqid = md5(microtime() . time() . rand(1, 100));
    }
    private function outputCommonHeader() {
        $this->header.= 'From: ' . $this->fromaddr . $this->eol;
        //$this->header .= 'To: '.$this->toaddr.$this->eol;
        //$this->header .= 'Subject: '.$this->topic.$this->eol;
        $this->header.= 'Message-ID: <' . $this->uniqid . $this->domain . '>' . $this->eol;
        $this->header.= 'MIME-Version: 1.0' . $this->eol;
        $this->header.= 'Reply-To: ' . $this->fromaddr . $this->eol;
        $this->header.= 'Return-Path: ' . $this->fromaddr . $this->eol;
        $this->header.= 'X-Mailer: Xmail System' . $this->eol;
        $this->header.= 'Content-Disposition: inline' . $this->eol;
    }
    private function mime_content_type($f) {
        $temp = trim(exec('file -bi ' . escapeshellarg($f)));
        $temp = preg_replace('/s+/', ' ', $temp);
        $temp = explode(' ', $temp);
        return $temp[0];
    } //判断文件的mime类型
    /*
     * 只带有抄送
    */
    private function mailWithCC() {
        $this->header.= 'Cc: ' . $this->cc . $this->eol;
        $this->header.= 'Content-type: text/html; charset=UTF-8' . $this->eol;
        $this->header.= 'Content-Transfer-Encoding: 8bit' . $this->eol;
        $this->msg = $this->content;
        if (mail($this->toaddr, $this->topic, $this->msg, $this->header)) {
            return 1;
        } else {
            return 0;
        }
    }
    /*
     * $filedir需要是绝对地址
    */
    private function attachmentToBase64($filedir) {
        $this->filename = basename($filedir);
        @$fopen = fopen($filedir, 'r');
        $str = fread($fopen, filesize($filedir));
        $str = base64_encode($str);
        $this->filestr = $str;
    }
    /*
     * 只带有附件
    */
    private function mailWithAttach() {
        $this->attachmentToBase64($this->attach);
        $this->header.= 'Content-type: multipart/mixed; boundary="' . str_replace('--', '', $this->boundary) . '"' . $this->eol;
        $this->msg.= $this->eol . $this->boundary . $this->eol;
        $this->msg.= 'Content-Type: text/html; charset=utf-8' . $this->eol;
        $this->msg.= 'Content-Disposition: inline' . $this->eol;
        $this->msg.= $this->eol . $this->content . $this->eol;
        $this->msg.= $this->boundary . $this->eol;
        $this->msg.= 'Content-Type: ' . $this->mime_content_type($this->attach) . $this->eol;
        $this->msg.= 'Content-Disposition: attachment; filename="' . $this->filename . '"' . $this->eol;
        $this->msg.= 'Content-Transfer-Encoding: base64' . $this->eol;
        $this->msg.= $this->eol . $this->filestr . $this->eol;
        $this->msg.= $this->eol . $this->boundary . '--';
        if (mail($this->toaddr, $this->topic, $this->msg, $this->header)) {
            return 1;
        } else {
            return 0;
        }
    }
    /*
     * 带有附件和抄送
    */
    private function mailAll() {
        $this->attachmentToBase64($this->attach);
        $this->header.= 'Cc: ' . $this->cc . $this->eol;
        $this->header.= 'Content-type: multipart/mixed; boundary="' . str_replace('--', '', $this->boundary) . '"' . $this->eol;
        $this->msg.= $this->eol . $this->boundary . $this->eol;
        $this->msg.= 'Content-Type: text/html; charset=utf-8' . $this->eol;
        $this->msg.= 'Content-Disposition: inline' . $this->eol;
        $this->msg.= $this->eol . $this->content . $this->eol;
        $this->msg.= $this->boundary . $this->eol;
        $this->msg.= 'Content-Type: ' . $this->mime_content_type($this->attach) . $this->eol;
        $this->msg.= 'Content-Disposition: attachment; filename="' . $this->filename . '"' . $this->eol;
        $this->msg.= 'Content-Transfer-Encoding: base64' . $this->eol;
        $this->msg.= $this->eol . $this->filestr . $this->eol;
        $this->msg.= $this->eol . $this->boundary . '--';
        if (mail($this->toaddr, $this->topic, $this->msg, $this->header)) {
            return 1;
        } else {
            return 0;
        }
    }
    /*
     * 不带抄送和附件
    */
    private function mailSimple() {
        $this->header.= 'Content-type: text/html; charset=UTF-8' . $this->eol;
        $this->header.= 'Content-Transfer-Encoding: 8bit' . $this->eol;
        $this->msg = $this->content;
        if (mail($this->toaddr, $this->topic, $this->msg, $this->header)) {
            return 1;
        } else {
            return 0;
        }
    }
    public function send() {
        if (emptyempty($this->attach) && emptyempty($this->cc)) {
            $this->outputCommonHeader();
            return $this->mailSimple();
        } else if (emptyempty($this->attach)) {
            $this->outputCommonHeader();
            return $this->mailWithCC();
        } else if (emptyempty($this->cc)) {
            $this->outputCommonHeader();
            $this->getBoundary(); //有附件就生成boundary
            return $this->mailWithAttach();
        } else if (!emptyempty($this->toaddr) && !emptyempty($this->topic) && !emptyempty($this->cc) && !emptyempty($this->content) && !emptyempty($this->attach)) {
            $this->outputCommonHeader();

            $this->getBoundary(); //有附件就生成boundary

            return $this->mailAll();

        }

    }

}

?>

示例代码,有些变量需要上下文环境,代码如下:

setToaddr($this->temp['receipt_address']);
$m->setTopic($this->temp['mail_title']);
$m->setContent($this->temp['mail_content']);
$m->setFromaddr($_SESSION['user']['name'] . ' <' . $_SESSION['user']['name'] . '@' . SystemDomain . '>');
$m->setDomain('@' . SystemDomain);
$m->setCc($this->temp['cc_address']);
$m->setAttach(PATH . '/temp/' . $this->temp['attachment_file']);
$m->send();
?>

优点:使用方便就一个简单的函数

缺点:需要php.ini支持该函数,如果某些服务器不支持而又不能改环境那就不行了而且总是不稳定,发的有时能收到有时不能.


本文地址:

转载随意,但请附上文章地址:-)

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 5.9万人学习

Django 教程
Django 教程

共28课时 | 2.4万人学习

Excel 教程
Excel 教程

共162课时 | 9.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号