php mail
首先,php邮箱发送需要使用第三方插件,
PHPMailer git :https://github.com/PHPMailer/PHPMailer
安装
可在项目目录使用composer安装,当然前提电脑上已经安装过composer
composer require phpmailer/phpmailer
或者可以直接去git下载程序,然后直接引入,具体可以参考作者git里说明,
我这边使用的composer安装的,下面演示composer方式,
//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\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //开启bug提示,测试完毕后可注释掉
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.qiye.aliyun.com'; //SMTP服务器地址 可看各大邮箱官网提供的地址
$mail->SMTPAuth = true; //启用SMTPAuth服务
$mail->Username = '例如66@66.com'; //邮箱帐号
$mail->Password = '密码'; //邮箱密码
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption 默认SSL模式
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` 默认465 SSL端口
$mail->setFrom('例如66@66.com', '系统通知'); ///发件人地址和发件人昵称 必填
$mail->addAddress('收件人'); //收件人地址
//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>';//邮箱内容 可以使用html
//普通内容模式,不使用html
$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}";
}
中文出现乱码情况,可以设置编码
$mail->CharSet = 'UTF-8';