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

Need Help #2313

Closed
SparkyAPI opened this issue Apr 13, 2021 · 37 comments
Closed

Need Help #2313

SparkyAPI opened this issue Apr 13, 2021 · 37 comments

Comments

@SparkyAPI
Copy link

SparkyAPI commented Apr 13, 2021

Hello. Im using Apache2 and wanna send some mails. But when i write "new PHPMailer()", i get an error, that the function would be undefined and the whole functions of PHPMailer wont work.

The code:
https://prnt.sc/11blkvv

Error:
https://prnt.sc/11bm6wu

The folder where PHPMailer is:
https://prnt.sc/11bllmd

@XL-2000
Copy link

XL-2000 commented Apr 13, 2021

Without any in-depth views on your code, I see some things wrong here....

  1. You declare "use" before "require", in other words, you try to use something before it is there... switch these around
  2. "new PHPMailer" does not assign the PHPMailer to a variable to use. I think it should be something like "$phpmailer = new PHPMailer;"
  3. Your require path should be something like '../PHPMailer...' or '$_SERVER['DOCUMENT_ROOT'].'/PHPMailer...' or whatever

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

I can't see any of those screen shots. Please post code and error messages as text, not images. Please make sure you've read the instructions on how to load PHPMailer.

@Synchro Synchro closed this as completed Apr 13, 2021
@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

use is fine before require. They define local aliases, and have no relation to files or loaded classes – until you create instances of them, which can happen much later.

@SparkyAPI
Copy link
Author

Without any in-depth views on your code, I see some things wrong here....

  1. You declare "use" before "require", in other words, you try to use something before it is there... switch these around
  2. "new PHPMailer" does not assign the PHPMailer to a variable to use. I think it should be something like "$phpmailer = new PHPMailer;"
  3. Your require path should be something like '../PHPMailer...' or '$_SERVER['DOCUMENT_ROOT'].'/PHPMailer...' or whatever

What do you mean with step 3? Where should i put the PHPMailer Folder in?

@SparkyAPI
Copy link
Author

use is fine before require. They define local aliases, and have no relation to files or loaded classes – until you create instances of them, which can happen much later.

It still wont work...

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

You shouldn't. Learn how to use composer and it will worry about that for you.

@SparkyAPI
Copy link
Author

SparkyAPI commented Apr 13, 2021

use is fine before require. They define local aliases, and have no relation to files or loaded classes – until you create instances of them, which can happen much later.

    require 'PHPMailer/src/Exception.php';
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';

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

    $phpmailer = new PHPMailer;

Right?

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

It will work, but convention, and all the PHPMailer docs and examples, put use statements first. I suggest you base your code on the examples provided.

If your requires don't work, check that you are using the correct paths to wherever you put your PHPMailer files. You've not posted your error messages so I have no idea what's going wrong for you.

@SparkyAPI
Copy link
Author

It will work, but convention, and all the PHPMailer docs and examples, put use statements first. I suggest you base your code on the examples provided.

If your requires don't work, check that you are using the correct paths to wherever you put your PHPMailer files. You've not posted your error messages so I have no idea what's going wrong for you.

If i load the page, the whole page wont work. My INDEX is iin test and the PHPMailer is in test/PHPMailer/src/

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

Any fatal error messages will be in your web sever's error log. If you're writing PHP, you need to know where that is.

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

You could try adding this to the beginning of your script, before the requires:

ini_set('display_errors', true);
error_reporting(E_ALL);

But be sure to remove them in production.

@SparkyAPI
Copy link
Author

You could try adding this to the beginning of your script, before the requires:

ini_set('display_errors', true);
error_reporting(E_ALL);

But be sure to remove them in production.

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer' not found in /var/www/html/team/test/index.php:13 Stack trace: #0 {main} thrown in /var/www/html/team/test/index.php on line 13

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

Please post your actual code.

@SparkyAPI
Copy link
Author

SparkyAPI commented Apr 13, 2021

Please post your actual code.

Now, the site is loading... and loading...

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
   //Import PHPMailer classes into the global namespace
   //These must be at the top of your script, not inside a function
   require 'PHPMailer/src/Exception.php';
   require 'PHPMailer/src/PHPMailer.php';
   require 'PHPMailer/src/SMTP.php';

   use PHPMailer\PHPMailer\PHPMailer;
   use PHPMailer\PHPMailer\SMTP;
   use PHPMailer\PHPMailer\Exception;
   
   //Load Composer's autoloader
   
  
