ps:学习于慕课网视频地址
1:发邮件
github插件地址
示例
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//引入类
$rootPath = dirname(__FILE__);//获取项目目录用于定义绝对路径
//Load Composer's autoloader
require $rootPath.'/PHPMailer/src/Exception.php';
require $rootPath.'/PHPMailer/src/PHPMailer.php';
require $rootPath.'/PHPMailer/src/SMTP.php';
require $rootPath.'/PHPMailer/vendor/autoload.php';
//实例化类
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
//$mail->SMTPDebug = 2; // Enable verbose debug output如果邮件发送不成功打开此选项用于调试bug
$mail->isSMTP(); // Set mailer to use SMTP设置smtp服务器
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers如smtp.alyu.com
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->CharSet = 'UTF-8'; //设置邮件中文编码
$mail->Username = 'user@example.com'; // SMTP username发送账号
$mail->Password = 'secret'; // SMTP password密码
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted加密方式
//$mail->Port = 587; // TCP port to connect to端口
//Recipients
$mail->setFrom('from@example.com', 'Mailer'); //发件地址,人
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient收件地址,人
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information'); //回复邮箱
//$mail->addCC('cc@example.com'); //抄送
//$mail->addBCC('bcc@example.com'); //密送
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments附件
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML邮件正文是否有带HTML标签如果有设为true
$mail->Subject = 'Here is the subject'; //标题
$mail->Body = 'This is the HTML message body <b>in bold!</b>';//正文
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';//没有带HTML内容
if($mail->send()){
echo 'Message has been sent';
}else{
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}