Viewed   142 times



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 = 'secret@secret.com';          // 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('secret@secret.com', 'Mailer');
$mail->addAddress('secret@secret.com', 'secret');     // Add a recipient
$mail->addAddress('secret@secret.com', 'secret');               // Name is optional
//$mail->addReplyTo('secret@secret.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 = '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

 Answers

2

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.

Monday, October 31, 2022
 
dmnd
 
2

Problem was in csrf token that wasn't sending with form. So I added

var _token = $('[name="_token"]').val(),

and send it with other form data and everything is ok.

Thursday, September 8, 2022
 
sheena
 
5

Use jQuery.ajax() function to submit a form without refreshing a page. You need to do something like this:

test.php:

<script type="text/javascript" src="jquery-version.js"></script>
<script type="text/javascript" src="ajaxform.js"></script>

<form action='process.php' method='post' class='ajaxform'>
 <input type='text' name='txt' value='Test Text'>
 <input type='submit' value='submit'>
</form>

process.php:

<?php
      // Get your form data here in $_POST
?>

ajaxform.js

jQuery(document).ready(function(){

    jQuery('.ajaxform').submit( function() {

        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            data    : $(this).serialize(),
            success : function( data ) {
                         alert('Form is successfully submitted');       
                      },
            error   : function(){
                         alert('Something wrong');
                      }
        });

        return false;
    });

});
Wednesday, November 9, 2022
 
shoham
 
4
foreach ($_POST as $Field=>$Value) { 
if($Value != ''){
$body .= "$Field: $Valuen";
}
}
Sunday, August 7, 2022
 
3

First, check if the "sendcopy" checkbox is checked:

$sendCopy = isset($_POST['sendcopy']);

You don't have to check its value. The frontend simply won't send anything if the checkbox is not checked, so if it exists in the $_POST array, that means the user has checked it.

Next, just send the exact same mail to the sender's email address:

        if ($sendCopy) {
            $sentToSender = mail($email, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head);
        }

You can add this code just before redirecting your user to the success page.

The missing parentheses have now been added to the code above, so it works well.

Saturday, November 19, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :