php mail()两个副本发送

时间:2015-10-10 14:14:49

标签: php email

我有一条简单的消息,我使用php mail()发送。 使用的代码:

//recipient info
$to = "$bookernavn <$mail>";
$from = "Visens Venner Hillerød <booking@eksample.dk>";
$subject = "Kvittering - $a_titel - ". date("j/n - Y",$a_dato);

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";
$headers .= "Return-Path: $from" . "\r\n";
$headers .= "Bcc: $from" . "\r\n";

// now lets send the email. 
mail($to, $subject, $mailmsg, $headers); }

由于某些奇怪的原因,每次都会发送两封邮件...
有时间隔几分钟......
有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您不会检查表单是否已提交,因此浏览器刷新将再次发送表单数据并导致再次发送邮件。当用户按下后退按钮时也会发生这种情况。

发送电子邮件后,您需要执行303重定向以防止重新提交。如果您愿意,可以重定向到同一页面。

这称为Post/Redirect/Get pattern

mail(...);
header('Location: /some-page-php', true, 303);
exit;

答案 1 :(得分:0)

防止这种情况发生的一种简单方法是使用POST方法代替GET表单。

<form method="post">



if (isset($_POST['submitted']))

并在邮件代码的末尾使用重定向,该重定向将使用GET方法将浏览器发送到加载。

您不仅可以将用户重定向到正常页面,而且还会发送邮件&#34;或错误页面&#34;抱歉有错误,请再试一次#34;,刷新浏览器打开的页面只会发送GET,而不会触发发送邮件功能

if (empty($errors)) {
  header('Location: http://www.example.com/mail_OK.html');
  exit;
} else {
  // passing data to the "error/retry" page
  $info = array(
    'msg' => $msg,
    'email' => $_POST['email'],
    'name' => $_POST['name']
    // etc...
  )
  header('Location: http://www.example.com/mailform.php?'.http_build_query($info));
  exit;
}

在表单中,您可以检索这些信息

<input name="name" type="text" placeholder="Naam" class="form-control" value="<?php echo htmlspecialchars($_GET['name']); ?>">
相关问题