?>
<!DOCTYPE html>
<html lang="de">

<head>
    <title>PHP SMPT Mailer</title>
</head>

<body>
<?php
     //Instantiation and passing `true` enables exceptions
   $mail = new PHPMailer(true);
   
   try {
       //Server settings
       $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
       $mail->isSMTP();                                            //Send using SMTP
       $mail->Host       = 'smtp.google.com';                     //Set the SMTP server to send through
       $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
       $mail->Username   = 'mymail';                     //SMTP username
       $mail->Password   = 'mypw';                               //SMTP password
       $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
       $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
   
       //Recipients
       $mail->setFrom('[email protected]', 'Mailer');
       $mail->addAddress('[email protected]', 'Joe User');     //Add a recipient
       $mail->addAddress('[email protected]');               //Name is optional
   
       //Attachments
       //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
      // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name
   
       //Content
       $mail->isHTML(true);                                  //Set email format to HTML
       $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';
   
       $mail->send();
       echo 'Message has been sent';
   } catch (Exception $e) {
       echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
   }
    
   ?>
   <form method="POST" action="index.php">
        <button type="submit" name="submit">COOOL</button>
    </form>
</body>

</html>

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

This code will attempt to send a message every time you load the page. If your hosting provider blocks outbound SMTP (as many do), you will likely have to wait for SMTP's 10-minute timeout period to expire before the script will continue. This is addressed in the troubleshooting guide, including tests to help you diagnose it. Setting $mail->SMTPDebug = SMTP::DEBUG_CONNECTION; may make this more apparent.

@SparkyAPI
Copy link
Author

This code will attempt to send a message every time you load the page. If your hosting provider blocks outbound SMTP (as many do), you will likely have to wait for SMTP's 10-minute timeout period to expire before the script will continue. This is addressed in the troubleshooting guide, including tests to help you diagnose it. Setting $mail->SMTPDebug = SMTP::DEBUG_CONNECTION; may make this more apparent.

I built a little form now with executing the mail stuff. But the site is still loading the whooooole time

@SparkyAPI
Copy link
Author

And now after removing SMTPDebug i've got that error:
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

Sigh. The same SMTPDebug output that would show you what's happening... Re-enable it and post the output here.

@SparkyAPI
Copy link
Author

Sigh. The same SMTPDebug output that would show you what's happening... Re-enable it and post the output here.

Bro now im not getting an error, but i dont receive the email

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

Show the debug output. It will show you the SMTP transaction whether it's successful or not.

@SparkyAPI
Copy link
Author

Show the debug output. It will show you the SMTP transaction whether it's successful or not.

SMTP::DEBUG_CONNECTION right?

@SparkyAPI
Copy link
Author

Show the debug output. It will show you the SMTP transaction whether it's successful or not.

Yeah, invalid user or password. Im at yandex

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

I don't know how many times I need to say this, but POST YOUR DEBUG OUTPUT.

@SparkyAPI
Copy link
Author

SparkyAPI commented Apr 13, 2021

I don't know how many times I need to say this, but POST YOUR DEBUG OUTPUT.

First, thank you for your help bro. Im from germany okay xD

The output:

2021-04-13 09:27:59 Connection: opening to smtp.google.com:25, timeout=300, options=array()
2021-04-13 09:27:59 Connection: opened
2021-04-13 09:27:59 SERVER -> CLIENT: 220 mx.google.com ESMTP f7si14528561wrx.322 - gsmtp
2021-04-13 09:27:59 CLIENT -> SERVER: EHLO sparkysky.de
2021-04-13 09:27:59 SERVER -> CLIENT: 250-mx.google.com at your service, [45.146.252.194]250-SIZE 157286400250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-04-13 09:27:59 CLIENT -> SERVER: STARTTLS
2021-04-13 09:27:59 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-04-13 09:27:59 CLIENT -> SERVER: EHLO sparkysky.de
2021-04-13 09:27:59 SERVER -> CLIENT: 250-mx.google.com at your service, [45.146.252.194]250-SIZE 157286400250-8BITMIME250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
2021-04-13 09:27:59 CLIENT -> SERVER: QUIT
2021-04-13 09:27:59 SERVER -> CLIENT: 221 2.0.0 closing connection f7si14528561wrx.322 - gsmtp
2021-04-13 09:27:59 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

