尝试使用PHPMailer时出现错误

时间:2020-04-05 16:35:42

标签: php phpmailer

我正在创建一个网站作为大学项目,我当然想包括一个联系页面。在此联系页面中,我创建了一个表单,网站的用户可以在其中与我联系(网站的管理员)并向我发送电子邮件。我将显示我立即编写的代码。

您还可以告诉我我应该在哪里编写代码吗?也许这就是我做错了。因此,我第一次在函数中编写该函数,并以联系表的形式调用了该函数。没用然后,我在一个名为send_mail.php的单独php文件中编写了PHPMailer的代码,并在联系表单(这是另一个文件)中,在form标记中编写了属性action =“ send_mail.php”。这是正确的吗?请告诉我我是否做错了。

此外,我想通过我的实际Gmail帐户接收这些电子邮件

我无法使其正常工作,并且出现以下错误:

警告:从第24行的D:\ Xampp \ htdocs \ licenta_ecomm \ public \ send_mail.php中的空值创建默认对象

致命错误:未捕获错误:调用D:\ Xampp \ htdocs \ licenta_ecomm \ public \ send_mail.php:25中未定义的方法stdClass :: isSMTP()堆栈跟踪:#0 {main}抛出D:\ Xampp第25行的\ htdocs \ licenta_ecomm \ public \ send_mail.php

现在,我对此还很陌生,我以前从未使用过PHPMailer,我有点困惑。 这是联系表格(后面是phpmailer代码)

  <div class="container">
    <div class="row">
        <div class="col-lg-12 text-center">
            <h2 class="section-heading">Contacteaza-ne!</h2>
            <h3 class="section-subheading text-muted"><?php display_message(); ?></h3>
        
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            <form name="sentMessage" id="contactForm" action="send_mail.php" method="post" >
             <!--  EVENTUAL FUNCTIE??-->
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <input type="text" name="nume_contact" class="form-control" placeholder="Numele *" id="name" required data-validation-required-message="Introduceți numele dvs.">
                            <p class="help-block text-danger"></p>
                        </div>
                        <div class="form-group">
                            <input type="email" name="email_contact" class="form-control" placeholder="Adresă e-mail *" id="email" required data-validation-required-message="Introduceți adresa e-mail">
                            <p class="help-block text-danger"></p>
                        </div>
                        <div class="form-group">
                            <input type="text" name="subiect" class="form-control" placeholder="Subiect *" id="phone" required data-validation-required-message="Introduceți subiectul">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <textarea name="mesaj" class="form-control" placeholder="Mesajul dvs. *" id="message" required data-validation-required-message="Introduceți mesajul dorit"></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-lg-12 text-center">
                        <div id="success"></div>
                        <button name="submit" type="submit" class="btn btn-xl">Trimiteți mesaj</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

这是我编写的用于发送电子邮件的代码(send_mail.php)

<!DOCTYPE html>
<html>
<?php
    require_once("../resources/config.php"); 

    //clase PHPMailer importante din namespace-ul global
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;

    require 'D:\Xampp\composer\vendor\autoload.php';

    //variabilele din formularul de contact
    $nume_contact = escape_string($_POST['nume_contact']);
    $subiect = escape_string($_POST['subiect']);
    $email_from = escape_string($_POST['email_contact']);
    $mesaj = escape_string($_POST['mesaj']);

    try {

        //Server settings
        $mail->SMTPDebug = 1;                   // Enable verbose debug output
        $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   = 'myemailaddress@gmail.com';                     // SMTP username
        $mail->Password   = 'secret';           // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    
        //sender
        $mail->setFrom('myemailaddress@gmail.com', 'H.P.');

        //recipient
        $mail->addAddress($email_from, $nume_contact);     // Add a recipient
 
        //body content
        $body = "<p>You received a message from: ".$nume_contact.". The message is: ".$mesaj."</p>";

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subiect;
        $mail->Body    = $body;
        $mail->AltBody = strip_tags($body);
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
?>
</html>

0 个答案:

没有答案