-1

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();
}
?>
Marco G
  • 11
  • 7
  • 1
    I'm betting your form element does not hold the matching name attribute. Error reporting will tell you. Plus, make sure your form does have a post method. – Funk Forty Niner Feb 02 '16 at 21:03
  • and the 4th one closed @Dagon *as per*. Edit: Hey, where'd you go George? – Funk Forty Niner Feb 02 '16 at 21:08
  • 1
    don't check for a post by looking for a form field. that's unreliable - you can make typos on either end, but `$_SERVER['REQUEST_METHOD'] == 'POST'` is 100% reliable. – Marc B Feb 02 '16 at 21:32
  • 'what part of this code actually sends the email' ... This part `if(!$mail->send())` This line will both controll the return value of `send()` method and run the `send()` method.. – ern Feb 02 '16 at 21:39
  • That's not the issue. I've edited the position of my if (isset($_POST['submit'])). This issue is my email is not sending at all like this. Although it works with the test i posted above – Marco G Feb 02 '16 at 21:42
  • So should i write something like this: if (isset($_POST['submit'])) { $mail->send() } ?? – Marco G Feb 02 '16 at 21:44
  • nope .. It should run after setting all attributes of `$mail` object .. – ern Feb 02 '16 at 21:55
  • I just edited the code. That's my best guess. How would you do it? – Marco G Feb 02 '16 at 22:05
  • @MarcoG does it solved your problem adding `$mail->send()` at the and of your "old" script? – ern Feb 02 '16 at 22:07
  • Your edit also should work " if there is no errors " but it will create an object and set the attributes unnecessarily .. But also read Marc B's comment.. – ern Feb 02 '16 at 22:15
  • Still doesn't work. The email isn't sending – Marco G Feb 02 '16 at 23:27

1 Answers1

0

Make sure you have an

<input type="hidden" name="submit" value="foo" />

or change your submit button to

<input type="submit" value="Submit!" name="submit" />

to make sure that you know when your form has been submitted with the isset() check

user5697101
  • 571
  • 4
  • 12