【问题标题】:Submitting form from interior (2nd level) pages从内部(第二级)页面提交表格
【发布时间】:2019-07-01 18:35:32
【问题描述】:

我的网站页脚中有一个使用 PHPMailer 的联系表格。所以它显示在所有页面上,因为它是模板的一部分。它在“顶级”页面(即 www.mywebsite.com/index.html)上完美运行,但在内部(或二级页面,即 www.mywebsite.com/pricing/basic.html)上它不发送。控制台说它找不到位于我的站点目录根目录中的“contact.php”文件。

我尝试添加“../contact.php”作为表单操作以向上移动,但仍然找不到。

<?php

/*
THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION
*/

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

require 'PHPMailer-master/vendor/autoload.php';

/*
*  CONFIGURE EVERYTHING HERE
*/

// an email address that will be in the From field of the email.
$fromEmail = 'xxxxxxxx';
$fromName = 'No Reply Email';

// an email address that will receive the email with the output of the form
$sendToEmail = 'xxxxxx';
$sendToName = 'New Contact Form Message';

// 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:', 'email' => 'Email:', 'message' => 'Message:');

// message that will be displayed when everything is OK :)
$okMessage = 'Successfully submitted - we 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);
error_reporting(E_ALL & ~E_NOTICE);

try {

    if (count($_POST) == 0) throw new \Exception('Form is empty');
    $emailTextHtml .= "<h3>New message from xxxxx xxxxx:</h3><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>";
    $emailTextHtml .= "<p>Have a great day!<br><br>Sincerely,<br><br>xxxx xxxx</p>";

    $mail = new PHPMailer;

    $mail->setFrom($fromEmail, $fromName);
    $mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
    $mail->addReplyTo($_POST['email'], $_POST['name']);



    $mail->Subject = $subject;

    $mail->Body = $emailTextHtml;
    $mail->isHTML(true);
    //$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy


    if (!$mail->send()) {
        throw new \Exception('Email send failed. ' . $mail->ErrorInfo);
    }

    $responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
    // $responseArray = array('type' => 'warning', 'message' => $errorMessage);
    $responseArray = array('type' => 'warning', 'message' => $e->getMessage());
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

    这听起来与此脚本中的内容无关,而与从何处加载有关。尝试将表单操作指向绝对路径而不是相对路径,即action="/contact.php" 而不是相对路径action="contact.php"action="../contact.php"。这样,无论从哪里提交,它都会始终指向同一个脚本。

    与您描述的问题不同,这行看起来很可疑:

    require 'PHPMailer-master/vendor/autoload.php';
    

    这表明您可能正在使用 PHPMailer 自己的composer.json 文件;只有在on PHPMailer 上工作时才应该使用它,而不仅仅是在项目中使用它。您通常希望 vendor 文件夹和 composer.json 文件位于您自己项目的顶层,并且 PHPMailer 的文件会被 composer 放入 vendor/phpmailer - 尽管您不必关心这一点,因为vendor/autoload.php 会弄清楚的。请阅读自述文件,了解如何使用 Composer 在您自己的项目中安装 PHPMailer。

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2012-12-15
      • 2016-11-10
      相关资源
      最近更新 更多