热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

mail函数,发送邮件语法:mail(to,subject,message,headers,parameters)to规定邮件的接收者subject规定邮件的主题。该参数不能包...
mail函数,发送邮件

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

to 规定邮件的接收者

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

message 规定要发送的消息

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()函数就可以用了,示例代码如下:

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

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.= &#39;Message-ID: <&#39; . $this->uniqid . $this->domain . &#39;>&#39; . $this->eol;
        $this->header.= &#39;MIME-Version: 1.0&#39; . $this->eol;
        $this->header.= &#39;Reply-To: &#39; . $this->fromaddr . $this->eol;
        $this->header.= &#39;Return-Path: &#39; . $this->fromaddr . $this->eol;
        $this->header.= &#39;X-Mailer: Xmail System&#39; . $this->eol;
        $this->header.= &#39;Content-Disposition: inline&#39; . $this->eol;
    }
    private function mime_content_type($f) {
        $temp = trim(exec(&#39;file -bi &#39; . escapeshellarg($f)));
        $temp = preg_replace(&#39;/s+/&#39;, &#39; &#39;, $temp);
        $temp = explode(&#39; &#39;, $temp);
        return $temp[0];
    } //判断文件的mime类型
    /*
     * 只带有抄送
    */
    private function mailWithCC() {
        $this->header.= &#39;Cc: &#39; . $this->cc . $this->eol;
        $this->header.= &#39;Content-type: text/html; charset=UTF-8&#39; . $this->eol;
        $this->header.= &#39;Content-Transfer-Encoding: 8bit&#39; . $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, &#39;r&#39;);
        $str = fread($fopen, filesize($filedir));
        $str = base64_encode($str);
        $this->filestr = $str;
    }
    /*
     * 只带有附件
    */
    private function mailWithAttach() {
        $this->attachmentToBase64($this->attach);
        $this->header.= &#39;Content-type: multipart/mixed; boundary="&#39; . str_replace(&#39;--&#39;, &#39;&#39;, $this->boundary) . &#39;"&#39; . $this->eol;
        $this->msg.= $this->eol . $this->boundary . $this->eol;
        $this->msg.= &#39;Content-Type: text/html; charset=utf-8&#39; . $this->eol;
        $this->msg.= &#39;Content-Disposition: inline&#39; . $this->eol;
        $this->msg.= $this->eol . $this->content . $this->eol;
        $this->msg.= $this->boundary . $this->eol;
        $this->msg.= &#39;Content-Type: &#39; . $this->mime_content_type($this->attach) . $this->eol;
        $this->msg.= &#39;Content-Disposition: attachment; filename="&#39; . $this->filename . &#39;"&#39; . $this->eol;
        $this->msg.= &#39;Content-Transfer-Encoding: base64&#39; . $this->eol;
        $this->msg.= $this->eol . $this->filestr . $this->eol;
        $this->msg.= $this->eol . $this->boundary . &#39;--&#39;;
        if (mail($this->toaddr, $this->topic, $this->msg, $this->header)) {
            return 1;
        } else {
            return 0;
        }
    }
    /*
     * 带有附件和抄送
    */
    private function mailAll() {
        $this->attachmentToBase64($this->attach);
        $this->header.= &#39;Cc: &#39; . $this->cc . $this->eol;
        $this->header.= &#39;Content-type: multipart/mixed; boundary="&#39; . str_replace(&#39;--&#39;, &#39;&#39;, $this->boundary) . &#39;"&#39; . $this->eol;
        $this->msg.= $this->eol . $this->boundary . $this->eol;
        $this->msg.= &#39;Content-Type: text/html; charset=utf-8&#39; . $this->eol;
        $this->msg.= &#39;Content-Disposition: inline&#39; . $this->eol;
        $this->msg.= $this->eol . $this->content . $this->eol;
        $this->msg.= $this->boundary . $this->eol;
        $this->msg.= &#39;Content-Type: &#39; . $this->mime_content_type($this->attach) . $this->eol;
        $this->msg.= &#39;Content-Disposition: attachment; filename="&#39; . $this->filename . &#39;"&#39; . $this->eol;
        $this->msg.= &#39;Content-Transfer-Encoding: base64&#39; . $this->eol;
        $this->msg.= $this->eol . $this->filestr . $this->eol;
        $this->msg.= $this->eol . $this->boundary . &#39;--&#39;;
        if (mail($this->toaddr, $this->topic, $this->msg, $this->header)) {
            return 1;
        } else {
            return 0;
        }
    }
    /*
     * 不带抄送和附件
    */
    private function mailSimple() {
        $this->header.= &#39;Content-type: text/html; charset=UTF-8&#39; . $this->eol;
        $this->header.= &#39;Content-Transfer-Encoding: 8bit&#39; . $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[&#39;receipt_address&#39;]);
$m->setTopic($this->temp[&#39;mail_title&#39;]);
$m->setContent($this->temp[&#39;mail_content&#39;]);
$m->setFromaddr($_SESSION[&#39;user&#39;][&#39;name&#39;] . &#39; <&#39; . $_SESSION[&#39;user&#39;][&#39;name&#39;] . &#39;@&#39; . SystemDomain . &#39;>&#39;);
$m->setDomain(&#39;@&#39; . SystemDomain);
$m->setCc($this->temp[&#39;cc_address&#39;]);
$m->setAttach(PATH . &#39;/temp/&#39; . $this->temp[&#39;attachment_file&#39;]);
$m->send();
?>

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

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


本文地址:

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

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
author-avatar
Annia000
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有