HTML for无法发布我的PHP脚本,为什么?

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

我正在使用SendGrid通过HTML表单发送电子邮件。 HTML对我来说看起来正确,PHP对我也正确,但是当我单击“提交”按钮时,我被定向到仅显示Cannot POST /sendmail.php的页面。我是PHP新手,但是代码看起来正确。

在错误消息/页面的控制台中,有两个错误。

[第一个这样说:Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-ThhI8UaSFEbbl6cISiZpnJ4Z44uNSq2tPKgyRTD3LyU='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

[第二个说:Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.

这些错误消息是否相关?

[我还应该提到我正在NodeJS服务器上运行表单。在本地主机上运行时,邮件脚本能工作吗?这是我的HTML:

<!DOCTYPE html>
<html>
  <body>
    <form name="form" action="sendmail.php" method="POST">
        <input type="text" name="name" placeholder="name">
        <input type="text" name="email" placeholder="email">
        <input type="text" name="message" placeholder="message">
        <button name="submit" type="submit">Submit</button>
    </form>
  </body>
</html>

这是我的PHP

<?php
/*SendGrid Library*/
require 'vendor/autoload.php';

if (isset($_POST['submit'])) {
    /*Post Data*/
    $user_name = $_POST['name'];
    $user_email = $_POST['email'];
    $user_message = $_POST['message'];

    /*Content*/
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom("$user_email", "$user_name");
    $email->setSubject("Send Email Attachments with Twilio SendGrid");
    $email->addTo("[email protected]", "Example User");
    $email->addContent(
       "text/html",
       "How easy can this be?"
    );

    $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));


    /*Response*/
    try {
        $response = $sendgrid->send($email);
        print $response->statusCode() . "\n";
        print_r($response->headers());
        print $response->body() . "\n";
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."\n";
    }
}
?>

所有相关问题对我不是很有帮助。谢谢

php html forms http-post sendgrid
1个回答
0
投票

很明显,您的问题出在CSP(内容安全策略)上。

非常好的指导:https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP

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