0

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';
}       
?>
Kevin
  • 41,694
  • 12
  • 53
  • 70
user3630457
  • 39
  • 1
  • 8

2 Answers2

3

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
Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

This happen cause you didn't apply any condition for this to run so code runs line by line on page load .

so keep your mail sending code in submit check condition so it will run after submit click

if(isset($_POST['submit'])) {
  // your mail sending code
}

or using any of your post data like for email check

if(!empty($_POST['email'])) {
  // your mail sending code
}
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44