我想用php(使用windows操作系统)发送电子邮件,.However我一直遇到连接问题,附件文件不是found.Are,有什么解决方案吗?
//-------------sending mail----------------------------
//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;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
//Create an instance; passing true enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'username@gmail.com'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('hotelpage@gmail.com', 'Hotels Page');
$mail->addAddress('MohanDepesh@gmail.com', 'MohanDepesh'); //Add a recipient
$mail->addReplyTo('no-reply@gmail.com', 'no-reply');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//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>';
$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}";
}

发布于 2022-07-05 12:23:19
请核对你收到错误的以下几点-
1.检查你是否使用正确的邮件和密码。2.由于“允许不太安全的应用程序”,settings.Try可能会启动此设置进行测试。如果您仍然面临问题,请附上准确的错误。
编辑:由于允许不太安全的应用程序现在是禁用的,您可以使用appPassword。
https://stackoverflow.com/questions/72868999
复制相似问题