Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't send test email from bare PHP to SMTP #2966

Closed
SonicSong opened this issue Oct 14, 2023 · 6 comments
Closed

Can't send test email from bare PHP to SMTP #2966

SonicSong opened this issue Oct 14, 2023 · 6 comments

Comments

@SonicSong
Copy link

I have an issue when trying to send test email from bare PHP on my local machine via SMTP

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'libs/PHPMailer-master/src/Exception.php';
require 'libs/PHPMailer-master/src/PHPMailer.php';
require 'libs/PHPMailer-master/src/SMTP.php';

$mail = new PHPMailer(true);

try {
//Server settings
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'fedora.email';
    $mail->Port = 587;
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->Username = '<username>';
    $mail->Password = "<password>";

    //Recipients
    $mail->setFrom('<email>', '<name>');
    $mail->addAddress('<email>', '<name>');     //Add a recipient

    //Content
    //$mail->isHTML(true);
    $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';

    //echo "Probably works?<br>";
    //var_dump($mail);

    return $mail->send();

/*} catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else! */
//}catch (Exception $e) {
//    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
//}
    echo "Email sent successfully!";
} catch (phpmailerException $e) {
    echo "PHPMailer Error: " . $e->errorMessage();
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

Error that I get is this:

PHP Fatal error:  Uncaught Error: Undefined constant "PHPMailer\PHPMailer\PHPMailer" in /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php:2021
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php(1682): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Sat, 14 O...', 'This is the HTM...')
#1 /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->postSend()
#2 /Applications/XAMPP/xamppfiles/htdocs/form-php/form_mail.php(37): PHPMailer\PHPMailer\PHPMailer->send()
#3 {main}
  thrown in /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php on line 2021

Fatal error: Uncaught Error: Undefined constant "PHPMailer\PHPMailer\PHPMailer" in /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php:2021
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php(1682): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Sat, 14 O...', 'This is the HTM...')
#1 /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->postSend()
#2 /Applications/XAMPP/xamppfiles/htdocs/form-php/form_mail.php(37): PHPMailer\PHPMailer\PHPMailer->send()
#3 {main}
  thrown in /Applications/XAMPP/xamppfiles/htdocs/form-php/libs/PHPMailer-master/src/PHPMailer.php on line 2021

I'm trying to run this via XAMPP and local PHP install that is on version 8.2.11.
In the end it is supposed to work on LAMP server.

@Synchro
Copy link
Member

Synchro commented Oct 14, 2023

It's unclear where that is coming from. One thing though, you are catching an exception class called phpmailerException, but that doesn't exist.

@SonicSong
Copy link
Author

After adding smtpConnect() and smtpClose I was able to use the appliaction key and it seemed like it worked but in the end I didn't received any email.

In terminal I've got this message

2023-10-14 16:36:27	SERVER -> CLIENT: 220 fedora.email ESMTP Welcome to feeder! [zone-mta/3.6.5]
2023-10-14 16:36:27	CLIENT -> SERVER: EHLO <device>
2023-10-14 16:36:27	SERVER -> CLIENT: 250-fedora.email Nice to meet you, <idk what is that but probably good to cut>
                   	                  250-PIPELINING
                   	                  250-8BITMIME
                   	                  250-SMTPUTF8
                   	                  250-AUTH LOGIN PLAIN
                   	                  250-STARTTLS
                   	                  250 SIZE 31457280
2023-10-14 16:36:27	CLIENT -> SERVER: STARTTLS
2023-10-14 16:36:27	SERVER -> CLIENT: 220 Ready to start TLS
2023-10-14 16:36:28	CLIENT -> SERVER: EHLO <device>
2023-10-14 16:36:28	SERVER -> CLIENT: 250-fedora.email Nice to meet you, <idk what is that but probably good to cut>
                   	                  250-PIPELINING
                   	                  250-8BITMIME
                   	                  250-SMTPUTF8
                   	                  250-AUTH LOGIN PLAIN
                   	                  250 SIZE 31457280
2023-10-14 16:36:28	CLIENT -> SERVER: AUTH LOGIN
2023-10-14 16:36:28	SERVER -> CLIENT: 334 <id?>
2023-10-14 16:36:28	CLIENT -> SERVER: [credentials hidden]
2023-10-14 16:36:29	SERVER -> CLIENT: 334 <id?>
2023-10-14 16:36:29	CLIENT -> SERVER: [credentials hidden]
2023-10-14 16:36:29	SERVER -> CLIENT: 235 Authentication successful
2023-10-14 16:36:29	CLIENT -> SERVER: QUIT
2023-10-14 16:36:29	SERVER -> CLIENT: 221 Bye
Email sent successfully!%                                                                                             

I have no idea how to move forward with this as it seems like it does connect to SMTP but in the end it doesn't send anything.

Closed by accident sorry

@SonicSong SonicSong reopened this Oct 14, 2023
@SonicSong
Copy link
Author

Okay. Figured out that on PHPMailer-6.8.0 it worked without any issue. No idea what's wrong with 6.8.1.

Screenshot 2023-10-16 at 09 13 13

@Synchro
Copy link
Member

Synchro commented Oct 16, 2023

I don't know what code that is, but it's not from PHPMailer. At a guess I'd say it's perhaps a child class that needs updating?

@SonicSong
Copy link
Author

SonicSong commented Oct 16, 2023

To be fair I got the library from this link that was in README.md in the manual installation section.

It can be that it was just outdated and it was fully my fault for not getting it from the Releases.

@Synchro
Copy link
Member

Synchro commented Oct 16, 2023

The code in the current download of that file is the same. An obvious syntax error like that would never have been in a release version as even the most basic tests would have failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants