Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I'm trying to create a way to send auto emails on my website. I'm using phpmailermaster for this. to test the mailing out I created a button that when pressed should send an email to the email I have written in there. but I can't seem to get it to work? what am I doing wrong?? it continue to give me "kan geen mail versturen" this means in english "can't send a mail"

I tried to do an error check and I get this error:
Message could not be sent.Mailer Error: SMTP connect() failed. 


mailen.php

<?php
// Deze dependencies laden we handmatig in 
//ik laad hier de files van php mailer master in 
    use PHPMailer\PHPMailer\PHPMailer;
    require 'PHPMailer.php';
    require 'SMTP.php';
    require 'Exception.php';
//Deze function stuurt emails via Gmail
function mailen($ontvangerStraat = null, $ontvangerNaam = null, $onderwerp = null, $bericht = null) {

    $mail = new PHPMailer();

    //verbinden met Gmail
    $mail->IsSMTP(); //SMTP = simple mail transfer protocol
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl"; //zorgt voor versleutelde verbinding tussen 2 computers
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465;

    //Identificeer jezelf bij gmail'
    $mail->Username = "schoolrebecca98@gmail.com";
    $mail->Password = "xxxx"; 

    //email opstellen
    $mail->isHTML(true);
    //$mail->SetForm("schoolrebecca98@gmail.com", "Naam"); werkt niet bij deze versie
    $mail->From = 'schoolrebecca98@gmail.com';
    $mail->FromName = 'Naam';
    $mail->Subject = $onderwerp;
    $mail->CharSet = 'UTF-8';
    //heele rare rij code misschien hier naar kijken als het fout gaat
    $bericht = "<body style=\"font-family: Verdana, Verdana, Geneva, sans-serif; font-size: 14px; color: #000;\">". $bericht . "</body></html>";
    $mail->AddAddress($ontvangerStraat, $ontvangerNaam);
    //mail = bericht en moet in de body van het bericht komen te staan
    $mail->Body = $bericht;

    //stuur mail
    //dit werkt niet 
    if($mail->Send()){
        //mail succesfully sends
        echo "<script>alert('Mail is verstuurd');</script>";
    }else{ 
        //mail can't send
        echo "<script>alert('Kon geen mail versturen');</script>";
    }
}

//UPDATE 8-6-2021: de hele dag mee bezig geweest maar hij blijft me geen mail versturen.
//UPDATE 9-6-2021: maandag en dinsdag de heledag mee bezig geweest en het werkte niet en nu werkt het opeens wel???? 
//Maar hoe weet hij gedachte te verzenden? UPDATE dat doet hij in registreren.php
?>


the mail structure
<?php include "mailen.php";?>
<html>
   <!-- sending a mail -->
   <body>
      
      <?php
      //moet misschien nog in prepared statements maar eerst maar voor zorgen dat dit werkt
      //tijd plus 1 uur
         $newtime = date('H:i', time() + 3600);
         //echo $newtime;
         //$sql = "SELECT * FROM orders";
  
        // $result = $conn -> query($sql);
       
            // while($row = $result-> fetch_assoc()){
                  $email = "schoolrebecca98@gmail.com";
                  $klant = "test";
                  $onderwerp = "Bestelling";
                  $bericht = "Geachte $klant, uw bestelling is bevestigd! U kunt uw order ophalen om $newtime";
                  mailen($email, $klant, $onderwerp, $bericht);
            // }

      ?>
      
   </body>
</html>


the button that should send this mail however this doesn't work
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<button type="button" class="btn btn-primary"><a href="mail/bevestigordermail.php">sent mail</a></button>


What I have tried:

I have a book with a tutorial on how to do php mailer master but I can't get it to work nor do I see what I'm doing wrong. Tried looking it up on the internet but this also gave me no results. it does seem to be a common problem
Posted
Updated 7-Dec-21 22:49pm
v4
Comments
Richard MacCutchan 8-Dec-21 4:29am    
It is most likely Google blocking you. You need to authorise the web server with Google.
Rebecca2002 8-Dec-21 4:48am    
yes that was indeed the case. had to look on the internet how to do that but it works now.
Richard MacCutchan 8-Dec-21 6:26am    
This happened to me a long time ago and, sorry, but I could not recall how I fixed it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900