I have a problem with my PHP Mail. It stops working from time to time without me even touching the code. I have a script wich checks if required forms are empty. This script works. But if I fill in all the required fields I should get a text wich sais "Your mail was successfully sent" but I dont. But if I edit my code by just moving a bit of text some lines down and back to the original position again it works, for a while. It's often under the night it stops working. Can it be the server thats causing trouble or is it my code that I posted below?
<?php
if(isset($_POST['submit'])){
$namn = strip_tags($_POST['namn']);
$foretag = strip_tags($_POST['foretag']);
$adress = strip_tags($_POST['adress']);
$postnr = strip_tags($_POST['postnr']);
$ort = strip_tags($_POST['ort']);
$telefon = strip_tags($_POST['telefon']);
$epost = strip_tags($_POST['epost']);
$meddelande = strip_tags($_POST['meddelande']);
function check_required_fields($required_array){
$field_errors = array();
foreach($required_array as $fieldname){
if ((!isset($_POST[$fieldname])) || (empty($_POST[$fieldname]))){
if($_POST[$fieldname] != '0'){
$field_errors[] = $fieldname;
}
}
}
return $field_errors;
}
$errors = array();
$required_fields = array('namn', 'telefon', 'meddelande');
$errors = array_merge($errors, check_required_fields($required_fields));
if(empty($errors)){
$meddelande=nl2br($meddelande);
if(empty($foretag)){ $foretag='-'; }
if(empty($adress)){ $adress='-'; }
if(empty($postnr)){ $postnr='-'; }
if(empty($ort)){ $ort='-'; }
if(empty($epost)){ $epost='-'; }
$body =
"
Namn: <b>". $namn ."</b><br />
Företag: <b>". $foretag ."</b><br />
Adress: <b>". $adress ."</b><br />
Postnr: <b>". $postnr ."</b><br />
Ort: <b>". $ort ."</b><br />
Telefon: <b>". $telefon ."</b><br />
E-post: <b>". $epost ."</b><br /><br />
Meddelande: <b><br />". $meddelande
;
$headers = "From: $namn <webmaster@allflytt.com>\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Mailer: PHP v".phpversion();
$success = mail('info@allflytt.com', 'Meddelande', $body, $headers);
}
}
?>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<?php
if(!empty($errors)){
echo "<p class=\"field_error\">De rödmarkerade fälten måste fyllas i.</p>";
}
?>
<?php
if(empty($errors) && $success){
echo "<p class=\"p_success\">Tack för ditt meddelande! Vi kommer att besvara det inom kort.</p>";
}
?>
<p>
<label for="namn">
<?php if(!empty($errors)){if(in_array("namn", $errors)){echo "<span class=\"field_error\">";}}?>Namn: *<?php if(!empty($errors)){if(in_array("namn", $errors)){echo "</span>";}} ?>
</label><br />
<input type="text" name="namn" id="namn" class="text" tabindex="15" value="<?php if(!empty($errors)){ echo $namn; } ?>" />
<br />
<label for="foretag">Företag:</label><br />
<input type="text" name="foretag" id="foretag" class="text" tabindex="20" value="<?php if(!empty($errors)){ echo $foretag; }?>" />
<br />
<label for="adress">Adress:</label><br />
<input type="text" name="adress" id="adress" class="text" tabindex="30" value="<?php if(!empty($errors)){ echo $adress; } ?>" />
<br />
<label for="postnr">Postnummer:</label><br />
<input type="text" name="postnr" id="postnr" class="text_medium" tabindex="40" value="<?php if(!empty($errors)){ echo $postnr; } ?>" />
<br />
<label for="ort">Ort:</label><br />
<input type="text" name="ort" id="ort" class="text" tabindex="50" value="<?php if(!empty($errors)){ echo $ort; } ?>" />
<br />
<label for="telefon">
<?php if(!empty($errors)){if(in_array("telefon", $errors)){echo "<span class=\"field_error\">";}}?>Telefon: *<?php if(!empty($errors)){if(in_array("telefon", $errors)){echo "</span>";}} ?>
</label><br />
<input type="text" name="telefon" id="telefon" class="text" tabindex="60" value="<?php if(!empty($errors)){ echo $telefon; } ?>" />
<br />
<label for="epost">E-post:</label><br />
<input type="text" name="epost" id="epost" class="text" tabindex="70" value="<?php if(!empty($errors)){ echo $epost; } ?>" />
<br />
<label for="meddelande">
<?php if(!empty($errors)){if(in_array("meddelande", $errors)){echo "<span class=\"field_error\">";}}?>Meddelande: *<?php if(!empty($errors)){if(in_array("meddelande", $errors)){echo "</span>";}} ?>
</label><br />
<textarea name="meddelande" id="meddelande" class="textarea" tabindex="80"><?php if(!empty($errors)){ echo $meddelande; } ?></textarea>
<br />
<input type="submit" name="submit" value="Skicka" class="submit" />
</p>
</form>
could not find if anything wrong in your code. check if APC cache is enabled in your server. sometimes it creates problem in taking updated code.
if you are using SMTP server then mail() function may not work. you can use PEARS for this.
Problem solved. Changed to a new server host. No issues any more!
I have experienced a similar problem. Turned out the browser, by error, sometimes double-posts your form; One time with content and another time without content.
If no content is submitted to your script, obviously there's nothing to act upon. There are a number of ways to check on this, but they way I discovered it was a desperate last resort measure where I sent emails to myself with each run of the script. And I showed that I often got two mails where the script had run only once.
It's worht trying :)