my php email for is sending emails every time the page is refreshed. For example the user is filling out the form and sending it with the send button. That's all fine and good but if they refresh the page it sends the email again with all the same form info.
I believe this is the problem code but don't know what it is.
require_once('class.phpmailer.php');
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = 'WebForm';
$email = $_POST['email'];
$body = $_POST['message'];
$mail = new PHPMailer;
// $mail->SMTPDebug = 2;
// print_r($_POST);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->Username = "person@emailaddy.com";
$mail->Password = "password";
$mailto = "person@emailaddy.com";
$mailfrom = "person@emailaddy.com";
$mail->SetFrom($mailto, '');
// $mail->AddReplyTo($mailfrom, 'email');
$address = 'person@emailaddy.com';
$mail->AddAddress($address, "My Addy");
$mail->Subject = $subject;
$mail->AltBody = $body;
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo 'Message has been sent';
}
}
Use a header instead and make sure you have no output before header.
if(!$mail->Send()) {
header("Location: http://www.example.com");
exit;
}
If that does not work for you, use a meta refresh method:
if(!$mail->Send()) {
$to = "http://www.example.com";
$url = $to;
print "<meta HTTP-EQUIV=Refresh CONTENT=\"0; URL=$url\">";
exit;
}
or display a message and redirect after 5 seconds:
if(!$mail->Send()) {
$to = "http://www.example.com";
$url = $to;
print "<meta HTTP-EQUIV=Refresh CONTENT=\"5; URL=$url\">";
print "Thank you for your message.";
exit;
}
Edit: (cookie/token method)
You can use a cookie and this is just an example.
<?php
$token = time();
setcookie('formToken', $token, time() + 3600);
if(isset($_POST['submit'])){
if($_POST['token'] != $_COOKIE['formToken']){
// die("Sorry");
$error_list .= '<li>You can not submit this form twice.</li>';
echo $error_list;
echo '
Thank you, your message has been sent. You do not need resubmit it again.
';
exit;
}
foreach( $_POST as $values ) { $data .= $values . "<br>"; echo $data; }
}
?>
<form action="" method="POST">
Name: <input type="text" name="name">
<br>
Email: <input type="text" name="email">
<input type="hidden" name="token" value="<?php echo $token; ?>" />
<input type="submit" value="Submit" name="submit" />
</form>
try this-
require_once('class.phpmailer.php');
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = 'WebForm';
$email = $_POST['email'];
$body = $_POST['message'];
$mail = new PHPMailer;
// $mail->SMTPDebug = 2;
// print_r($_POST);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->Username = "person@emailaddy.com";
$mail->Password = "password";
$mailto = "person@emailaddy.com";
$mailfrom = "person@emailaddy.com";
$mail->SetFrom($mailto, '');
// $mail->AddReplyTo($mailfrom, 'email');
$address = 'person@emailaddy.com';
$mail->AddAddress($address, "My Addy");
$mail->Subject = $subject;
$mail->AltBody = $body;
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo 'Message has been sent';
}
}
the mail
sending function was outside of the if condition with $_POST
check. So it was sending mail everytime it is refreshed.
You should use different files, at different URLs to handle the actions:
- send email
- display confirmation page
For example:
In your form
<form ... method="post" action="/sendmail.php">
In sendmail.php
// same code as above except:
if(!$mail->Send()) {
header("Location: success.php");
} else {
header("Location: error.php");
}
See the doc for details.
In success.php
<p>Oh yeah ;)</p>
In error.php
<p>Ooops :(</p>