Jump to content

Mail function code not working


mdmartiny

Recommended Posts

Hello Everyone,

 

I do not know if I am not doing something right or if I am completely wrong. I am trying to get an error or success message depending on the out come .

 

This is the code that I have written so far

<?php 

if (!$_POST['submit']){
	$msg = "you must enter a name and message";
	header("Location: {$_SERVER['URL']}testmail.php?msg=$msg");
	exit;

} else {
	$name = $_POST['name'];
	$message = $_POST['message'];

if (strlen($name) <= 20 && $message <= 300){

	if (($name == "") || ($message == "")){
		$msg = " please fill in all required feilds";
		header("Location: {$_SERVER['URL']}testmail.php?msg=$msg");
		exit;

	} else {

		ini_set("SMTP", "smtp.greatlakes.net");

		$to = "[email protected]";
		$subject = "Classified Ad Submission";
		$headers = "From: [email protected]";
		$headers = "MIME-Version: 1.0rn"; 
	    $headers = "Content-type: text/html"; 
		$headers = "charset=iso-8859-1rn"; 

		$message = "<html><body>This is a email sent from $name<br /><br />$message</body></html>";

		function mail($to, $subject, $message, $headers) {
			$msg = "mail has been sent";
		} else {
			$msg = "there was a error";
		}
	}

} else {
	$msg = "You have exceded the max lentgh";
	header("Location: {$_SERVER['URL']}testmail.php?msg=$msg");
	exit;
	} 
} 
echo "$msg";
?>

Link to comment
https://forums.phpfreaks.com/topic/229636-mail-function-code-not-working/
Share on other sites

a cleaned up version of your code.

<?php
if (isset($_GET['msg'])){
echo $_GET['msg'];
        exit();
}
else{
if (!$_POST['submit']){
	$err = "you must enter a name and message";

}
$name = $_POST['name'];
$message = $_POST['message'];
if ($name == "" || $message == ""){
	$err = " please fill in all required fields";
}
if (strlen($name) >20 || strlen($message) >300){
	$err = "You have exceeded the max length";
}
if (isset($err)){
	header("location:?msg=$err");
	exit();
}
else {

	ini_set("SMTP", "smtp.greatlakes.net");

	$to = "[email protected]";
	$subject = "Classified Ad Submission";
	$headers = "From: [email protected]";
	$headers .= "MIME-Version: 1.0rn";
	$headers .= "Content-type: text/html";
	$headers .= "charset=iso-8859-1rn";

	$message = "<html><body>This is a email sent from $name<br /><br />$message</body></html>";
	$outcome = mail($to,$subject,$message,$headers);
	if ($outcome){
		$msg = "mail has been sent";
	}
	else{
		$msg = "there was an error";
	}
	header("location:?msg=$msg");
	exit();
}
}
?>

Let me know if you have further issues

a cleaned up version of your code.

<?php
if (isset($_GET['msg'])){
echo $_GET['msg'];
        exit();
}
else{
if (!$_POST['submit']){
	$err = "you must enter a name and message";

}
$name = $_POST['name'];
$message = $_POST['message'];
if ($name == "" || $message == ""){
	$err = " please fill in all required fields";
}
if (strlen($name) >20 || strlen($message) >300){
	$err = "You have exceeded the max length";
}
if (isset($err)){
	header("location:?msg=$err");
	exit();
}
else {

	ini_set("SMTP", "smtp.greatlakes.net");

	$to = "[email protected]";
	$subject = "Classified Ad Submission";
	$headers = "From: [email protected]";
	$headers .= "MIME-Version: 1.0rn";
	$headers .= "Content-type: text/html";
	$headers .= "charset=iso-8859-1rn";

	$message = "<html><body>This is a email sent from $name<br /><br />$message</body></html>";
	$outcome = mail($to,$subject,$message,$headers);
	if ($outcome){
		$msg = "mail has been sent";
	}
	else{
		$msg = "there was an error";
	}
	header("location:?msg=$msg");
	exit();
}
}
?>

Let me know if you have further issues

 

Using the code that you have given me I made some changes to the original code that I wrote. I put comments in to explain some of the things that I am trying to do

 

<?php 

if (!$_POST['submit']){
	header("Location: testmail.php"); //redirect back to form page if they came here by mistake
	exit;

} else {
	$name = $_POST['name'];
	$message = $_POST['message'];

if (strlen($name) >20 || strlen($message) >300 ){

	if ($name == "" || $message == ""){
		$msg = " please fill in all required feilds"; //redirects back to form page with error message
		header("Location: testmail.php?msg=$msg");
		exit;

	} else {

		ini_set("SMTP", "smtp.greatlakes.net");

		$to = "[email protected]";
		$subject = "Classified Ad Submission";
		$headers = "From: [email protected]";
		$headers = "MIME-Version: 1.0rn"; 
	    $headers = "Content-type: text/html"; 
		$headers = "charset=iso-8859-1rn"; 

		$message = "<html><body>This is a email sent from $name<br /><br />$message</body></html>";

		$outcome = mail($to,$subject,$message,$headers);
		if ($outcome){
    			$msg = "mail has been sent";
		}else{
    			$msg = "there was an error";
		}
	}

} else {

	$msg = "You have exceded the max length"; //redirects to form page with message
	header("Location: testmail.php?msg=$msg");
	exit;
	} 
} 

echo "$msg";

?>

 

All I am getting is q blank white page with nothing on it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.