我在尝试使用PHP的PHPMailer库发送电子邮件时遇到了一个问题。我收到一个错误,说“无法发送邮件”。我已经尝试了不同的方法来发送邮件,即使我得到了ChatGpt的帮助,但我不能。我已经遵循了文档,我的代码看起来像这样:
<form action="mail.php" method="post">
<input type="text" name="name">
<input type="email" name="email" id="">
<input type="submit" value="submit">
</form>
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require __DIR__ . '/PHPMailer-master/PHPMailer.php';
require __DIR__ . '/PHPMailer-master/Exception.php';
require __DIR__ . '/PHPMailer-master/SMTP.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer(true);
try {
// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mypassword'; // Replace with your Gmail password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Sender and recipient details
$mail->setFrom('[email protected]', 'Ahmed Maajid');
$mail->addAddress('[email protected]'); // Set your Gmail address as both sender and recipient
// Email content
$mail->isHTML(false);
$mail->Subject = 'New submission from your website';
$mail->Body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email;
$mail->send();
echo 'Email sent successfully!';
} catch (Exception $e) {
echo 'Oops! There was a problem: ' . $mail->ErrorInfo;
}
}
?>
字符串
2条答案
按热度按时间cpjpxq1n1#
您可以重新检查以下内容
奖励临时启用debug true,以便您可以了解更多有关错误的信息
字符串
谢谢你,谢谢
vdgimpew2#
Composer是启动和运行PHPMailer的最简单方法。只需打开终端并输入:
composer require phpmailer/phpmailer
。以下是发送电子邮件的代码片段:字符串