PHPMailer表单没有错误或发送

芬恩

在error_log或页面上没有错误。错误日志肯定工作,如果我改变require 'PHPMailer.php';require 'PHPFailer.php';我得到PHP Fatal error: require(): Failed

display_errors设置为true

error_reporting设置为 E_ALL & ~E_NOTICE

PHPMailer 6.1.7-PHP 7.2

PHPMailer.php和Exception.php都在根目录中。在我刚发送到/php/form_error.php时,已注释掉重定向。

任何想法将不胜感激,谢谢。

contact_post.php

<?php
    /*
    THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION
    */
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'PHPMailer.php';
    require 'Exception.php';
    
    /*
    *  CONFIGURE EVERYTHING HERE
    */
    
    // an email address that will be in the From field of the email.
    $fromEmail = '[email protected]';
    $fromName = 'Info domain';
    
    // an email address that will receive the email with the output of the form
    $sendToEmail = '[email protected]';
    $sendToName = 'Info domain';
    
    // subject of the email
    $subject = 'New message from contact form';
    
    // form field names and their translations.
    // array variable name => Text to appear in the email
    $fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
    
    // message that will be displayed when everything is OK :)
    $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
    
    // If something goes wrong, we will display this message.
    $errorMessage = 'There was an error while submitting the form. Please try again later';
    
    /*
     *  LET'S DO THE SENDING
     */
    
    // if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    
    try
    {
        
        if(count($_POST) == 0) throw new \Exception('Form is empty');
        $emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
        $emailTextHtml .= "<table>";
        
        foreach ($_POST as $key => $value) {
            // If the field exists in the $fields array, include it in the email
            if (isset($fields[$key])) {
                $emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
            }
        }
        $emailTextHtml .= "</table><hr>";
        
        $mail = new PHPMailer(true);
        
        $mail->setFrom($fromEmail, $fromName);
        $mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
        $mail->addReplyTo($from);
    
        $mail->isHTML(true);
        
        $mail->Subject = $subject;
        $mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy
        
        if(!$mail->send()) {
            throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
        }
        
        $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
    catch (\Exception $e)
    {
        // $responseArray = array('type' => 'danger', 'message' => $errorMessage);
        $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
    }
    
    
    // if requested by AJAX request return JSON response
    if ($responseArray['type'] == 'success') {
        // success redirect
    
       //header('Location: /php/form_complete.php');
    }
    else {
        //error redirect
        //header('Location: /php/form_error.php');
    }

contact.html上的表格

<form id="contact-form" method="post" action="/contact_post.php" role="form">

    <div class="messages"></div>

    <div class="controls">

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="form_name">First name*</label>
                    <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your first name *" required="required" data-error="First name is required.">
                    <div class="help-block with-errors"></div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label for="form_lastname">Last name*</label>
                    <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your last name *" required="required" data-error="Last name is required.">
                    <div class="help-block with-errors"></div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="form_email">Email*</label>
                    <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                    <div class="help-block with-errors"></div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label for="form_need">Please specify your need*</label>
                    <select id="form_need" name="need" class="form-control" required="required" data-error="Please specify your need.">
                        <option value=""></option>
                        <option value="Request quotation">Request quotation</option>
                        <option value="Request order status">Request order status</option>
                        <option value="Request copy of an invoice">Request copy of an invoice</option>
                        <option value="Other">Other</option>
                    </select>
                    <div class="help-block with-errors"></div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="form_message">Message*</label>
                    <textarea id="form_message" name="message" class="form-control" placeholder="Please enter message here*" rows="4" required="required" data-error="Please, leave us a message."></textarea>
                    <div class="help-block with-errors"></div>
                </div>
            </div>
            <div class="col-md-12">
                <input type="submit" class="btn btn-success btn-send" value="Send message">
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <p class="text-muted">
                    <strong>*</strong> These fields are required.
                </p>
            </div>
        </div>
    </div>

</form>

htaccess文件

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Options -Indexes
同步

根据我的评论,当出现问题时,您什么也不做,因此没什么可看的。

要查看实际出了什么问题,请在出现问题时输出一些有用的信息,例如:

catch (\Exception $e)
{
    $responseArray = ['type' => 'danger', 'message' => $e->getMessage()];
    var_dump($responseArray, $mail->ErrorInfo);
}

一件事:

    if(!$mail->send()) {
        throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
    }

不需要这样做-PHPMailer已经设置为抛出异常,因此应该是:

$mail->send();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

代码显示没有错误,但不会将结果发送

Symfony中的无效表单没有错误

命令行中的SMS Twilio API错误,但浏览器中没有错误,但没有发送消息

Laravel 5.5 Mailgun不发送电子邮件-没有错误

表单不将数据传递到数据库,但没有错误Laravel 5.8.22

PHPmail-Contact表单未收到邮件且没有错误

如何知道是否在伪造者中提交表单时没有错误

Flask wtf表单未提交且没有错误反馈

为什么此登录表单会失败且没有错误?

表单不更新值,验证失败但没有错误

没有错误吗?

CakeEmail未发送,但没有错误

发送邮件任务似乎运行没有错误,但不发送邮件

表单验证jQuery没有错误

从Curl提交表单但在浏览器中提交-没有错误记录

尝试使用jQuery将表单自动提交到Ajax函数失败,没有错误

PHP如何提交表单,如果没有错误。没有JavaScript

PHP表单到数据库-没有错误,但没有结果

没有错误的LaTeX文档错误

Django 表单发布没有错误的数据,但未注册为“is_valid”

承诺不发送任何数据。没有错误?

PHP 邮件没有发送到电子邮件,但没有错误

表单未验证且没有错误。包含CSRF令牌

fetch API : 只有在 POST 表单中没有错误时才重定向

使用 smtplib 发送电子邮件,没有错误但没有发送消息

Node.JS:即使没有错误,Nodemailer 也不发送电子邮件

PHPMailer:似乎没有发送密码

嵌入不发送,我没有错误

PHP删除表单不删除没有错误