如果您正在使用Apache托管服务器,并且需要配置SMTP,有两种方法可以实现。
方法一:使用PHPMailer
要使用PHPMailer,您需要在您的网站上安装它并将以下代码添加到您的PHP脚本中:
Host = 'smtp.gmail.com';
//启用SMTP验证
$mail->SMTPAuth = true;
// SMTP 用户名(您的电子邮件地址)
$mail->Username = 'your.email@example.com';
// SMTP 密码
$mail->Password = 'your-email-password';
// SMTP端口号
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = 'tls';
//设置邮件发送者
$mail->setFrom('from@example.com', 'From Name');
//设置邮件接收者
$mail->addAddress('recipient@example.com', 'Recipient Name');
//邮件主题
$mail->Subject = 'Testing PHPMailer';
//邮件正文
$mail->Body = 'This is a test email.';
//发送邮件
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
请确保将代码中的以下值替换为您的值: