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 = "mikedmartiny@gmail.com";
		$subject = "Classified Ad Submission";
		$headers = "From: autostylersrv@greatlakes.net";
		$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
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 = "mikedmartiny@gmail.com";
	$subject = "Classified Ad Submission";
	$headers = "From: autostylersrv@greatlakes.net";
	$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

Link to comment
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 = "mikedmartiny@gmail.com";
	$subject = "Classified Ad Submission";
	$headers = "From: autostylersrv@greatlakes.net";
	$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 = "mikedmartiny@gmail.com";
		$subject = "Classified Ad Submission";
		$headers = "From: autostylersrv@greatlakes.net";
		$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.