0

Failed to call new PHPMailer and does not create an instance of PHPMailer. The setting of $mail->SMTPDebug creates a stdClass instance, and then calling a non-existent method on it (isSMTP) fails. So it's all down to that instance creation failure. PHPMailer Version 6.0.7 Live Hosting Service

I have tried $mail = new stdClass();
or $mail = NULL; This is symptom fix but does not the cause and causes my page to send an email every time it loads.

 <?php 
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    require 'PHPMailer/Exception.php';
    require 'PHPMailer/PHPMailer.php';
    require 'PHPMailer/SMTP.php';


    if(isset($_POST[‘submit’]))

    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
    $mail->isSMTP(); 

    $mail->Host = 'mail.email.org'; 
    $mail->SMTPSecure = 'ssl'; <-- Recommend by the hosting service 
    $mail->Port = 465; <-- Recommend by the hosting service <--Hosting Service Docs verify this
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'Using the correct Password'; 


    $to = '[email protected]'; <-- sending this to myself
    $from = '[email protected]'; <--sending to myself


    $first_name = ((isset($_POST['FirstName']))&&(!is_null($_POST['FirstName'])))? $_POST['FirstName']:'';
    $last_name = ((isset($_POST['LastName']))&&(!is_null($_POST['LastName'])))? $_POST['LastName']:'';
    $email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
    $age = ((isset($_POST['Age']))&&(!is_null($_POST['Age'])))? $_POST['Age']:'';
    $student = ((isset($_POST['Student']))&&(!is_null($_POST['Student'])))? $_POST['Student']:'';
    $agree18 = ((isset($_POST['Agree18']))&&(!is_null($_POST['Agree18'])))? $_POST['Agree18']:'';


   /* Set the mail sender. */
    $mail->setFrom( $to , 'Research');

   /* Add a recipient. */
    $mail->addAddress( $_POST['Email'] , 'Research');
   /* Set the subject. */
    $mail->Subject = 'Learn More about Research Requested';

    $mail->isHTML(TRUE);
    $mail->Body = '<html> "First Name:" . $first_name . " Last Name:" . $last_name .  " Email:". $email . " Age:" . $age . " Student:" . $student . " Agree18:" . $agree18 . ""
</html>';


    $mail->AltBody = ' "First Name:" . $first_name . " Last Name:" . $last_name .  " Email:". $email . " Age:" . $age . " Student:" . $student . " Agree18:" . $agree18 . ""
';  

    if($mail->send()){

      $msg="Your email msg has been send";


    }else{

       $msg="mail msg has not been send"; 
       echo 'Mailer Error: ' . $mail->ErrorInfo;
     }
    ?>

Hope to find a solution to why it's failing.

Removed all my code, Performed what synchro stated and got this:

object(PHPMailer\PHPMailer\PHPMailer)#1 (74) { ["Priority"]=> NULL ["CharSet"]=> string(10) "iso-8859-1" ["ContentType"]=> string(10) "text/plain" ["Encoding"]=> string(4) "8bit" ["ErrorInfo"]=> string(0) "" ["From"]=> string(14) "root@localhost" ["FromName"]=> string(9) "Root User" ["Sender"]=> string(0) "" ["Subject"]=> string(0) "" ["Body"]=> string(0) "" ["AltBody"]=> string(0) "" ["Ical"]=> string(0) "" ["MIMEBody":protected]=> string(0) "" ["MIMEHeader":protected]=> string(0) "" ["mailHeader":protected]=> string(0) "" ["WordWrap"]=> int(0) ["Mailer"]=> string(4) "mail" ["Sendmail"]=> string(18) "/usr/sbin/sendmail" ["UseSendmailOptions"]=> bool(true) ["ConfirmReadingTo"]=> string(0) "" ["Hostname"]=> string(0) "" ["MessageID"]=> string(0) "" ["MessageDate"]=> string(0) "" ["Host"]=> string(9) "localhost" ["Port"]=> int(25) "Helo"]=> string(0) "" ["SMTPSecure"]=> string(0) "" ["SMTPAutoTLS"]=> bool(true) ["SMTPAuth"]=> bool(false) ["SMTPOptions"]=> array(0) { } ["Username"]=> string(0) "" ["Password"]=> string(0) "" ["AuthType"]=> string(0) "" ["oauth":protected]=> NULL ["Timeout"]=> int(300) ["dsn"]=> string(0) "" ["SMTPDebug"]=> int(0) ["Debugoutput"]=> string(4) "html" ["SMTPKeepAlive"]=> bool(false) ["SingleTo"]=> bool(false) ["SingleToArray":protected]=> array(0) { } ["do_verp"]=> bool(false) ["AllowEmpty"]=> bool(false) ["DKIM_selector"]=> string(0) "" ["DKIM_identity"]=> string(0) "" ["DKIM_passphrase"]=> string(0) "" ["DKIM_domain"]=> string(0) "" ["DKIM_copyHeaderFields"]=> bool(true) ["DKIM_extraHeaders"]=> array(0) { } ["DKIM_private"]=> string(0) "" ["DKIM_private_string"]=> string(0) "" ["action_function"]=> string(0) "" "XMailer"]=> string(0) "" ["smtp":protected]=> NULL ["to":protected]=> array(0) { } ["cc":protected]=> array(0) { } ["bcc":protected]=> array(0) { } ["ReplyTo":protected]=> array(0) { } ["all_recipients":protected]=> array(0) { } ["RecipientsQueue":protected]=> array(0) { } ["ReplyToQueue":protected]=> array(0) { } ["attachment":protected]=> array(0) { } ["CustomHeader":protected]=> array(0) { } "lastMessageID":protected]=> string(0) "" ["message_type":protected]=> string(0) "" ["boundary":protected]=> array(0) { } ["language":protected]=> array(0) { } ["error_count":protected]=> int(0) ["sign_cert_file":protected]=> string(0) "" ["sign_key_file":protected]=> string(0) "" ["sign_extracerts_file":protected]=> string(0) "" ["sign_key_pass":protected]=> string(0) "" ["exceptions":protected]=> bool(false) ["uniqueid":protected]=> string(0) ""

