Jump to content

PHP mail stops working


tovesson

Recommended Posts

Hi!

I have quite a odd issue with my PHP mail code. The thing is that it works from time to time. As for now I have three required fields (Namn, Telefon, Meddelande). The PHP function wich checks if they are filled in or not works without problem every time. It's when I fill in all the required fields the problem starts. When I fill in all the required fields I am supposed to get the following success message "Tack för ditt meddelande! Vi kommer att besvara det inom kort.". But I don't, and the mail is not even sent. To fix this I have to upload a identical file that is already on the server and overwrite the old one. Then it often starts working directly but sometimes after a while as well. I've also noticed that the PHP mail stops working under the night. So what I do in the morning is upload a identical file that is already on the server and overwrite the old one to make it work again. Then it works all day but under the night, as mentioned, it stops working again. I have gone trough my code several of times but can't find any errors, hopefully with some fresh eyes we might find something. I've got some suggestions before. One was to check if APC cache is enabled on the server, I've checked and it is. Also one was if I'm using a SMTP server. The answer there is, yes I am. But I asked my web host and he said that I did not need to use Pear to send mail from the server. I post my code below so you can get a look at it. Thank in advance.

 

<?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@example.com>\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Mailer: PHP v".phpversion();

$success = mail('mail@example.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>

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.