发布于 5年前
PHP使用phpmailer发送邮件
1、安装邮件发送扩展
composer require phpmailer/phpmailer
2、发送邮件配置和发送
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mxhichina.com'; // 这里使用阿里云的企业邮箱服务器
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '你的邮箱用户名'; // SMTP username
$mail->Password = '你的邮箱密码'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('来源邮箱地址', '小智');
$mail->addAddress('接收人邮箱', '邮箱昵称'); // Add a recipient
$mail->addReplyTo('来源邮箱地址', '小智');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
// Attachments
$mail->addAttachment('附件相对路径地址','附件名称'); // Add attachments
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = '邮件标题';
$mail->Body = '邮件正文';
$mail->send();
echo '邮件已发送';
} catch (Exception $e) {
echo "邮件未发送成功,错误信息: {$mail->ErrorInfo}";
}