2017-04-20 64 views
1

我有一個簡單的網站建立在節點JS框架與PHP郵件處理我的聯繫表格。我似乎無法得到它的工作,我想知道是否有一個固有的問題,節點JS本身無法處理PHP代碼。它有一個HTML頁面的index.html,其中包含的聯繫表格代碼:節點JS應用程序不處理PHP郵件代碼

<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 = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - 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: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected] 
$headers = "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers); 
return true;    
?> 

每當我打「提交「在我的表單的底部,我得到以下錯誤:」不能POST POST /contact_me.php「。經過一番實驗後,我在php代碼中添加了一條echo聲明(我對php的理解非常有限),並確定在節點服務器運行時,php似乎沒有處理。如果是這樣的話,我能做些什麼來解決這個問題? route.js或index.js(Node的Express Server文件夾下)中是否有某些內容需要添加,才能使php郵件程序正常工作?

+0

你如何運行你的節點?你的php文件在哪裏?誰在處理php的請求? – Panther

+0

以及爲什麼不能發送來自節點的電子郵件?爲什麼你需要php來做到這一點? – Panther

+0

1.用'替換[email protected] ...'和'message to.'來註釋掉字符串2.標題'Reply-To:$ email_address'必須沒有換行符。 – Deadooshka

回答

0

我學習NodeJS一樣。爲什麼你不想在ExpressJS上使用Nodemailer?它易於使用和設置。

+0

好的,我想弄清楚Nodemailer。在https://nodemailer.com/usage/。我已經通過'$ npm install nodemailer'將nodemailer安裝到'node_modules'目錄中。然後它會告訴你使用SMTP或類似的東西來「創建一個傳輸對象」。給出了一個代碼示例,但它不會告訴你代碼在哪裏(在哪個文件中,在哪個文件夾中)。它也沒有解釋它如何連接到我的index.html中的表單。你能告訴我傳輸對象代碼在哪裏?我正在看一些教程,但他們都遺漏了這些重要的細節。 – jcbridwe

+0

看[進我的回購](https://github.com/PinkyRabbit/usenodemailer)我做了工作的例子 –

+0

這很好,謝謝!因此,本質上,傳輸對象和配置將放入Express中的routes文件夾下的index.js文件中。這正是我一直在尋找的答案。我正在努力讓它現在插入。 – jcbridwe