【发布时间】:2018-06-22 16:04:12
【问题描述】:
我承认我对 PHP 了解不多。我发现这个脚本可以发送电子邮件,它适用于我的其他页面之一,但现在它不起作用,我不知道为什么。我唯一改变的是我在脚本中间添加了“if”语句来处理电子邮件,因此电子邮件是保密的。有人有什么想法吗?
<?php
/*if(!isset($_GET['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}*/ //This error seems to just get in the way. I am cutting it out.
$mailTo = $_GET['to'];
$subject = $_GET['subject'];
$name = $_GET['name'];
$visitor_email = $_GET['email'];
$message = $_GET['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
//If statements to change value from html to actual email addresses
switch($mailTo) {
case "ED": $mailTo = "example@example.com";
break;
case "AS": $mailTo = "example@example.org";
break;
case "OM": $mailTo = "example@example.org";
break;
case "VC": $mailTo = "example@example.org";
break;
case "Pres": $mailTo = "example@example.com";
break;
case "VP": $mailTo = "example@example.net";
break;
case "Sec": $mailTo = "example@example.com";
break;
case "Treas": $mailTo = "example@example.com";
break;
case "Dir1": $mailTo = "example@example.com";
break;
case "Dir2": $mailTo = "example@example.com";
break;
case "Dir3": $mailTo = "example@example.net";
break;
case "Dir4": $mailTo = "example@example.com";
break;
case "Dir5": $mailTo = "example@example.com";
break;
case "Dir6": $mailTo = "example@example.gov";
break;
case "Dir7": $mailTo = "example@example.com";
break;
case "Dir8": $mailTo = "example@example.com";
break;
case "Dir9": $mailTo = "example@example.com";
break;
}
$email_from = $visitor_email;//<== update the email address
$email_subject = $subject;
if($subject==="Please sign me up for the example Newsletter")
{
$email_body = "$name has requested to be subscribed to the example
Newsletter\n";
$email_body .= "$name's email address is $visitor_email\n";
}
else
{
$email_body = "*** The following message is from the user ***\n";
$email_body = $message;
}
if($message!=="")
$email_body .= "They have also added a custom message:\n $message";
$to = $mailTo;//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
if(mail($to,$email_subject,$email_body,$headers))
echo "Mail submitted successfully";
else
echo "Mail not sent";
//done. redirect to thank-you page.
//header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
在我的控制台中,我没有收到任何错误,我正在使用 AJAX 并且我没有收到任何错误,返回值始终是提交成功,并且正在提交的查询字符串是有效的.我已将其范围缩小到它必须是 PHP 中的某些东西,它搞砸了并使电子邮件无效。
这里是 AJAX 功能的 JS。我的验证函数调用 AJAX,然后将其发送到 PHP。所有的 JS 都已经调试好,可以正常工作了。
function ajaxFunction(caller)
{
'use strict';
console.log("1. *** BEGINNING AJAX FUNCTION ***");
var ajaxRequest;
try{
// Real browsers
ajaxRequest = new XMLHttpRequest();
} catch(e) {
// IE browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
// Something went wrong
alert("Your browser broke");
return false;
}
}
}
// Function that will recieve data sent from the server
ajaxRequest.onreadystatechange = function() {
console.log("2. The current ready state is: " + ajaxRequest.readyState);
if(ajaxRequest.readyState === 4){
var errorP = document.getElementById("errorP");
console.log("Response Text is " + ajaxRequest.responseText);
errorP.innerHTML = ajaxRequest.responseText;
}
}
// Create the date object and send it to the server
var to;
var subject;
var select = document.getElementById("select");
var subj = document.getElementById("subject");
var name = document.getElementById("name");
var nameVal = name.value;
var email = document.getElementById("email");
var emailVal = email.value;
var message = document.getElementById("message");
var messageVal = message.value;
if(caller==="newsletter")
{
to = "newsletter@al-van.org";
subject = "Please sign me up for the Al-Van Newsletter";
}
else if(caller==="contact")
{
to = select.value;
subject = subj.value;
}
var queryString = "?name=" + nameVal + "&email=" + emailVal + "&message=" + messageVal + "&to=" + to + "&subject=" + subject;
console.log("6. The query string is: " + queryString);
ajaxRequest.open("GET", "form-to-email.php" + queryString, true);
ajaxRequest.send(null);
}
【问题讨论】:
-
check tail /var/log/maillog 你会看到你发送的邮件发生了什么
-
这是服务器上某处的文件吗?
-
是的,你可以使用tail来检查他们的状态
-
考虑换个地址而不是大量的如果更改地址
-
我正在为客户建立一个站点,他们让我通过 ftp 访问他们的服务器。我不知道在他们的服务器上哪里可以找到尾巴。他们正在使用 bluehost。
标签: php