dqouryz3595 2014-08-04 13:11
浏览 31
已采纳

PHP邮件在页面加载上发送空白消息

I am having an issue with PHP Mailer. It is sending a blank email every time the page is loaded.

I'm sure it's something simple (maybe missing a condition if submit button is hit) to fix this.

Their documentation doesn't seem to have one though and the script first worked when I first used it but then started sending emails on page load after the first few times. Thanks!

<form method="post" action="">
    <div class="form-group">
        <input name="name" type="text" class="form-control" placeholder="Enter Name">
    </div>
    <div class="form-group">
        <input name="email" type="email" class="form-control" placeholder="Enter Email">
    </div>
    <div class="form-group">
        <input name="subject" type="text" class="form-control" placeholder="Enter Subject">
    </div>
    <div class="form-group">
         <textarea name="message" class="form-control" rows="5" placeholder="Enter Message"></textarea>
    </div>
    <button name="submit" type="submit" value="submit" class="btn btn-submit">Submit</button>
</form>

<?php 

require '/phpmailer/PHPMailerAutoload.php';
require_once('/phpmailer/class.phpmailer.php');
include("/phpmailer/class.smtp.php");

$emailaddress = 'info@newpointdigital.com';

$message=
    'Name:  '.$_POST['name'].'<br />
    Email:  '.$_POST['email'].'<br />
    Subject:    '.$_POST['subject'].'<br />
    IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
    Message:<br /><br />
    '.nl2br($_POST['message']).'
    ';

$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
//$mail->SMTPDebug  = 2;                     // 1 = errors and messages,2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp.gmail.com"; // sets the SMTP server
$mail->Port       = 465;                    // set the SMTP port for the GMAIL server
$mail->Username   = "info@newpointdigital.com"; // SMTP account username (the email account your created)
$mail->Password   = "newpoint!@#$";        // SMTP account password (the password for the above email account)
$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted

$mail->CharSet  = 'UTF-8';  // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject    = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);

$mail->AddAddress($emailaddress);

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}       
?>
  • 写回答

2条回答 默认 最新

  • duanjuete9206 2014-08-04 13:13
    关注

    You don't check to see if a form submission occurred. You just call your email code when the page loads. There are several ways to decide if a form submission occurred. One way is to check to see if the form action is POST:

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // email code goes here
    }
    

    You can also check to see if the submit button was pressed:

     if (isset($_POST['submit'])) {
          // email code goes here
     }
    

    Other things to know:

    • You don't do any data validation so it is still possible for someone to submit nothing resulting in a blank email being sent
    • You don't protect against header injections which makes your form vulnerable to sending spam
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路