0

I want to use PHPMailer for sending emails for registration as a verification method but for some reason the PHPMailer classes are not being recognised as if they were never installed


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require '../vendor/autoload.php';


function sendemail_verify($full_name,$email,$verify_token)
{
    $mail = new PHPMailer(true);
    $mail->isSMTP();                                                        //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                                   //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                               //Enable SMTP authentication
    $mail->Username   = 'bobomejl123@gmail.com';                            //SMTP username
    $mail->Password   = 'testsifrazamejl';   

enter image description here

enter image description here

enter image description here

LP1
  • 46
  • 6
  • 1
    require the autoloader before doing anything. – medilies Mar 12 '22 at 16:57
  • @medilies Could you be a little more specific? – LP1 Mar 12 '22 at 16:57
  • this line `require '../vendor/autoload.php';` needs to be executed before any `use` line. – medilies Mar 12 '22 at 16:58
  • @medilies I tried that it doesn't work sadly – LP1 Mar 12 '22 at 17:01
  • 1
    are you using a framework? it would be great if you expose some of you folder structure using `tree` command... did moving the `require` line to the top of the file change the error mesage ? – medilies Mar 12 '22 at 17:03
  • Yes moving it to the top changed the error. I am trying to use PHPMailer – LP1 Mar 12 '22 at 17:09
  • 2
    @medilies that's not true. `use` statements are typically placed before the autoloader, and they work just fine there as they are just local aliases, they don't load anything or create instances. The autoloader *must* be loaded before creating instances though. Make sure you are actually loading the classes through composer and that you have installed your dependencies. – Synchro Mar 12 '22 at 17:17
  • @Synchro I have followed the instructions on github and it should be running I really dont't know what could be the issue. I updated with a picture of my workspace where you can clearly see the classes however for some reason use doesn't see them – LP1 Mar 12 '22 at 17:20
  • 1
    That shows your IDE complaining, which isn't the same as the code not working. If your IDE isn't interpreting your composer config correctly it may be complaining incorrectly. – Synchro Mar 12 '22 at 17:23
  • @Synchro It's not just the IDE i keep getting the error on my page and the email isn't being sent I will update with the screenshot of the error – LP1 Mar 12 '22 at 17:24
  • Well that shows that PHPMailer is being loaded and run just fine (i.e. your IDE is wrong), but you have an SMTP authentication error that you should be able to diagnose by making use of `SMTPDebug = 2` – but that's a different question that has been asked here many times, and is covered in the PHPMailer docs. – Synchro Mar 12 '22 at 17:28
  • @Synchro but it's not working though. Didn't you see the new image with the complete error? And the email is not being sent. – LP1 Mar 12 '22 at 17:30
  • are you using your project from the command-line or the browser ? if the answer is browser then show the error from it – medilies Mar 12 '22 at 17:33
  • @medilies the screenshot of the error is above – LP1 Mar 12 '22 at 17:38
  • **Gmail** won't allow to use the real account password. go to https://myaccount.google.com/apppasswords and generate `key` to use on this app (that feature will require activating two factor authentication on your Gmail first). https://medium.com/@kumardeepak.xyz/how-to-setup-google-app-password-and-integrate-it-with-phpmailer-6-74c66332a017 – medilies Mar 12 '22 at 17:43
  • 1
    Does this answer your question? [Getting error while sending email through Gmail SMTP - "Please log in via your web browser and then try again. 534-5.7.14"](https://stackoverflow.com/questions/20337040/getting-error-while-sending-email-through-gmail-smtp-please-log-in-via-your-w) – medilies Mar 12 '22 at 17:56
  • @medilies No unfortunately – LP1 Mar 12 '22 at 18:00
  • 1
    Yes I did see the error, and strange as it may seem, I also read what it said, which told me that composer was working fine and that you have a very well-known problem that is described in great detail in the docs. – Synchro Mar 13 '22 at 14:29
  • I have found the solution thank you all for the help. The solution was to use a gmail account which doesn't have two facto authentication and allow in that account the use of less secure apps. This I probbably isn't the besto solution but it works for me – LP1 Mar 13 '22 at 15:35

0 Answers0