PHP email form sends email everytime page is refreshed

后端 未结 3 988
半阙折子戏
半阙折子戏 2021-01-17 06:11

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 g

3条回答
  •  Happy的楠姐
    2021-01-17 06:51

    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.

提交回复
热议问题