我是PHP的新手,在网上以一个脚本为例。我已经安装了Xampp,php已经安装并运行。我曾尝试使用hMailServer设置本地邮件服务器,但我不完全确定设置是否正确。无论如何,这是PHP代码:
<?php
$subject="";
$detail="";
$customer_mail="";
$name="";
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='sean.myers92@gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>
以及相应的HTML代码:
<table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td><strong>Contact Form </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="test.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%">Subject</td>
<td width="2%">:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
当我提交表单时,我收到错误警告:mail()[function.mail]:SMTP服务器响应:需要530 SMTP身份验证。有什么想法吗?感谢
来自hMailServer的论坛:http://www.hmailserver.com/forum/viewtopic.php?f=6&t=22039
在IP范围内。转到我的计算机ip范围,取消本地到外部和/或外部到外部的身份验证。
Xampp没有内置邮件服务器。您需要设置一个。
PHP邮件问题,无法发送
使用XAMPP 发送邮件时出错
或者使用另一个stmp服务器(即谷歌邮件)和邮件库(如PEAR:mail或swiftmailer)来建立具有登录凭据的imap/smtp连接
它的php没有配置,您可以更新它以指向有效的smtp服务器在php.ini中查找[mail function]
,这仅适用于windows您可能希望在本地设置邮件服务器,因为它不支持密码或使用phpmailerhttp://sourceforge.net/projects/phpmailer它确实支持登录
从该链接下载最新的phpmailerhttp://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list并阅读它的gmail教程。对我来说,它也可以在本地系统中工作:)+这是一个非常简单的教程。
/***如果成功,则返回错误字符串或null*/
函数authSendEmail($sendto,$replyto,$subject,$message,$namefrom="){
//SMTP+服务器详细信息
/****配置启动***/
$smtpServer="mail.server.com";
$port="25";
$timeout="5";
$username="test@server.com";
$password="测试";
$newLine="\r\n";
/***配置结束***/* 2ХХ — команда успешно выполнена 3XX — ожидаются дополнительные данные от клиента 4ХХ — временная ошибка, клиент должен произвести следующую попытку через некоторое время 5ХХ — неустранимая ошибка */ //0 //Connect to the host on the specified port $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout); $smtpResponse = fgets($smtpConnect, 1024); if(empty($smtpConnect) || $smtpResponse{0} !='2' ) { return "Failed to connect: $smtpResponse"; } //1 //hello server fputs($smtpConnect,"EHLO servername" . $newLine); $smtpResponse = fgets ($smtpConnect); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; $smtpResponse = fgets ($smtpConnect); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; $smtpResponse = fgets ($smtpConnect); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; $smtpResponse = fgets ($smtpConnect); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; $smtpResponse = fgets ($smtpConnect); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; $smtpResponse = fgets ($smtpConnect); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; //2 //Request Auth Login fputs($smtpConnect,"AUTH LOGIN" . $newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='3' ) return "Failed: $smtpResponse"; //3 //Send username fputs($smtpConnect, base64_encode($username).$newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='3' ) return "Authentification failed: $smtpResponse"; //4 //Send password fputs($smtpConnect, base64_encode($password).$newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; //6 //Email From fputs($smtpConnect, "MAIL FROM: ".$username . $newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; //7 //Email To fputs($smtpConnect, "RCPT TO: ".$sendto . $newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; //8 //The Email fputs($smtpConnect, "DATA" . $newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='3' ) return "Failed: $smtpResponse"; //9 //Construct Headers $headers = "MIME-Version: 1.0" . $newLine; $headers .= "Content-type: text/html; charset=windows-1251" . $newLine; $headers .= "To: Администратор <$sendto>" . $newLine; $headers .= "From: $namefrom <$username>" . $newLine; $headers .= "Reply-To: $namefrom <$replyto>" . $newLine; $headers .= "X-Mailer: ".phpversion(). $newLine; fputs($smtpConnect, "Subject: $subjectn$headersnn$messagen.n" ); $smtpResponse = fgets($smtpConnect, 515); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; //10 // Say Bye to SMTP fputs($smtpConnect,"QUIT" . $newLine); $smtpResponse = fgets($smtpConnect, 1024); if($smtpResponse{0} !='2' ) return "Failed: $smtpResponse"; return false; } ?>