我的验证联系表单无效。当我尝试提交表单时,会发生以下错误:
警告:mail()[function.mail]:无法连接到mailserver "本地主机"端口25,验证您的" SMTP"和" smtp_port"设置 php.ini或使用ini_set() 第53行的E:\ CMS_Site \ wamp \ www \ sitename \ contents \ send_mail.php
<?php
require_once 'mandrill-api-php/src/Mandrill.php'; //Not required with Composer
$mandrill = new Mandrill('eWTy3pUA1Okb-4lwUtk4dg');
if(isset($_POST['name']) != NULL && isset($_POST['email']) != NULL && isset($_POST['message']) != NULL )
{ // if(isset($_POST['submit_form']) != NULL) IF START
$name = strtoupper (trim($_POST['name']));
$address = trim($_POST['address']);
$email = strtolower(trim($_POST['email']));
$contact = trim($_POST['contact']);
$country = trim($_POST['country']);
$website = trim($_POST['website']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
//---------------------------------------------
error_reporting(E_ALL ^ E_NOTICE);
$my_email = "kiranpahadi@gmail.com";
$errors = array();
// Remove $_COOKIE elements from $_REQUEST.
if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
// Validate email field.
if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{
$_REQUEST['email'] = trim($_REQUEST['email']);
if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}
}
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}
// Check for a blank form.
function recursive_array_check_blank($element_value)
{
global $set;
if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{
foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}
}
}
recursive_array_check_blank($_REQUEST);
if(!$set){$errors[] = "You cannot send a blank form";}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}
if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}
// Build message.
function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."The Message has been submitted successfully ";
$message = stripslashes($message);
$subject = stripslashes($subject);
if($email)
{
$headers = "From: {$name} <{$_REQUEST['email']}>";
$headers .= PHP_EOL;
$headers .= "Reply-To: " . $_REQUEST['email'];
}
else
{
if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);}
$headers = "From: {$name} <{$_REQUEST['email']}>";
}
mail($my_email,$subject,$message,$headers);
?>
<b>Thank you <?php if(isset($_REQUEST['name'])){print stripslashes($_REQUEST['name']);} ?></b>
<?php
//---------------------------------------------
}
else
{
?>
<script type="text/javascript">
function validate_mail()
{
var mail_name=document.mail_form.name.value;
var mail_email=document.mail_form.email.value;
var mail_contact=document.mail_form.contact.value;
var mail_message=document.mail_form.message.value;
var mail_subject=document.mail_form.subject.value;
var spaceRegxp = /\w+/;
var emailRegxp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
var contactRegxp = /^\d+$/;
if (spaceRegxp.test(mail_name) != true){
alert("PLEASE CHECK NAME");
document.mail_form.name.focus();
return false;}
if (emailRegxp.test(mail_email) != true){
alert("PLEASE CHECK EMAIL");
document.mail_form.email.focus();
return false;}
if (contactRegxp.test(mail_contact) != true){
alert("PLEASE CHECK CONTACT NUMBER");
document.mail_form.contact.focus();
return false;}
if (spaceRegxp.test(mail_subject) != true){
alert("PLEASE CHECK Subject");
document.mail_form.subject.focus();
return false;}
if (spaceRegxp.test(mail_message) != true){
alert("PLEASE CHECK MESSAGE");
document.mail_form.message.focus();
return false;}
else {
document.mail_form.action = 'index.php?t=contact&i=25';
document.mail_form.btn_submit.disabled=1;
document.mail_form.btn_submit.value = ' PLEASE WAIT ... ';
document.mail_form.submit();
}
}
</script>
<form name = "mail_form" method="post" enctype="multipart/form-data">
<div><label for="name"> Full Name: </label> <input name="name" type="text" size="50" /> </div>
<div><label for="address"> Address: </label> <input name="address" type="text" size="50" /></div>
<div><label for="email"> Email: </label><input name="email" type="text" size="50" /></div>
<div><label for="contact">Contact:</label> <input name="contact" type="text" size="50" /></div>
<div><label for="country">Country:</label> <input name="country" type="text" size="50" /> </div>
<div><label for="website">Website:</label> <input name="website" type="text" size="50" /> </div>
<div><label for="subject">Subject:</label> <input name="subject" type="text" size="50" /> </div>
<div><label for="message">Your Message:</label>
<textarea name="message" cols="40" rows="8"></textarea>
</div>
<div> <p> <input type="Button" name="btn_submit" id="submit-go" value=" Send Mail " onClick="validate_mail()"/> </p>
</div>
</form>
<?php
}
?>
答案 0 :(得分:2)
您通过php发送邮件的代码完全没有问题。 如果您在Windows中使用WAMP,它们将不会设置任何SMTP服务器。因此,如果您需要从WAMP发送邮件,则需要使用任何SMTP服务器支持。您可以使用Mandrill Mailchimb从localhost发送电子邮件,只需在其中创建一个a / c,下载swift文件并将其添加到您的工作目录中即可设置邮件服务器。有关如何通过mandrill发送邮件的更多信息,请参见下面的代码..
或
您可以在支持SMTP设置的实时服务器中使用此代码 通过代码中的Mandrill Swift文件发送邮件。您可以将邮件主题等更改为您自己的邮件。
send_mail.php
<?php
if(isset($_POST['name']) != NULL && isset($_POST['email']) != NULL && isset($_POST['message']) != NULL ){
$name = strtoupper (trim($_POST['name']));
$address = trim($_POST['address']);
$email = strtolower(trim($_POST['email']));
$contact = trim($_POST['contact']);
$country = trim($_POST['country']);
$website = trim($_POST['website']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
//---------------------------------------------
error_reporting(E_ALL ^ E_NOTICE);
$my_email = "kiranpahadi@gmail.com";
$errors = array();
// Remove $_COOKIE elements from $_REQUEST.
if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
// Validate email field.
if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])){
$_REQUEST['email'] = trim($_REQUEST['email']);
if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){
$errors[] = "Email address is invalid";
}
else{
$exploded_email = explode("@",$_REQUEST['email']);
if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){
$errors[] = "Email address is invalid";
}
else{
if(substr_count($exploded_email[1],".") == 0){
$errors[] = "Email address is invalid";
}
else{
$exploded_domain = explode(".",$exploded_email[1]);
if(in_array("",$exploded_domain)){
$errors[] = "Email address is invalid";
}
else{
foreach($exploded_domain as $value){
if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){
$errors[] = "Email address is invalid";
break;
}
}
}
}
}
}
}
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){
$errors[] = "You must enable referrer logging to use the form";
}
// Check for a blank form.
function recursive_array_check_blank($element_value){
global $set;
if(!is_array($element_value)){
if(!empty($element_value)){
$set = 1;
}
}
else{
foreach($element_value as $value){
if($set){
break;
}
recursive_array_check_blank($value);
}
}
}
recursive_array_check_blank($_REQUEST);
if(!$set){
$errors[] = "You cannot send a blank form";
}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){
foreach($errors as $value){
print "$value<br>";
}
exit;
}
if(!defined("PHP_EOL")){
define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");
}
// Build message.
function build_message($request_input){
if(!isset($message_output)){
$message_output ="";
}
if(!is_array($request_input)){
$message_output = $request_input;
}
else{
foreach($request_input as $key => $value){
if(!empty($value)){
if(!is_numeric($key)){
$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
}
else{
$message_output .= build_message($value).", ";
}
}
}
}
return rtrim($message_output,", ");
}
$message = build_message($_REQUEST);
$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."The Message has been submitted successfully ";
$message = stripslashes($message);
$subject = stripslashes($subject);
if($email){
$headers = "From: {$name} <{$_REQUEST['email']}>";
$headers .= PHP_EOL;
$headers .= "Reply-To: " . $_REQUEST['email'];
}
else{
if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){
$from_name = stripslashes($_REQUEST['name']);
}
$headers = "From: {$name} <{$_REQUEST['email']}>";
}
include_once "swift/lib/swift_required.php";
$from = array("frommail@mail.com" => "Your Name");
$to="kiranpahadi@gmail.com";
$message = "Hello -This is a test mail";
$subject = "Subject – Test";
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
$transport->setUsername('kiranpahadi@gmail.com');
$transport->setPassword('eWTy3pUA1Okb-4lwUtk4dg');
$swift = Swift_Mailer::newInstance($transport);
sleep(2);
$html = "" . $message . "";
$maildetails = new Swift_Message($subject);
$maildetails->setFrom($from);
$maildetails->setBody($html, 'text/html');
$maildetails->setTo($to);
$maildetails->addPart($message, 'text/plain');
if ($recipients = $swift->send($maildetails, $failures)) {
echo 'Message successfully sent!';
} else {
echo "There was an error:n";
//print_r($failures);
}
//mail($my_email,$subject,$message,$headers);
?>
<b>Thank you <?php if(isset($_REQUEST['name'])){print stripslashes($_REQUEST['name']);} ?></b>
<?php
}
else{
?>
<form name = "mail_form" method="post" action ="send_mail.php"enctype="multipart/form-data">
<div><label for="name"> Full Name: </label> <input name="name" type="text" size="50" /> </div>
<div><label for="address"> Address: </label> <input name="address" type="text" size="50" /></div>
<div><label for="email"> Email: </label><input name="email" type="email" size="50" /></div>
<div><label for="contact">Contact:</label> <input name="contact" type="text" size="50" /></div>
<div><label for="country">Country:</label> <input name="country" type="text" size="50" /> </div>
<div><label for="website">Website:</label> <input name="website" type="text" size="50" /> </div>
<div><label for="subject">Subject:</label> <input name="subject" type="text" size="50" /> </div>
<div><label for="message">Your Message:</label>
<textarea name="message" cols="40" rows="8"></textarea>
</div>
<div><p><input type="submit" name="btn_submit" id="submit-go" value="Send Mail"/></p></div>
</form>
<?php
}
?>
#我刚从代码中删除了一些不需要的行。
您可以从http://ajesh.co.in/downloads/swift.zip
答案 1 :(得分:0)
WAMP没有SMTP服务器。
解释
为了能够发送电子邮件,您需要一个外发电子邮件服务器。在大多数Linux系统中,默认存在一个,PHP将使用sendmail,一个Linux应用程序,用于向您安装的任何邮件传输代理提交邮件。
在Windows中,为了能够从PHP发送邮件,您需要在某处使用外发邮件服务器,并且需要告诉PHP它的地址和端口。这是在php.ini中使用SMTP和smtp_port设置完成的。它将在端口25上默认为“localhost”。除非您在该计算机上设置了邮件服务器,否则将失败。
例如,如果您的ISP为您提供外发邮件服务器,您可以使用其地址和端口号。或者,如果您认真发送邮件,则可以在本地计算机或本地网络中的某个人上设置自己的邮件服务器。
消息来源Why mail() PHP function does not work with WAMP default installation?