如何解决 php 邮件程序不发送电子邮件的问题

问题描述 投票:0回答:1

我想制作一个工作联系表单,以便在用户填写表单时向我发送电子邮件。我想使用 phpmailer,因为我将 localhost 与 xampp 一起使用。我有这个代码:

<?php
    session_start();
    
    require "C:/xampp/htdocs/Composer/vendor/autoload.php";

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception; 

    require "C:/xampp/htdocs/Composer/vendor/phpmailer/phpmailer/src/PHPMailer.php";
    require "C:/xampp/htdocs/Composer/vendor/phpmailer/phpmailer/src/SMTP.php";
    require "C:/xampp/htdocs/Composer/vendor/phpmailer/phpmailer/src/Exception.php";
?>

这是我的 php 文件的标头和实际发送电子邮件的代码:

<?php

                $message = "";                        

                if(isset($_POST["btn"]))
                {
                    $userName = $_POST["username"];
                    $userPhone = $_POST["userphone"];
                    $userEmail = $_POST["useremail"];
                    $userSubject = $_POST["usersubject"];
                    $userMessage = $_POST["usermessage"];
                    $toemail = "myemail";

                    $mail = new PHPMailer(true);

                    try
                    {
                        $mail -> isSMTP();
                        $mail -> Host = 'smtp.gmail.com';
                        $mail-> SMTPAuth = true;
                        $mail-> Username = 'myemail';
                        $mail-> Password = 'mypassword'; 
                        $mail-> SMTPSecure = 'tls';             
                        $mail-> Port = 587;

                        $mail -> setFrom($userEmail, $userName);
                        $mail -> addAddress($toemail);

                        $mail -> isHTML(false);
                        $mail -> Subject = $userSubject;
                        $mail -> Body = "Name: $userName\r\n" .
                                        "Phone: $userPhone\r\n" .
                                        "Message: $userMessage\r\n";

                        $mail -> send();
                        $message = "Your information has been received";
                    }
                    catch(Exception $e)
                    {
                        $message = "Error sending email. Please try again later";
                    }
                }
            ?>

但是当我填写表单并单击“提交”时,我收到错误消息,这意味着 try 块没有执行。我也收到此错误未定义类型 PHPMailer\PHPMailer\PHPMailer 在这一行中:

$mail = new PHPMailer(true);
和未定义类型 PHPMailer\PHPMailer\Exception 在这一行中:
catch(Exception $e)
。我已经下载了 phpmailer 和作曲家,并且我已经仔细检查了路径,所以我认为这些不是问题。该错误来自 intelephense P1009。我还问了 gpt,但它说是路径或类似的东西,但就像我说的,我仔细检查了它们。

php smtp
1个回答
0
投票

使用

phpmailer
 获取 
Composer

composer require phpmailer/phpmailer

然后,在你的代码中

require "path/to/vendor/autoload.php"; //PHPMailer Object
use PHPMailer\PHPMailer\PHPMailer; //PHPMailer class from the PHPMailer\PHPMailer namespace will be used in the current script
use PHPMailer\PHPMailer\Exception; //Exception class from the PHPMailer\PHPMailer namespace will be used in the current script

尝试一下

© www.soinside.com 2019 - 2024. All rights reserved.