douying6206 2017-07-09 07:33
浏览 45
已采纳

每次明确打开时,PHP文件都会发送邮件

I have a contact page which made with the help of PHPMailer. The minimal code is as follows

<form class="form-horizontal" method="POST" action="Mail.php" name="contactForm">
...
</form>

This works perfectly fine, but whenever I try to open Mail.php explicitly on the webserver, the webserver sends blank email every time. Here's a screenshot of email whenever I access Mail.php screenshot

Below is the minimal code from PHPMailer's Mail.php

$mail->Subject = $subject;
    $mail->Body    = "<b>From: </b>". $sender. "<br>" ." <b>Name: </b>". $yourName. "<br>". "<b> Message Body </b>" .$message;
    $mail->AltBody = "<b>From: </b>". $sender. "<br>" ." <b>Name: </b>". $yourName. "<br>". "<b> Message Body </b>" .$message;

        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } 
        else {
            echo "Message has been sent....You're being redirected.....";
            }

How can I restrict the user to explicitly access Mail.php?

  • 写回答

3条回答 默认 最新

  • dongwei1954 2017-07-09 09:13
    关注

    So, as suggested by Jiri Hrazdil and Ahmad Mobaraki, using isset should solve my problem, but I couldn't figure out how to implement that. So I came up with my own solution. I check, if the variables are empty in the first place, So this way if anyone directly tries to access the file it'll show a message the Fields are empty, This is the way I did it.

    require 'PHPMailer/PHPMailerAutoload.php';
    $yourName = $_POST['yourName'];
    $sender  = $_POST['emailID'];
    $subject  = $_POST['subject'];
    $message  = $_POST['message'];
    $to = 'email@email.com';
    header('Refresh: 5; URL=whatever.php');
    
    if(empty($yourName) || empty($sender) || empty($subject) || empty($message) || empty($message))
    {
        echo "Fields are empty";
    }
    else
    {
        //Working Code of PHP Mailer
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