0

0

phpmailer发送邮件代码

php中文网

php中文网

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

|

1756人浏览过

|

来源于php中文网

原创

本文章收藏了两款利用phpmailer来发送邮件,当前如果你的机器配置好了php自带的邮件发送功能那更好哦,mail()这个那就更方便了。

issmtp();
    $mail->host = $mailconfig['smtpservers']; //smtp servers
    $mail->smtpauth = true; // 启用smtp验证功能
    $mail->username = $mailconfig['smtpusername'];
    $mail->password = $mailconfig['smtppassword'];
    $mail->from = $mailconfig['smtpfrom'];
    $mail->fromname = $_cfg['site_name'];
    $mail->charset = "gb2312";
    $mail->encoding = "base64";
    $mail->addaddress($sendto_email, ""); //收件人地址,可以替换成任何想要接收邮件的email信箱,格式是addaddress("收件人email","收件人姓名")
    $mail->addreplyto($mailconfig['addreplyto'], ""); //增加回复标签addreplyto
    $mail->ishtml(true);
    $mail->subject = $subject;
    $mail->body = $body;
    $mail->altbody = "text/html"; //该属性的设置是在邮件正文不支持html的备用显示
    if (!$mail->send()) {
        return false;
        //echo "邮件发送有误";
        //echo "邮件错误信息: " . $mail->errorinfo;
        
    } else {
        return true;
    }
}
?>

方法二

