I'm trying to run PHPMailer only if my form is submitted. I've tried it a few different ways but no success.
I'm guessing my if (isset($_POST['submit'])) { tag is in the wrong spot? I'm also not sure what part of this code actually sends the email. When I test it using
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
it works, so I know it runs correctly. Any ideas how to run this only if my form is submitted?
<?php
require_once 'C:\wamp\www\phpmailer\PHPMailer-master\PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com '; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test@gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('tester@gmail.com', 'Marco'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'NEW APPLICATION';
$mail->Body = "--PERSONAL INFORMATION--
First Name: $fname Middle Name: $mname Last Name: $lname
Address:$address City:$city State:$state Zip:$zip ";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if (isset($_POST['submit']))
{
$mail->send();
}
?>