如果我不编辑代码,PHP Mail 将停止工作
我的 PHP 邮件有问题。它有时会停止工作,我什至没有接触代码。我有一个脚本可以检查所需的表单是否为空。这个脚本有效。但是,如果我填写所有必填字段,我应该会收到一条文本,上面写着“您的邮件已成功发送”,但我没有。但是,如果我通过仅将一些文本向下移动一些行并再次返回到原始位置来编辑代码,那么它会工作一段时间。经常到了晚上就停止工作了。可能是服务器造成了问题,还是我在下面发布的代码?
<?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 <[email protected]>\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Mailer: PHP v".phpversion();
$success = mail('[email protected]', '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>
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 which checks if required forms are empty. This script works. But if I fill in all the required fields I should get a text which says "Your mail was successfully sent" but I don't. 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 that is 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 <[email protected]>\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Mailer: PHP v".phpversion();
$success = mail('[email protected]', '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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找不到您的代码是否有问题。
检查您的服务器是否启用了 APC 缓存。
有时它会在更新代码时产生问题。
如果您使用 SMTP 服务器,则 mail() 函数可能无法工作。
您可以使用 PEARS 来实现此目的。
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.
我也遇到过类似的问题。结果浏览器有时会错误地重复发布您的表单;一次有内容,另一次没有内容。
如果没有内容提交到您的脚本,显然没有什么可采取的行动。有很多方法可以检查这一点,但我发现这是一个绝望的最后手段,我在每次运行脚本时都会向自己发送电子邮件。我表明我经常收到两封脚本只运行一次的邮件。
值得一试:)
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 :)
问题解决了。更换了新的服务器主机。没有任何问题了!
Problem solved. Changed to a new server host. No issues any more!