从php文件发送并发送电子邮件到我的Gmail帐户

时间:2013-11-05 21:45:17

标签: php email wamp

我正在尝试通过此php文件发送电子邮件,我正在使用WAMP服务器。我找不到php.ini文件来编辑它们,我在locaclhost上运行这些.php文件。以下是代码,请好好看看, 我得到的错误是"Warning: mail(): SMTP server response: 550 The address is not valid. in C:\wamp\www\Test\dbregister.php on line 69"

<?php
// the following are for validating the inputs 

if (isset($_POST['name'])){
  if (ctype_alpha($_POST['name'])){
  $uname=$_POST['name'];}else{header('Location: register.php');};
}else{
  header('Location: register.php');
};

if (isset($_POST['pwd'])){
  $pwd=$_POST['pwd'];
}else{
  header('Location: register.php');
};

if (isset($_POST['email'])){
  if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  $email=$_POST['email'];}else{header('Location: register.php');};
}else{
  header('Location: register.php');
};

if (isset($_POST['sex'])){
  $sex=$_POST['sex'];
}else{
  header('Location: register.php');
};

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="user"; // Table name 
@mysql_connect("$host","$username","$password") or die("cannot connect"); 
@mysql_select_db("$db_name")or die("cannot select DB");

$name=$_POST['name'];
$pwd=$_POST['pwd'];
$email=$_POST['email'];
$gender=$_POST['sex'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(user_name, password, email)VALUES('$name', '$pwd',         '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='first.php'>Back to main page</a>";
// everything till here works fine, I am able to see the data in the data base tables.
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to=$email;  // the email address would be taken from the another webpage
// Your subject
$subject="Your confirmation link here";
// From
$header="From: sushwanth.n@gmail.com";
// Your message
$message="Your Comfirmation link \r\n";
$message.="Test email \r\n";

// send email
mail($to,$subject,$message,$header);  // I am getting the error in this line.
//mail('sushwanth.n@gmail.com','Test Email','This is a test email.',"From:     sunnibaba1@gmail.com");
// I have used the above line too see if it works, but even that was showing and error
}

// if not found 
else {
echo "This email is already in use, If you are a registered user, clik below to login";
echo "<a href='login.php'>LOGIN</a>";
//echo " <form method="post" action="login.php"><input type="submit" name="login">    </form> ";
}
?>

请告诉我如何调试此操作,以及我是否需要包含任何其他功能才能使其正常工作。

1 个答案:

答案 0 :(得分:0)

您尝试使用的SMTP服务器可能需要身份验证,而本机mail()命令不支持。在这种情况下,您需要使用外部PHP类(例如PHPMailer)并为SMTP服务器提供有效的凭据。

您需要配置的文件可能是sendmail.ini:

确保包含您的电子邮件凭据和端口