0

3 Answers 3

0

This is a duplicate of your previous question with a different title.

When you have code that doesn't work, cut it back to a minimal example so that you exclude opportunities for ambiguity, and enable verbose, visible error reporting:

use PHPMailer\PHPMailer\PHPMailer;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'PHPMailer/PHPMailer.php';
$mail = new PHPMailer;
var_dump($mail);

If that doesn't work, a more complex script isn't going to work either. For this to fail, I would suspect that there is something severely wrong with your PHP installation, and at the very least I would expect to see some warnings logged.

Sign up to request clarification or add additional context in comments.

6 Comments

No SMTP FILE Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\SMTP' not found in /PHPMailer/PHPMailer.php:1759 Stack trace: #0 /PHPMailer/PHPMailer.php(1884): PHPMailer\PHPMailer\PHPMailer->getSMTPInstance() #1 /PHPMailer/PHPMailer.php(1797): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #2 l/PHPMailer/PHPMailer.php(1539): PHPMailer\PHPMailer PHPMailer->smtpSend('Date: Tue, 11 J...', '<html> "First N...') #3 /PHPMailer/PHPMailer.php(1375): PHPMailer\PHPMailer\PHPMailer->postSend() #4 (69): PHPMailer\PHPMailer\PHPMailer->send() #5 {main} thrown in PHPMailer/PHPMailer.php on line 1759
That’s actually a positive sign relative to your other script! Try adding the SMTP use statement and the require for it too.
Would I need to add a use PHPMailer\PHPMailer\SMTP;? Cause I did not in the Org code.
I add the use SMTP line and it seems like it worked. I did not know that was required but all my error went away and it made the connection and sent. I am not sure if thats the fix? I will play around with it and confirm.
Strictly speaking you don’t need the use statement for the SMTP class because it’s never referred to directly in your code. You will need the require though. Glad it worked for you; if this answer helped you solve the issue, please mark this answer as such (click the check mark).
|
0

You forgot a bracket after condition with isset($_POST[...]).

if(isset($_POST['submit'])) {
    //                      ^^
    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
    $mail->isSMTP(); 
    ...
}
// of course, don'f forget to add a closing one too here.

4 Comments

The issue is when I add the {}, it does not send any emails to me. If I remove it and add the $mail = new stdClass(); or $mail = NULL; , It emails me. If I do both, it does not email me. Already redownloaded the PHPMailer Files as well with no change.
@TD00: brackets are correctly in there. So check if there is any input/button with name=submit in your form. If so, you have used bad quotes around the key. It has to be $_POST['submit'] not $_POST[‘submit’] (I've good ones quotes in my answer).
Both of these things came up in the original question discussion.
@Synchro: really don't check the chat, working just with info&codes published here in question.
0

Fixed <-- Thanks to Synchro --> & Thanks Panthers for the input

Added use PHPMailer\PHPMailer\SMTP; <- This fixed most of my issues but not always required per Synchro

For working example with PHPMailer 6.0.7 look for Synchro's
This is a duplicate of your previous question with a different title Link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.