我只想制作一个基本的电子邮件发送脚本。因为我想在本地主机上运行它,所以我使用Gmail SMTP。我都启用了IMAP设置,并且允许从不太安全的应用程序访问。令人惊讶的是,我仍然无法连接。每次尝试时,我都会收到错误SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
为什么会这样?我正在使用此代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['submit'])){
$to = $_POST['email'];
$subject = "First email";
$txt = "Do you want our service?";
//require_once('PHPMailer/PHPMailer.php');
require_once "PHPMailer\PHPMailer.php";
require_once "PHPMailer\SMTP.php";
require_once "PHPMailer\Exception.php";
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->isHTML();
$mail->Username = "myemailnotimportant@gmail.com";
$mail->Password = "password123";
$mail->SetFrom('myemailnotimportant@gmail.com', 'Sir Walter Scott');
$mail->Subject = $subject;
$mail->Body = $txt;
$mail->AddAddress($to);
if ($mail->Send()) {
echo "email sent";
} else {
echo "Something is wrong: <br><br>" . $mail->ErrorInfo;
}
}
?>