PHP Mail issues - Wont send mail

Status
Not open for further replies.

browserv

New Member
Messages
2
Reaction score
0
Points
0
Hello everybody in the x10hosting community,
I am needing some urgent support here with my site.
The php mail function is not working correctly.
I have verified that the email is set correctly, and the mail function is in the correct place.

PHP:
<?PHP
//Define some email stuff
$to = $email;
$subject = "Welcome to Browser Villagers!";
$message = "
<html>
<head>
<title>Welcome to Browser Villagers!</title>
</head>
<body>
<p>Dear $username,</p>
<p>Thank you for registering to Browser Villagers!</p>
<p>In order to access your account, please visit (Removed due to security reasons).</p>
<p>Thank you for your interest,</p>
<i><b>The Browser Villagers Team</b></i>
</body>
</html>
";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	session_start();
	
	include("database related/configuration.php");
	$username = clean($_POST["username"]);
	$password = clean($_POST["password"]);
	$email = clean($_POST["email"]);
	
	// We need to find out if the user is registering or signing in, so let's check the email post.
	
	
	if (strlen($email) < 1){
		$li = true;
	}else{
		$li = false;
	}
	
	if ($li){
		if (!userExists($username)){
			$error .= "That user doesn't exist.";
		}else{
			$info = mysql_fetch_row(mysql_query("SELECT * FROM UserInformation WHERE Username='$username'"));
			$salt = $info[4];
			Edited out due to security reasons
			if (mysql_num_rows(mysql_query("SELECT * FROM UserInformation WHERE Username='$username' AND Password='$sp'")) >= 1){
				$_SESSION['loggedin'] = true;
				$_SESSION['username'] = $username;
			}else{
				$error .= "That password is incorrect. Try again!";
			}
		}
	}
	else{
		$salt = openssl_random_pseudo_bytes(20);
		$salt = base64_encode($salt);
		$hpassword = Edited out due to security reasons // Generated a secure password to input into database
		if (userExists($username)){
			$error .= "A user already has the username $username.<br />";
		}
		
		
		
		if (!isset($error)){
			mysql_query("INSERT INTO UserInformation SET Username='$username', Email='$email', Password='$hpassword', Salt='$salt'");
			mysql_query("INSERT INTO Items SET UN='$username'");
			genNewMale($username, 21, "Athlete", "Genius", "Chef", "Bookworm");
			genNewMale($username, 21, "Athlete", "Genius", "Chef", "Bookworm");
			genNewFemale($username, 21, "Athlete", "Genius", "Chef", "Bookworm");
			genNewFemale($username, 21, "Athlete", "Genius", "Chef", "Bookworm");
			genNewFemale($username, 7, "Athlete", "Genius", "Chef", "Bookworm");
			mail($to, $subject, $message, $headers);
		}
		
	}
	
	function userExists($user){
		$query = mysql_query("SELECT * FROM UserInformation WHERE Username='$user'");
		$query = mysql_num_rows($query);
		if ($query < 1){
		return false;
		}else{
		return true;}
	}
	
	function clean($string){
		$string = mysql_real_escape_string($string);
		$string = strip_tags($string);
		return $string;
	}
	
	function genNewMale($user, $age, $trait1, $trait2, $trait3, $trait4){
		
		$query = mysql_query("SELECT Name FROM VillagerNames WHERE Gender='m'");
		$names = Array();
		while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
			$names[] =  $row['Name'];  
		}
		
		$name = array_rand($names);
		$name = $names[$name];
		$query = "INSERT INTO IslandVillagers SET UN='$user', VillagerHealth='100', VillagerAge='$age', VillagerGender='m', Trait1='$trait1', Trait2='$trait2', Trait3='$trait3', Trait4='$trait4', VillagerName='$name'";
		mysql_query($query);
	}
	
	function genNewFemale($user, $age, $trait1, $trait2, $trait3, $trait4){
		
		$query = mysql_query("SELECT Name FROM VillagerNames WHERE Gender='f'");
		$names = Array();
		while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
			$names[] =  $row['Name'];  
		}
		
		$name = array_rand($names);
		$name = $names[$name];
		$query = "INSERT INTO IslandVillagers SET UN='$user', VillagerHealth='100', VillagerAge='$age', VillagerGender='f', Trait1='$trait1', Trait2='$trait2', Trait3='$trait3', Trait4='$trait4', VillagerName='$name'";
		mysql_query($query);
	}
?>

Many thanks,
Browser Villagers Team
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
PHP manual said:
http://www.php.net/manual/en/function.mail.php

Note:When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.


mail() returns FALSE if it fails. If it returns FALSE, the problem could be in your program. If it returns TRUE, but the message doesn't send, then there could be an issue somewhere else. You should check the return value of mail() so you can handle failures.

Also, enable error logging in PHP then check the log for errors. It should say why the message didn't send.
 
Last edited:
Status
Not open for further replies.
Top