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';
}
?>