使用插件
发送邮件
使用插件PHPMailer
主要用到里面的 PHPMailerAutoload.php文件
随便在一个一个文件夹中进行测试
PHPMailerAutoload.php完整
<?php
/**
* PHPMailer SPL autoloader.
* PHP Version 5
* @package PHPMailer
* @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
* @author Jim Jagielski (jimjag) <jimjag@gmail.com>
* @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
* @author Brent R. Matzelle (original founder)
* @copyright 2012 - 2014 Marcus Bointon
* @copyright 2010 - 2012 Jim Jagielski
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* PHPMailer SPL autoloader.
* @param string $classname The name of the class to load
*/
function PHPMailerAutoload($classname)
{
//Can't use __DIR__ as it's only in PHP 5.3+
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
if (is_readable($filename)) {
require $filename;
}
}
if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
//SPL autoloading was introduced in PHP 5.1.2
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
spl_autoload_register('PHPMailerAutoload', true, true);
} else {
spl_autoload_register('PHPMailerAutoload');
}
} else {
/**
* Fall back to traditional autoload for old PHP versions
* @param string $classname The name of the class to load
*/
function __autoload($classname)
{
PHPMailerAutoload($classname);
}
}
testMail.php
require( '../PHPMailer/PHPMailerAutoload.php' );只使用一次这个文件
$mail = new PHPMailer (); // 实例化对象和Java一样
$mail->IsSMTP (); // 启用SMTP
$mail->Host = "smtp.qq.com"; // smtp服务器的名称(这里以QQ邮箱为例)
这里介绍一下QQ邮箱获得授权码的方式
打开邮箱(以我的为例)
打开网页版QQ邮箱→设置→账户→POP3/SMTP服务 选择开启(如果已经开启就点击附近的“生成授权码”,会以短信的方式回执得到授权码)
$mail->Username ="532822302@qq.com"; // 你的邮箱名
$mail->Password =""; // 邮箱密码(授权码)
$mail->From = "532822302@qq.com"; // 发件人地址(也就是你的邮箱地址)
$mail->FromName ="Alice88"; // 发件人姓名
- 设置使用ssl加密方式登录鉴权:
$mail->SMTPSecure = 'ssl';
- 设置ssl连接smtp服务器的远程服务器端口号,可选465或587
$mail->Port = 465;
$mail->AddAddress("499941792@qq.com", "尊敬的Miss Wang" );写你要发邮件给谁,还有对他的称呼
$mail->WordWrap = 50; // 设置每行字符长度
$mail->IsHTML(true); // 是否HTML格式邮件
$mail->CharSet = "UTF-8"; // 设置邮件编码
$mail->Subject = "Friday is today!!"; // 邮件主题
$mail->Body = "Happy every day!"; // 邮件内容
$mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; // 邮件正文不支持HTML的备用显示
$mail->send();//发送邮件
最后的发送指令
完整testMail.php代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发送邮件</title>
</head>
<body>
<?php
require( '../PHPMailer/PHPMailerAutoload.php' );
$mail = new PHPMailer (); // 实例化(Java)
// $mail->SMTPDebug = 2;
$mail->IsSMTP (); // 启用SMTP
$mail->Host = "smtp.qq.com"; // smtp服务器的名称(这里以QQ邮箱为例)
$mail->SMTPAuth =true; // 启用smtp认证
$mail->Username ="532822302@qq.com"; // 你的邮箱名
$mail->Password =""; // 邮箱密码(授权码)
$mail->From = "532822302@qq.com"; // 发件人地址(也就是你的邮箱地址)
$mail->FromName ="Alice88"; // 发件人姓名
//设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
// //设置ssl连接smtp服务器的远程服务器端口号,可选465或587
$mail->Port = 465;
$mail->AddAddress("499941792@qq.com", "尊敬的Miss Wang" );
$mail->WordWrap = 50; // 设置每行字符长度
$mail->IsHTML(true); // 是否HTML格式邮件
$mail->CharSet = "UTF-8"; // 设置邮件编码
$mail->Subject = "Friday is today!!"; // 邮件主题
$mail->Body = "Happy every day!"; // 邮件内容
$mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; // 邮件正文不支持HTML的备用显示
$mail->send();//发送邮件
?>
</body>
</html>
效果:
浏览器打开这个
邮箱会接到邮件
完成发送邮件