2

I know there are many threads about this already but I can't make it work and I've already tried everything! There is no autoload.php on the phpmailer download and I'm stuck.

Please help, this error "class 'phpmailer' not found" keeps appearing even though I've already linked the required files. Xampp is running. All mailer files are inside folder PHPMailer (which contains folder src/ and inside it there are those 5 files I've linked). Thanks in advance!

<!--Contact Starts-->
        <div class="container contactform center">
            <h2 class="text-center  wowload fadeInUp"></h2>
            <div class="row wowload fadeInLeftBig">      
                  <div class="col-sm-6 col-sm-offset-3 col-xs-12">
                  <form method="post" action="index.php">
                    <input type="text" placeholder="Nombre" name="nombre" required>
                    <input type="email" placeholder="Email" name="email" required>
                    <input type="text" placeholder="Móvil" name="movil" required>
                    <textarea rows="5" placeholder="Mensaje" name="mensaje" required></textarea>
                    <button class="btn btn-danger" type="submit" name="sendBtn"><i class="fa fa-paper-plane"></i> Send</button>
                  </form>
                  </div>
            </div>
        </div>
    </div>
    <!--Contact Ends-->

<?php

if(isset($_POST["sendBtn"])){
    require "PHPMailer/src/PHPMailer.php";
    require "PHPMailer/src/OAuth.php";
    require "PHPMailer/src/SMTP.php";
    require "PHPMailer/src/POP3.php";
    require "PHPMailer/src/Exception.php";

    //Create a new PHPMailer instance

    $mail = new PHPMailer();

    //Tell PHPMailer to use SMTP

    $mail->isSMTP();


    //Enable SMTP debugging

    // 0 = off (for production use)

    // 1 = client messages

    // 2 = client and server messages

    $mail->SMTPDebug = 2;



    //Set the hostname of the mail server

    $mail->Host = 'smtp.gmail.com';

    // use

    // $mail->Host = gethostbyname('smtp.gmail.com');

    // if your network does not support SMTP over IPv6

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission

    $mail->Port = 587;

    //Set the encryption system to use - ssl (deprecated) or tls

    $mail->SMTPSecure = 'tls';

    //Whether to use SMTP authentication

    $mail->SMTPAuth = true;

    //Username to use for SMTP authentication - use full email address for gmail

    $mail->Username = "xxx@gmail.com";

    //Password to use for SMTP authentication

    $mail->Password = "xxxxxx";

    //Set who the message is to be sent from

    $mail->setFrom($_POST["email"], $_POST["nombre"]);

    //Set who the message is to be sent to

    $mail->addAddress('xxx@gmail.com', 'John Doe');

    //Set the subject line

    $mail->Subject = 'PHPMailer GMail SMTP test';

    //Read an HTML message body from an external file, convert referenced images to embedded,

    //convert HTML into a basic plain-text alternative body

    $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));



    //Replace the plain text body with one created manually

    $mail->AltBody = $_POST["mensaje"] . '<br><p Móvil: '.$_POST["movil"].'</p>';


    //send the message, check for errors

    if (!$mail->send()) {

        echo "Mailer Error: " . $mail->ErrorInfo;

    } else {

        echo "Message sent!";

    }
}
BabyBoy
  • 33
  • 1
  • 1
  • 3
  • 1
    Looks like you missed the suggested sanity check to be done before anything else. https://github.com/PHPMailer/PHPMailer/wiki/Tutorial#first-time Maybe you did not install it properly – RiggsFolly Sep 05 '17 at 16:36
  • 2
    This is a `namespace` issue try `$mail = new PHPMailer\PHPMailer\PHPMailer();` – cmorrissey Sep 05 '17 at 16:39
  • Thank you! Worked!! – BabyBoy Sep 05 '17 at 17:33
  • You only need to `require` the classes you're actually using, which is probably only `PHPMailer` and `SMTP`. Alternatively, use composer as the docs suggest and it will take care of it all for you. – Synchro Sep 05 '17 at 18:55

2 Answers2

15

Method 1:
instead of this :

 //Create a new PHPMailer instance
$mail = new PHPMailer();

use this:

 //Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();

Edit on 2022-09-03
Method 2: (correct way)
as @Synchro said in comments, you can use the namespace in your script file instead of full namespace/class call:

// add namespace in top of your script
use PHPMailer\PHPMailer\PHPMailer;
// then call specify the class with this : 
$mail = new PHPMailer();
Root
  • 2,269
  • 5
  • 29
  • 58
  • 2
    An alternative is to import PHPMailer's class names into your namespace, e.g. with `use PHPMailer\PHPMailer\PHPMailer; $mail = new PHPMailer;` Both approaches are detailed in the readme. – Synchro Sep 05 '17 at 18:55
0

You have to install composer To install see below link http://webdevzoom.com/how-to-install-composer-on-windows/

run this command in your project folder of xampp in cmd (Locally) "composer require phpmailer/phpmailer"

then you will get the auto upload file in vendor folder. After that you have to have to add " require 'vendor/auto upload. php' " to your above code..

Then use respective file according to the requirements.. use 'phpmailer/phpmailer/PHPMailer'; use' phpmailer/phpmailer/Exception';

saicharan
  • 1
  • 1