使用PHPMailer用PHP发送电子邮件时遇到问题:出现“无法发送邮件”错误

7hiiyaii  于 11个月前  发布在  PHP
关注(0)|答案(2)|浏览(137)

我在尝试使用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;
    }
}
?>

字符串

cpjpxq1n

cpjpxq1n1#

您可以重新检查以下内容

  • 您的Gmail密码实际上并不是用于登录Gmail帐户的密码。如果您使用的是Google的SMTP,则应该是密码生成持续时间应用密码创建。
  • 如果你使用localhost,你仍然需要连接到互联网(如果你使用SMTP)
  • 我希望你已经用你自己的配置修改了.示例字段。
  • 您可以参考网页了解如何为PHPMailer设置Google SMTP
    奖励临时启用debug true,以便您可以了解更多有关错误的信息
$mail->SMTPDebug = true;

字符串
谢谢你,谢谢

vdgimpew

vdgimpew2#

Composer是启动和运行PHPMailer的最简单方法。只需打开终端并输入:composer require phpmailer/phpmailer。以下是发送电子邮件的代码片段:

require("./vendor/phpmailer/phpmailer/src/PHPMailer.php");
require("./vendor/phpmailer/phpmailer/src/SMTP.php");

// make all the setup to send email
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSmtp();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; 
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "your_password";
$mail->setFrom("[email protected]");
// make email subject & body
$subject = "testing subject";
$messageBody = "testing message";
$mail->Subject = $subject;
$mail->Body = $messageBody;
// send to
$sendTo = "[email protected]";
$mail->AddAddress($sendTo);
// send request
if($mail->Send()) {
    echo 'Email has been sent!';
} else {
    echo $mail->ErrorInfo;
}

字符串

相关问题