mailto = implode($addressarray, ",");
    return true;
}
/**************************************************
函数 setcc($inaddress) 设置抄送人邮件地址  
参数 $inaddress 为包涵一个或多个邮件地址的字串,email地址变量,  
使用逗号来分割多个邮件地址 默认返回值为true  
**************************************************************/
function setcc($inaddress) {
    //--用explode()函数根据","对邮件地址进行分割
    $addressarray = explode(",", $inaddress);
    //--通过循环对邮件地址的合法性进行检查
    for ($i = 0; $icheckemail($addressarray[$i]) == false) return false;
}
//--所有合法的email地址存入数组中
$this->mailcc = implode($addressarray, ",");
return true;
}
/***************************************************
函数setbcc($inaddress) 设置秘密抄送地址 参数 $inaddress 为包涵一个或多  
个邮件地址的字串,email地址变量,使用逗号来分割多个邮件地址 默认返回值为  
true  
******************************************/
function setbcc($inaddress) {
    //--用explode()函数根据","对邮件地址进行分割
    $addressarray = explode(",", $inaddress);
    //--通过循环对邮件地址的合法性进行检查
    for ($i = 0; $i < count($addressarray); $i++) {
        if ($this->checkemail($addressarray[$i]) == false) return false;
    }
    //--所有合法的email地址存入数组中
    $this->mailbcc = implode($addressarray, ",");
    return true;
}
/*****************************************************************
函数setfrom($inaddress):设置发件人地址 参数 $inaddress 为包涵邮件  
地址的字串默认返回值为true  
***************************************/
function setfrom($inaddress) {
    if ($this->checkemail($inaddress)) {
        $this->mailfrom = $inaddress;
        return true;
    }
    return false;
}
/**********************
函数 setsubject($insubject) 用于设置邮件主题参数$insubject为字串,  
默认返回的是true  
*******************************************/
function setsubject($insubject) {
    if (strlen(trim($insubject)) > 0) {
        $this->mailsubject = ereg_replace("n", "", $insubject);
        return true;
    }
    return false;
}
/****************************************************
函数settext($intext) 设置文本格式的邮件主体参数 $intext 为文本内容默  
认返回值为true  
****************************************/
function settext($intext) {
    if (strlen(trim($intext)) > 0) {
        $this->mailtext = $intext;
        return true;
    }
    return false;
}
/**********************************
函数sethtml($inhtml) 设置html格式的邮件主体参数$inhtml为html格式,  
默认返回值为true  
************************************/
function sethtml($inhtml) {
    if (strlen(trim($inhtml)) > 0) {
        $this->mailhtml = $inhtml;
        return true;
    }
    return false;
}
/**********************
函数 setattachments($inattachments) 设置邮件的附件 参数$inattachments  
为一个包涵目录的字串,也可以包涵多个文件用逗号进行分割 默认返回值为true  
*******************************************/
function setattachments($inattachments) {
    if (strlen(trim($inattachments)) > 0) {
        $this->mailattachments = $inattachments;
        return true;
    }
    return false;
}
/*********************************
函数 checkemail($inaddress) :这个函数我们前面已经调用过了,主要就是  
用于检查email地址的合法性  
*****************************************/
function checkemail($inaddress) {
    return (ereg("^[^@ ]+@([a-za-z0-9-]+.)+([a-za-z0-9-]{2}|net|com|gov|mil|org|edu|int)$", $inaddress));
}
/*************************************************
函数loadtemplate($infilelocation,$inhash,$informat) 读取临时文件并且  
替换无用的信息参数$infilelocation用于定位文件的目录  
$inhash 由于存储临时的值 $informat 由于放置邮件主体  
***********************************************************/
function loadtemplate($infilelocation, $inhash, $informat) {
    /* 比如邮件内有如下内容: dear ~!username~,
     your address is ~!useraddress~ */
    //--其中"~!"为起始标志"~"为结束标志
    $templatedelim = "~";
    $templatenamestart = "!";
    //--找出这些地方并把他们替换掉
    $templatelineout = ""; //--打开临时文件
    if ($templatefile = fopen($infilelocation, "r")) {
        while (!feof($templatefile)) {
            $templateline = fgets($templatefile, 1000);
            $templatelinearray = explode($templatedelim, $templateline);
            for ($i = 0; $i < count($templatelinearray); $i++) {
                //--寻找起始位置
                if (strcspn($templatelinearray[$i], $templatenamestart) == 0) {
                    //--替换相应的值
                    $hashname = substr($templatelinearray[$i], 1);
                    //--替换相应的值
                    $templatelinearray[$i] = ereg_replace($hashname, (string)$inhash[$hashname], $hashname);
                }
            }
            //--输出字符数组并叠加
            $templatelineout.= implode($templatelinearray, "");
        } //--关闭文件fclose($templatefile);
        //--设置主体格式(文本或html)
        if (strtoupper($informat) == "text") return ($this->settext($templatelineout));
        else if (strtoupper($informat) == "html") return ($this->sethtml($templatelineout));
    }
    return false;
}
/*****************************************
函数 getrandomboundary($offset) 返回一个随机的边界值  
参数 $offset 为整数 – 用于多管道的调用 返回一个md5()编码的字串  
****************************************/
function getrandomboundary($offset = 0) {
    //--随机数生成
    srand(time() + $offset);
    //--返回 md5 编码的32位 字符长度的字串
    return ("----" . (md5(rand())));
}
/********************************************
函数: getcontenttype($infilename)用于判断附件的类型  
**********************************************/
function getcontenttype($infilename) {
    //--去除路径
    $infilename = basename($infilename);
    //--去除没有扩展名的文件
    if (strrchr($infilename, ".") == false) {
        return "application/octet-stream";
    }
    //--提区扩展名并进行判断
    $extension = strrchr($infilename, ".");
    switch ($extension) {
        case ".gif":
            return "image/gif";
        case ".gz":
            return "application/x-gzip";
        case ".htm":
            return "text/html";
        case ".html":
            return "text/html";
        case ".jpg":
            return "image/jpeg";
        case ".tar":
            return "application/x-tar";
        case ".txt":
            return "text/plain";
        case ".zip":
            return "application/zip";
        default:
            return "application/octet-stream";
    }
    return "application/octet-stream";
}
/**********************************************
函数formattextheader把文本内容加上text的文件头  
*****************************************************/
function formattextheader() {
    $outtextheader = "";
    $outtextheader.= "content-type: text/plain;  
charset=us-asciin";
    $outtextheader.= "content-transfer-encoding: 7bitnn";
    $outtextheader.= $this->mailtext . "n";
    return $outtextheader;
} /************************************************
函数formathtmlheader()把邮件主体内容加上html的文件头  
******************************************/
function formathtmlheader() {
    $outhtmlheader = "";
    $outhtmlheader.= "content-type: text/html;  
charset=us-asciin";
    $outhtmlheader.= "content-transfer-encoding: 7bitnn";
    $outhtmlheader.= $this->mailhtml . "n";
    return $outhtmlheader;
}
/**********************************
函数 formatattachmentheader($infilelocation) 把邮件中的附件标识出来  
********************************/
function formatattachmentheader($infilelocation) {
    $outattachmentheader = "";
    //--用上面的函数getcontenttype($infilelocation)得出附件类型
    $contenttype = $this->getcontenttype($infilelocation);
    //--如果附件是文本型则用标准的7位编码
    if (ereg("text", $contenttype)) {
        $outattachmentheader.= "content-type: " . $contenttype . ";n";
        $outattachmentheader.= ' name="' . basename($infilelocation) . '"' . "n";
        $outattachmentheader.= "content-transfer-encoding: 7bitn";
        $outattachmentheader.= "content-disposition: attachment;n";
        $outattachmentheader.= ' filename="' . basename($infilelocation) . '"' . "nn";
        $textfile = fopen($infilelocation, "r");
        while (!feof($textfile)) {
            $outattachmentheader.= fgets($textfile, 1000);
        }
        //--关闭文件 fclose($textfile);
        $outattachmentheader.= "n";
    }
    //--非文本格式则用64位进行编码
    else {
        $outattachmentheader.= "content-type: " . $contenttype . ";n";
        $outattachmentheader.= ' name="' . basename($infilelocation) . '"' . "n";
        $outattachmentheader.= "content-transfer-encoding: base64n";
        $outattachmentheader.= "content-disposition: attachment;n";
        $outattachmentheader.= ' filename="' . basename($infilelocation) . '"' . "nn";
        //--调用外部命令uuencode进行编码
        exec("uuencode -m $infilelocation nothing_out", $returnarray);
        for ($i = 1; $i < (count($returnarray)); $i++) {
            $outattachmentheader.= $returnarray[$i] . "n";
        }
    }
    return $outattachmentheader;
}
/******************************
函数 send()用于发送邮件,发送成功返回值为true  
************************************/
function send() {
    //--设置邮件头为空
    $mailheader = "";
    //--添加抄送人
    if ($this->mailcc != "") $mailheader.= "cc: " . $this->mailcc . "n";
    //--添加秘密抄送人
    if ($this->mailbcc != "") $mailheader.= "bcc: " . $this->mailbcc . "n";
    //--添加发件人
    if ($this->mailfrom != "") $mailheader.= "from: " . $this->mailfrom . "n";
    //---------------------------邮件格式------------------------------
    //--文本格式
    if ($this->mailtext != "" && $this->mailhtml == "" && $this->mailattachments == "") {
        return mail($this->mailto, $this->mailsubject, $this->mailtext, $mailheader);
    }
    //--html或text格式
    else if ($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments == "") {
        $bodyboundary = $this->getrandomboundary();
        $textheader = $this->formattextheader();
        $htmlheader = $this->formathtmlheader();
        //--设置 mime-版本
        $mailheader.= "mime-version: 1.0n";
        $mailheader.= "content-type: multipart/alternative;n";
        $mailheader.= ' boundary="' . $bodyboundary . '"';
        $mailheader.= "nnn";
        //--添加邮件主体和边界
        $mailheader.= "--" . $bodyboundary . "n";
        $mailheader.= $textheader;
        $mailheader.= "--" . $bodyboundary . "n";
        //--添加html标签
        $mailheader.= $htmlheader;
        $mailheader.= "n--" . $bodyboundary . "--";
        //--发送邮件
        return mail($this->mailto, $this->mailsubject, "", $mailheader);
    }
    //--文本加html加附件
    else if ($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments != "") {
        $attachmentboundary = $this->getrandomboundary();
        $mailheader.= "content-type: multipart/mixed;n";
        $mailheader.= ' boundary="' . $attachmentboundary . '"' . "nn";
        $mailheader.= "this is a multi-part message in mime format.n";
        $mailheader.= "--" . $attachmentboundary . "n";
        $bodyboundary = $this->getrandomboundary(1);
        $textheader = $this->formattextheader();
        $htmlheader = $this->formathtmlheader();
        $mailheader.= "mime-version: 1.0n";
        $mailheader.= "content-type: multipart/alternative;n";
        $mailheader.= ' boundary="' . $bodyboundary . '"';
        $mailheader.= "nnn";
        $mailheader.= "--" . $bodyboundary . "n";
        $mailheader.= $textheader;
        $mailheader.= "--" . $bodyboundary . "n";
        $mailheader.= $htmlheader;
        $mailheader.= "n--" . $bodyboundary . "--";
        //--获取附件值
        $attachmentarray = explode(",", $this->mailattachments);
        //--根据附件的个数进行循环
        for ($i = 0; $i < count($attachmentarray); $i++) {
            //--分割 $mailheader .= "n--".$attachmentboundary. "n";
            //--附件信息
            $mailheader.= $this->formatattachmentheader($attachmentarray[$i]);
        }
        $mailheader.= "--" . $attachmentboundary . "--";
        return mail($this->mailto, $this->mailsubject, "", $mailheader);
    }
    return false;
}
}
?>

