brokkosnarf
brokkosnarf

Reputation: 11

phpmailer wont send email after submitting form



Im messing around with phpmailer and i've got everything working.
But what im trying to do now is after submitting a form send a mail. Not anything too difficult just a basic email that acknowledges the form has been submitted (no form data).

problem : email is not sending after submitting form (email code is working 100% tested)

Hope someone can help me out :)

mail.php code :

<?php
//ini_set(‘display_errors’, 1);

include '/var/www/includes/mailer.php';

//require 'PHPMailerAutoload.php';

 $mail = new PHPMailer;

 $mail->SMTPDebug = 3;                                 // Enable verbose      debug output

$mail->isSMTP();                                      // Set mailer to  use SMTP
$mail->Host = 'smtp.nicetrygoyim.nl';                       //Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication


$mail->Username = '[email protected]';          // SMTP username
$mail->Password = 'nicetry';                             // SMTP password
$mail->SMTPSecure = 'TLS';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'secret');     // Add a recipient
$mail->addAddress('[email protected]', 'secret');               // Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');

//$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 = 'Here is the subject';
  $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';


   $mail->smtpConnect([
   'ssl' => [
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
     ]
      ]);

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

my Form :

<form action="mail.php" method="post">
 Leerlingnummer:<br>
 <input type="text" name="leerlingnummer"required placeholder="Voer hier het leerlingnummer in" /><br>
 E-mailadres:<br>

 <input type="submit" name="submit" class="groottext" value="Reparatie    indienen"/>

edit : typo

edit : forgot to mention problem

Upvotes: 0

Views: 405

Answers (1)

Synchro
Synchro

Reputation: 37810

If this code is running, you should be seeing a ton of debug output, even if it is working correctly. You don't actually say what the problem is, but you're doing a few things wrong that I can see. It would really help if you based your code on the examples provided and read the docs instead of just guessing.

$mail->SMTPSecure = 'TLS';

should be:

$mail->SMTPSecure = 'tls';

Don't call smtpConnect() yourself, you'll mess up the tracking of SMTP transaction state. If you want to set SSL params, set them the expected way and then just call send(), which will deal with the connection:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

The next question is why are you doing that? If you can't provide an explicit, specific reason for doing that, you're doing something wrong.

Upvotes: 1

Related Questions