Crankeedoodledoo Crankeedoodledoo - 4 years ago 56
HTML Question

The same php form works on one site but not the other

I have a simple query form that contains only three fields: name, email and message. It worked on one site. I've used the same form with the exact same code on another site and it just won't work. The message can't be sent and I keep getting the internal server error (500). I've used two different domains, on different hosts, but the code is the same. The only difference that I could see is the php version. The first website uses version 5.5 and the other one 5.6 (native <<< I'd love to know what that means). I tried switching the latter to 5.5 and it gave me an error: "undefined variable $email_content on line...".
My question is, why is it working on one site and not the other even with the same php versions and the same code? What am I missing?

PHP code:



$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {

echo "Došlo je do greške. Molimo vas da pokušate ponovo.";

$recipient = "";

$subject = "Nova poruka od $name";

$email_content .= "Ime: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Poruka:\n$message\n";

$email_headers = "From: $name <$email>";

if (mail($recipient, $subject, $email_content, $email_headers)) {

echo "Hvala! Vaša poruka je poslata.";
} else {

echo "Poruka nije poslata. Pokušajte ponovo.";

} else {

echo "Došlo je do problema prilikom slanja vaše poruke. Pokušajte ponovo.";

JS code:

$(function() {

var form = $('#contactForm');
var formMessages = $('#form-messages');

$(form).submit(function(event) {


var formData = $(form).serialize();

type: 'POST',
url: $(form).attr('action'),
data: formData

.done(function(response) {



.fail(function(data) {


if (data.responseText !== '') {

} else {

$(formMessages).text('Vaša poruka nije poslata. Pokušajte ponovo.');


<form method="post" id="contactForm" action="mailer.php">
<div class="form-group">
<label for="name">Ime i prezime</label>
<input type="text" class="form-control" name="name" id="name">
<div class="form-group">
<label for="email">Email adresa</label>
<input type="email" class="form-control" id="email" name="email" required>
<div class="form-group">
<label for="message">Unesite vašu poruku</label>
<textarea class="form-control" id="message" rows="4" name="message"></textarea>
<button type="submit" class="btn btn-primary">Pošalji</button>

UPDATE as per SSingh's request:

<br /> <b>Notice</b>: Undefined variable: email_content in <b>/home/elektro4/public_html/mailer.php</b> on line <b>27</b><br /> Poruka nije poslata. Pokušajte ponovo.

Answer Source

As it turns out, the hosting company has a security measure in place that prevents using email addresses not present/created on the server.
I just had to change the $email_headers = "From: $name <$email>"; to an email address I created in cPanel.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download