去官方的GitHub下载包
"phpmailer/phpmailer": "~6.0"
composer require phpmailer/phpmailer
<?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;
// Load Composer's autoloader
require 'vendor/autoload.php'; //框架支持自动加载就不用写
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true); //发送失败抛出异常
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$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
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
另外一个方法:
https://blog.csdn.net/qq_39864544/article/details/86655656
public function send(){
$mail = new PHPMailer();
$toemail = '1374624878@qq.com';//收件人
$mail->isSMTP();// 使用SMTP服务
$mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
$mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
$mail->SMTPAuth = true;// 是否使用身份验证
$mail->Username = "maiiiiiid@163.com";/// 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱
$mail->Password = "maiiiiiid142857";// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
$mail->SMTPSecure = "ssl";// 使用ssl协议方式
$mail->Port = 465;// 163邮箱的ssl协议方式端口号是465/994
$mail->setFrom("maiiiiiid@163.com","Maid");// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
$mail->addAddress($toemail,'Wang');// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
$mail->addReplyTo("maiiiiiid@163.com","Reply");// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
//$mail->addCC("xxx@163.com");// 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址(这个人也能收到邮件)
//$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
//$mail->addAttachment("bug0.jpg");// 添加附件
$mail->Subject = "注册验证";// 邮件标题
$num = rand(100000,999999);
$mail->Body = "邮件内容是 :您的验证码是:".$num;// 邮件正文
//$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用
if(!$mail->send()){// 发送邮件
// echo "Message could not be sent.";
// echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
echo 2;
}else{
echo 1; //成功
}
}
邮件内容必须是带有中文吗?
在common.php进行封装:
use PHPMailer\PHPMailer;
use PHPMailer\Exception;
function send_mail($to,$title = "注册验证",$content = "邮件内容是 :您的验证码是:"){
$mail = new PHPMailer();
$mail->isSMTP();// 使用SMTP服务
$mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
$mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
$mail->SMTPAuth = true;// 是否使用身份验证
$mail->Username = "maiiiiiid@163.com";/// 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱
$mail->Password = "maiiiiiid142857";// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
$mail->SMTPSecure = "ssl";// 使用ssl协议方式
$mail->Port = 465;// 163邮箱的ssl协议方式端口号是465/994
$mail->setFrom("maiiiiiid@163.com","Maid");// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
$mail->addAddress($to,'Wang');// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
$mail->addReplyTo("maiiiiiid@163.com","Reply");// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
//$mail->addCC("xxx@163.com");// 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址(这个人也能收到邮件)
//$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
//$mail->addAttachment("bug0.jpg");// 添加附件
$mail->Subject = $title;// 邮件标题
$num = rand(100000,999999);
$mail->Body = $content . $num;// 邮件正文
//$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用
if(!$mail->send()){// 发送邮件
// echo "Message could not be sent.";
// echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
echo 2;
}else{
echo 1; //成功
}
}
网友评论