[英]using AngularJs, trying to send email using an HTML Contact-Us Form via PHP on Google App Engine
以下是我的聯系表單的 html代碼
<div class="col">
<h3>Say hello</h3>
<div ng-show="error" class="error">
<p>Something wrong happened!, please try again.</p>
</div>
<form method="post" role="form" ng-submit="sendMessage(input)" class="contactForm" name="form" id="contact_form" >
<input type="text" name="firstname" id="firstname" placeholder="Name" required="required" ng-model="input.name">
<input type="text" name="telephone" id="telephone" placeholder="Telephone" required="required" ng-model="input.telephone">
<input type="text" name="email" id="email" placeholder="Email" required="required" ng-model="input.email">
<textarea name="comments" maxlength="1000" cols="20" rows="3" placeholder="Comment" required="required" ng-model="input.message"></textarea>
<button type="submit" name="submit" value="submit" ng-disabled="!form.$valid">Submit</button>
</form>
<!-- <div ng-show="process" style="text-align:center">
<img class="loader" src="" />
Sending ...
</div> -->
<div ng-show="success" class="success">
<p>Thank you for taking the time to contact us</p>
<p>Have A Great Day!</p>
</div>
<div class="clearboth"></div>
</div>
我來自app.js的代碼
var app = angular.module('example', []).
run(['$rootScope', '$http', '$browser', '$timeout', "$route", function ($scope, $http, $browser, $timeout, $route) {
$scope.sendMessage = function( input ) {
input.submit = true;
$scope.success = false;
$scope.error = false;
$scope.loaded = true;
$scope.process = true;
$http({
method: 'POST',
url: 'js/sendemail.php',
data: input,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success( function(data) {
if ( data.success ) {
$scope.success = true;
$scope.process = false;
$("form").trigger("reset");
} else {
alert("error :" +data);
$scope.error = true;
}
});
};
下面是我的sendemail.php代碼
<?php
$response = array( 'success' => false );
$formData = file_get_contents( 'php://input' );
$data = json_decode( $formData );
if ( $data->submit ) {
$name = $data->name;
$email = $data->email;
$telephone = $data->telephone;
$message = $data->message;
if ( $name != '' && $email != '' && $message != '' && $telephone != '') {
$mailTo = 'xxx@gmail.com';
$subject = 'New Contact Form Submission';
$body = 'From: ' . $name . "\n";
$body .= 'Email: ' . $email . "\n";
$body .= 'Telephone: ' . $telephone . "\n";
$body .= "Message:\n" . $message . "\n\n";
$success = mail( $mailTo, $subject, $body );
if ( $success ) {
$response[ 'success' ] = true;
} else {
$response[ 'success' ] = false;
}
}
}
echo json_encode( $response );
?>
我將日志保存在JavaScript sendMessage函數中,以檢查數據是否來自html。 是的,它來了。 我可以在警報消息中看到數據。
我瀏覽了許多鏈接,並更新了Google App Engine->設置->電子郵件地址。
當我單擊表單中的提交按鈕時。 我可以看到我用JS alert(“ error” + data)編寫的警報消息; 警報框包含整個php代碼。
下面也是我的app.yaml文件
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: static/index.html
upload: static/
- url: /images
static_dir: static/images
- url: /css
static_dir: static/css
- url: /js
static_dir: static/js
- url: /pages
static_dir: static/pages
- url: /static
static_dir: static
- url: /(.+\.php)
script: \1
- url: /.*
script: start.application
有人可以幫我解決這個問題。
嘗試使用Google App Engine郵件API(而不是PHP)代替python應用程序: sending-mail-with-mail-api ,您仍然可以使用Angular進行數據綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.