使用方法:

setto("a@a.com"); //收件人
$mail->setcc("b@b.com,[url=mailto:c@c.com]c@c.com[/url]"); //抄送
$mail->setcc("d@b.com,[url=mailto:e@c.com]e@c.com[/url]"); //秘密抄送
$mail->setfrom("[url=mailto:f@f.com]f@f.com[/url]"); //发件人
$mail->setsubject("主题"); //主题
$mail->settext("文本格式"); //发送文本格式也可以是变量
$mail->sethtml("html格式"); //发送html格式也可以是变量
$mail->setattachments("c:a.jpg"); //添加附件,需表明路径
$mail->send(); //发送邮件
?>


教程地址:

MarsX
MarsX

AI驱动快速构建App,低代码无代码开发,改变软件开发的游戏规则

下载

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

欢迎转载!但请带上文章地址^^

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
全国统一发票查询平台入口合集
全国统一发票查询平台入口合集

本专题整合了全国统一发票查询入口地址合集,阅读专题下面的文章了解更多详细入口。

37

2026.02.03

短剧入口地址汇总
短剧入口地址汇总

本专题整合了短剧app推荐平台,阅读专题下面的文章了解更多详细入口。

104

2026.02.03

植物大战僵尸版本入口地址汇总
植物大战僵尸版本入口地址汇总

