【问题标题】:Node JS app not processing php mailer codeNode JS 应用程序不处理 php 邮件程序代码
【发布时间】:2017-09-17 09:34:07
【问题描述】:

我有一个基于 Node JS 框架的简单网站,带有一个 php 邮件程序来处理我的联系表。我似乎无法让它工作,我想知道 Node JS 本身是否存在无法处理 php 代码的固有问题。它有一个 html 页面 index.html,其中包含一个联系表单代码:

 <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <h2 class="featurette-heading" style="text-align: center;">Message</h2>
                <h3 class="text-muted">Want to book an appointment? Send me a message and I'll contact you. Make sure you include your name, phone number, email, and when you'd like to book. It's that easy!</h3>
                <form name="sentMessage" id="contactForm" action="contact_me.php" method="post" novalidate>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="tel" class="form-control" placeholder="Phone Number" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <textarea rows="5" class="form-control" placeholder="Message" id="message" name="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                        <div class="row">
                            <div class="form-group col-xs-12">
                                <button type="submit" class="btn btn-primary">Send</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

在同一文件夹级别,有一个contact_me.php文件:

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
  empty($_POST['email'])        ||
  empty($_POST['phone'])        ||
  empty($_POST['message'])  ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
  {
  echo "No arguments Provided!";
  return false;
  }

$to = 'jcbridwe@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: 
$email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers = "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;            
?> 

每当我点击表单底部的“提交”时,都会收到以下错误:“无法发布 /contact_me.php”。经过一番实验,我在php代码中添加了一个echo语句(我对php的了解很有限),并确定在node服务器运行时,php似乎没有处理。如果是这种情况,我该怎么做才能解决这个问题?我需要添加 route.js 或 index.js(在 Node 的 Express Server 文件夹下)中的内容以使 php 邮件程序正常工作吗?

【问题讨论】:

  • 你是如何运行你的节点的?你的 php 文件在哪里?谁在处理 php 的请求?
  • 为什么你不能从节点发送电子邮件?为什么你需要 php 来做呢?
  • 1.用replacing yourname@yourdomain.com ...message to. 注释掉字符串 2. 标题Reply-To: $email_address 必须没有换行符。
  • @Panther - 当您问“您如何运行节点?”时,我不确定您的意思是什么?我在 Cloud9 IDE 上拥有该应用程序,我使用 $node server 命令运行它,并将更改推送到 git 和 heroku 以获得实时版本。这是一个简单的网站,使用 NodeJS 可能有点矫枉过正,但我​​想我将来可能需要添加功能。 index.html 和contact_me.php 文件都位于同一级别的客户端文件夹下。处理php?我没有使用 AJAX,如果这就是你的意思。我使用 php 是因为我认为它很简单,我只需要一个简单的 action 命令。
  • Node.js 对 php 一无所知。所以你需要有一个知道它的服务器(Apache、ngnix...),或者从你的 Node.js 脚本运行命令行 php 解释器(可能是最糟糕的想法),或者编写表单处理和Javascript 中的邮件代码。

标签: javascript php html node.js phpmailer


【解决方案1】:

我学习 NodeJS 一样。为什么你不想在 ExpressJS 中使用Nodemailer?它易于使用和设置。

【讨论】:

  • 好的,我正在尝试找出 Nodemailer。在nodemailer.com/usage。我已经通过$npm install nodemailer 将nodemailer 安装到node_modules 目录中。然后它会告诉您使用 SMTP 或类似的东西“创建一个传输对象”。给出了一个代码示例,但它没有告诉您代码在哪里(在什么文件中,在什么文件夹中)。它也没有解释它是如何连接到我的 index.html 中的表单的。你能告诉我传输对象代码在哪里吗?我正在看一些教程,但它们都遗漏了这些重要的细节。
  • into my repo我做了工作示例
  • 太好了,谢谢!所以本质上,传输对象和配置位于 Express 中的 routes 文件夹下的 index.js 文件中。这正是我一直在寻找的答案。我正在努力让它插上电源。
  • 我还有一个问题。您的示例中的 index.js 文件是什么文件夹。是放在 node_modules/express 还是 node_modules/nodemailer 下。我的 nodemailer 目录下没有任何名为“routes”的文件。我的 express 目录下确实有一个名为“routers”的文件夹,但这似乎不是添加该代码的正确位置。或者,我是否要在包含 routes/index.js 文件以及所有随附文件夹和文件的客户端目录下添加一个名为“nodemailer”的文件夹?
  • 我使用 Express 应用程序生成器来启动 expressjs 项目。 Link to turtorial。在 CLI 中调用 express 后,您将从一开始就拥有所有标准文件和文件夹。我在示例中使用的所有依赖项都可以在 package.json 文件中找到。只需输入npm install --save 即可安装依赖项
猜你喜欢
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 2021-08-07
  • 2019-12-21
  • 2015-11-10
  • 1970-01-01
相关资源
最近更新 更多