使用composer 实现邮件发送
使用composer安装PHPmailer 插件然后就可以很方便的使用邮件发送了
首先进入目录安装phpmailer
composer require phpmailer/phpmailer
2.等待一会儿就安装好了(请自行分辨是不是安装成功)
安装完成之后直接 use require 接着就是正经的发送邮件的代码了
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require_once "./vendor/autoload.php"; $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = 1; // Enable verbose debug output $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp.sina.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'username@sina.com'; // SMTP username $mail->Password = 'password'; // SMTP password $mail->SMTPSecure = 'ssl'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged $mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above //Recipients $mail->setFrom('username@sina.com', 'cleverzhm'); $mail->addAddress('814030691@qq.com', '爸爸'); // Add a recipient // 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>'; $result = $mail->send(); print_r($result); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }