AngularJS联系表单无法发送邮件:报错及代码求助
解决联系表单的控制台错误与邮件发送问题
首先,咱们先搞定那个控制台报错:ReferenceError: formData is not defined。你在控制器里写了console.log(formData);,但这里的formData是挂载在$scope上的变量,必须写成console.log($scope.formData);才能正确访问,这就是报错的直接原因。
接下来咱们解决邮件发送的核心问题——目前你的前端和后端数据传递格式不匹配,导致PHP无法正确接收数据,具体调整方案如下:
1. 修正AngularJS控制器代码
推荐两种数据传递方案,你可以根据需求选择:
方案A:使用URL编码格式(适配PHP的$_POST)
引入$httpParamSerializerJQLike序列化表单数据,让PHP能直接通过$_POST获取值,同时添加请求成功/失败的回调处理:
app.controller("contactController", ['$scope', '$http', '$httpParamSerializerJQLike', function ($scope, $http, $httpParamSerializerJQLike) { $scope.formData = { subject: '', email: '', message: '' }; // 修复console.log的错误 console.log($scope.formData); $scope.processForm = function () { $http({ method: 'POST', url: 'contact.php', data: $httpParamSerializerJQLike($scope.formData), headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(function(response) { // 请求成功后的提示 console.log(response.data); alert(response.data); }) .catch(function(error) { // 请求失败的错误处理 console.error('请求出错:', error); }); }; }]);
方案B:使用JSON格式传递数据
直接发送JSON格式数据,修改请求头为application/json,让后端明确接收格式:
app.controller("contactController", ['$scope', '$http', function ($scope, $http) { $scope.formData = { subject: '', email: '', message: '' }; console.log($scope.formData); $scope.processForm = function () { $http({ method: 'POST', url: 'contact.php', data: $scope.formData, headers: { 'Content-Type': 'application/json' } }) .then(function(response) { console.log(response.data); alert(response.data); }) .catch(function(error) { console.error('请求出错:', error); }); }; }]);
2. 修正PHP代码
根据你选择的前端方案,对应调整后端代码:
对应方案A(URL编码格式)
直接用$_POST获取数据,同时添加基础的非空判断,避免空值导致的异常:
<?php // 校验请求方法 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取并清洗表单数据 $subject = isset($_POST['subject']) ? trim($_POST['subject']) : '无主题'; $userEmail = isset($_POST['email']) ? trim($_POST['email']) : ''; $message = isset($_POST['message']) ? trim($_POST['message']) : ''; // 邮件配置 $to = "lurtze@hotmail.com"; $headers = 'From: ' . $userEmail . "\r\n" . 'Reply-To: ' . $userEmail . "\r\n" . 'X-Mailer: PHP/' . phpversion(); // 发送邮件并返回结果 if (mail($to, $subject, $message, $headers)) { echo "Thank you for contacting us!"; } else { echo "Sorry, we couldn't send your message right now."; } } else { echo "Invalid request method."; } ?>
对应方案B(JSON格式)
通过file_get_contents获取原始请求体,用json_decode解析后以对象属性方式访问数据:
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $post_data = file_get_contents("php://input"); $data = json_decode($post_data); // 校验数据解析是否成功 if (!$data) { echo "Invalid data received."; exit; } $subject = isset($data->subject) ? trim($data->subject) : '无主题'; $userEmail = isset($data->email) ? trim($data->email) : ''; $message = isset($data->message) ? trim($data->message) : ''; $to = "lurtze@hotmail.com"; $headers = 'From: ' . $userEmail . "\r\n" . 'Reply-To: ' . $userEmail . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if (mail($to, $subject, $message, $headers)) { echo "Thank you for contacting us!"; } else { echo "Sorry, we couldn't send your message right now."; } } else { echo "Invalid request method."; } ?>
额外提示
- 确保你的服务器已正确配置邮件服务(比如Sendmail或SMTP),很多时候
mail()函数失效是因为服务器邮件配置未完成。 - 后续记得添加前端输入验证(比如邮箱格式校验、内容非空判断),提升用户体验和数据有效性。
内容的提问来源于stack exchange,提问作者Morry
相关产品推荐
相关产品推荐

