【发布时间】: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