1

I found this code online, and it works like a charm only for one thing... I was also trying to add a phone number but I keep getting an error every time... I would imagine that I would just add it to the body but every time i try to, it fails. im not really sure what im doing wrong. Im fairly new to PHP so if someone could explain to me why this is happening, that would be greatly appreciated. The content is stored into a folder, but it doesnt seem that folder is refreshed once the email is sent... :\

 <?php
$msg = "";

if (isset($_POST['submit'])) {

    require 'phpmailer/PHPMailerAutoload.php';

    function sendemail($to, $from, $fromName, $body, $attachment = "") {
        $mail = new PHPMailer();
        $mail->setFrom($from, $fromName);
        $mail->addAddress($to);
        $mail->addAttachment($attachment);
        $mail->Subject = 'Contact Form - Email';
        $mail->Body = $body;
        $mail->isHTML(false);

        return $mail->send();
    }

    $name = $_POST['username'];
    $email = $_POST['email'];
    $body = $_POST['body'];

    $file = "attachment/" . basename($_FILES['attachment']['name']);
    if (move_uploaded_file($_FILES['attachment']['tmp_name'], $file)) {
        if (sendemail('email@mail.net', $email, $name, $body, $file)) {
            $msg = 'Email sent!';

        } else
            $msg = 'Email failed!';
    } else
        $msg = "Please check your attachment!";
}
?>
<html>
<head>
    <title>Contact</title>
</head>
<style type="text/css">
    input, textarea {
        width:250px;
        height: 27px;
        margin-bottom: 10px;
    }

    textarea {
        height: 200px;
        width: 100%;
        resize: vertical;
    }

    body {
        text-align: center;
        margin-top: 250px;
    }
</style>
<body>
    <img src="images/logo.png"><br><br>
    <form method="post" action="index.php" enctype="multipart/form-data">
        <input type="text"  name="username" placeholder="Name..." required><br>
        <input type="email" name="email" placeholder="Email..." required><br>
        <textarea name="body" placeholder="Message..." required></textarea><br>
        <input type="file" name="attachment" required><br>
        <input type="submit" name="submit" value="Send Email">
    </form>
    <br><br>
    <?php echo $msg; ?>
</body>

Whatwhat
  • 11
  • 3
  • After success, you need to redirect the page. – Saad Suri Aug 12 '17 at 07:08
  • After submission the form, if you refresh the page by refresh button or 'F5' key the form will submit again because that time page has the post values provide by form which you submitted. – GYaN Aug 12 '17 at 07:09
  • 1
    Where is the connection between your questions title and what you describe in the text? – arkascha Aug 12 '17 at 07:10

2 Answers2

1

After mail sent message you have to redirect your page on current page, your problem will be solved.

use

 header('Location: '.$_SERVER['REQUEST_URI']);
Sunil Rajput
  • 960
  • 9
  • 19
  • Thanks for the timely reply. I am still having trouble however. I tried implementing this code and i am receiving the same problem. :( I tried unlink("attachment/". $_FILES["tmp_name"]["name"]); but that wont work ether. The files are still in my attachments folder :\ – Whatwhat Aug 12 '17 at 17:21
  • try to redirect on other page like header('Location: abc.php'); – Sunil Rajput Aug 14 '17 at 05:08
0
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;
jvk
  • 2,133
  • 3
  • 19
  • 28
  • I tried this as well, but the folder still has the attachments in it, so as long as there is still an attachment in the folder, when the page is refreshed, it generates an email... :( iv tried deleting it, but i cant seem to get it to work D: – Whatwhat Aug 12 '17 at 17:24
  • OK! i got this method to work! thank you so much! I did header('Location: blahblahblah.php'); THANK YOU SO MUCH! :D!!!! – Whatwhat Aug 12 '17 at 18:42