mdmartiny Posted March 5, 2011 Share Posted March 5, 2011 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 More sharing options...
jonsjava Posted March 5, 2011 Share Posted March 5, 2011 you are telling the code to create a mail function, instead of just using mail(). try this: $outcome = mail($to,$subject,$message,$headers); if ($outcome){ $msg = "mail has been sent"; } else{ $msg = "there was an error"; } Link to comment https://forums.phpfreaks.com/topic/229636-mail-function-code-not-working/#findComment-1183139 Share on other sites More sharing options...
mdmartiny Posted March 5, 2011 Author Share Posted March 5, 2011 I added the code and all I still get is the blank page Link to comment https://forums.phpfreaks.com/topic/229636-mail-function-code-not-working/#findComment-1183143 Share on other sites More sharing options...
jonsjava Posted March 5, 2011 Share Posted March 5, 2011 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 Link to comment https://forums.phpfreaks.com/topic/229636-mail-function-code-not-working/#findComment-1183149 Share on other sites More sharing options...
mdmartiny Posted March 5, 2011 Author Share Posted March 5, 2011 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 Link to comment https://forums.phpfreaks.com/topic/229636-mail-function-code-not-working/#findComment-1183164 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.