You are connecting to the wrong server to send through gmail (though you said you're sending through Yandex??). As in the gmail example provided, it should be:

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

@SparkyAPI
Copy link
Author

SparkyAPI commented Apr 13, 2021

You are connecting to the wrong server to send through gmail (though you said you're sending through Yandex??). As in the gmail example provided, it should be:

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

Oops bro im sorry im using gmail in the example i sent you.
I tried yandex and gmail

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

You're connecting to the wrong port – should be 587, as per your script. Looks like gmail doesn't offer authentication on port 25, which is as expected.

@SparkyAPI
Copy link
Author

SparkyAPI commented Apr 13, 2021

587

If im adding the variable Port, the whole site will loading

@SparkyAPI
Copy link
Author

SparkyAPI commented Apr 13, 2021

My current Code:
`$mail = new PHPMailer();

try {
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = "smtp.google.com"; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = "[email protected]"; //SMTP username
$mail->Password = "pw"; //SMTP password
$mail->Port = 587;

$mail->setFrom("[email protected]", "SparkySky.de");
$mail->addAddress("[email protected]", "Robin");

$mail->Subject = "Test";
$mail->Body = "Wenn das geht bin ich ein Gott!";

if($mail->send()){
echo "Versendet";
}else{
echo "Fehler!".$mail->ErrorInfo;
}
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}`

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

So we are back to your host blocking it. Read their docs, see what they have to say about outbound SMTP. You could try:

$mail->Port = 465;
$mail->SMTPSecure = 'ssl';

though it's likely you'll have the same result.

@SparkyAPI
Copy link
Author

So we are back to your host blocking it. Read their docs, see what they have to say about outbound SMTP. You could try:

$mail->Port = 465;
$mail->SMTPSecure = 'ssl';

though it's likely you'll have the same result.

So i have to contact my host?

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

Yes, or at least read their docs. This is usually a pretty common question, so they should document it.

@SparkyAPI
Copy link
Author

Yes, or at least read their docs. This is usually a pretty common question, so they should document it.

My host said that they're not blocking ports

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

So do the tests to confirm that. They are all in the troubleshooting guide. You're not the first to have this kind of hosting problem.

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

You are very, very unlikely to need to do that. It's very rare for cheap hosting to do any kind of egress blocking from within instances (their blocks will be implemented upstream, at the network level). Also SMTP uses TCP, not UDP. Find your problem before trying to fix it. Changing random things is unlikely to help, and if you do accidentally fix it, you won't know how or why.

@SparkyAPI
Copy link
Author

You are very, very unlikely to need to do that. It's very rare for cheap hosting to do any kind of egress blocking from within instances (their blocks will be implemented upstream, at the network level). Also SMTP uses TCP, not UDP. Find your problem before trying to fix it. Changing random things is unlikely to help, and if you do accidentally fix it, you won't know how or why.

Now i've got this error:
SSL loaded 2021-04-13 12:00:38 Connection: opening to ssl:https://smtp.google.com:25, timeout=300, options=array() 2021-04-13 12:00:38 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:1408F10B:SSL routines:ssl3_get_record:wrong version number [/var/www/html/team/test/PHPMailer/src/SMTP.php line 387] 2021-04-13 12:00:38 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [/var/www/html/team/test/PHPMailer/src/SMTP.php line 387] 2021-04-13 12:00:38 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl:https://smtp.google.com:25 (Unknown error) [/var/www/html/team/test/PHPMailer/src/SMTP.php line 387] 2021-04-13 12:00:38 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Fehler!SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

@Synchro
Copy link
Member

Synchro commented Apr 13, 2021

This is exactly what I mean by "changing random things". SMTPSecure = 'ssl' to port 25 will never work. That is why the guide tells you exactly that, and that none of the examples use that combination. If telnet to a port doesn't connect, openssl to the same port will not work either, and nor will anything from PHP.

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

3 participants