本专题整合了植物大战僵尸版本入口地址汇总,前往文章中寻找想要的答案。

49

2026.02.03

c语言中/相关合集
c语言中/相关合集

本专题整合了c语言中/的用法、含义解释。阅读专题下面的文章了解更多详细内容。

9

2026.02.03

漫蛙漫画网页版入口与正版在线阅读 漫蛙MANWA官网访问专题
漫蛙漫画网页版入口与正版在线阅读 漫蛙MANWA官网访问专题

本专题围绕漫蛙漫画(Manwa / Manwa2)官网网页版入口进行整理,涵盖漫蛙漫画官方主页访问方式、网页版在线阅读入口、台版正版漫画浏览说明及基础使用指引,帮助用户快速进入漫蛙漫画官网,稳定在线阅读正版漫画内容,避免误入非官方页面。

76

2026.02.03

Yandex官网入口与俄罗斯搜索引擎访问指南 Yandex中文登录与网页版入口
Yandex官网入口与俄罗斯搜索引擎访问指南 Yandex中文登录与网页版入口

本专题汇总了俄罗斯知名搜索引擎 Yandex 的官网入口、免登录访问地址、中文登录方法与网页版使用指南,帮助用户稳定访问 Yandex 官网,并提供一站式入口汇总。无论是登录入口还是在线搜索,用户都能快速获取最新稳定的访问链接与使用指南。

429

2026.02.03

Java 设计模式与重构实践
Java 设计模式与重构实践

本专题专注讲解 Java 中常用的设计模式,包括单例模式、工厂模式、观察者模式、策略模式等,并结合代码重构实践,帮助学习者掌握 如何运用设计模式优化代码结构,提高代码的可读性、可维护性和扩展性。通过具体示例,展示设计模式如何解决实际开发中的复杂问题。

4

2026.02.03

C# 并发与异步编程
C# 并发与异步编程

本专题系统讲解 C# 异步编程与并发控制,重点介绍 async 和 await 关键字、Task 类、线程池管理、并发数据结构、死锁与线程安全问题。通过多个实战项目,帮助学习者掌握 如何在 C# 中编写高效的异步代码,提升应用的并发性能与响应速度。

5

2026.02.03

Python 强化学习与深度Q网络(DQN)
Python 强化学习与深度Q网络(DQN)

本专题深入讲解 Python 在强化学习(Reinforcement Learning)中的应用,重点介绍 深度Q网络(DQN) 及其实现方法,涵盖 Q-learning 算法、深度学习与神经网络的结合、环境模拟与奖励机制设计、探索与利用的平衡等。通过构建一个简单的游戏AI,帮助学习者掌握 如何使用 Python 训练智能体在动态环境中作出决策。

4

2026.02.03

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP发送邮件功能
PHP发送邮件功能

共6课时 | 6.8万人学习

Git 教程
Git 教程

共21课时 | 3.3万人学习

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

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