Comments (15)
A syntax error on that means that you're not using PHP 8.2, but something very old that doesn't support namespaces, which were introduced in PHP 5.3. I suggest you write a script containing <?php phpinfo();
to double check your version.
from phpmailer.
Ive double checked it: Same as before....
PHP Version 8.2.12 Build Date Nov 25 2023 08:05:53
from phpmailer.
This isn't something specific to PHPMailer. Try seeing if PHP gives you an error if you ask it to run this:
<?php
use abc\xyz;
That's a simple use
statement creating an alias for a non-existent class, but it should do nothing at all if you try to run it. If you run it on an old version of PHP, you should get the same error.
from phpmailer.
There is nothing and No errors.
from phpmailer.
OK, so now add the original lines back in, one at a time, until the problem starts happening.
from phpmailer.
My guess...
Switch the order of the "require" and "use" lines.
In your code you are using a namespace which is not available yet (since the loading of the files will happen AFTER the declaration.
I expect if you load the files before the "use", it should work (if the files are loaded properly)
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
from phpmailer.
No, that won't help. Use statements are just local aliases, the require statements have no effect on them. They only come into play when you instantiate a class.
from phpmailer.
OK, so now add the original lines back in, one at a time, until the problem starts happening.
theres is no change
My guess...
Switch the order of the "require" and "use" lines. In your code you are using a namespace which is not available yet (since the loading of the files will happen AFTER the declaration. I expect if you load the files before the "use", it should work (if the files are loaded properly)
require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
same...
from phpmailer.
Two questions:
- Are you using
<?php
at the start of your PHP file, incase unsafe / shorthand tags (<?
) are not allowed - In case you are not providing the full code file, are you sure you are not using "
use
" inside a class declaration? Like:
class Foo {
use PHPMailer\PHPMailer\PHPMailer;
}
it should be
use PHPMailer\PHPMailer\PHPMailer;
class Foo {
...
}
Maybe its good to provide you full file, since it seems to be a overall syntax error
from phpmailer.
Two questions:
1. Are you using `<?php` at the start of your PHP file, incase unsafe / shorthand tags (`<?`) are not allowed 2. In case you are not providing the full code file, are you sure you are **not** using "`use`" inside a class declaration? Like:
class Foo { use PHPMailer\PHPMailer\PHPMailer; }
it should be
use PHPMailer\PHPMailer\PHPMailer; class Foo { ... }
Maybe its good to provide you full file, since it seems to be a overall syntax error
I'll check it on tuesday, because my files are in the office ^^
from phpmailer.
So, what's the verdict?
from phpmailer.
Hello,
I have the same problem: PHP Parse error: syntax error, unexpected token "use"
The code I try to use is:
<? function cleaninput($input) { $input = trim($input); $input = stripslashes($input); $input = htmlspecialchars($input); return $input;} if (isset($_POST['name']) && isset($_POST['email'])) { if (isset($_POST['name'])) { $felado = cleaninput( $_POST['name'] ); } else { $felado = ''; }; if (isset($_POST['email'])) { $faemail = cleaninput( $_POST['email'] ); } else { $faemail = ''; }; if (isset($_POST['subject'])) { $targy = cleaninput( $_POST['subject'] ); } else { $targy = ''; }; if (isset($_POST['message'])) { $uzenet = cleaninput( $_POST['message'] ); } else { $uzenet = ''; }; require 'inc/Exception.php'; require 'inc/PHPMailer.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $mail = new PHPMailer(true); try { $mail->setLanguage('hu', 'inc/language/'); $mail->setFrom('[email protected]', $felado); $mail->addAddress('[email protected]', 'Someone'); $mail->addReplyTo($faemail, $felado); $mail->isHTML(true); $mail->Subject = 'Üzenet a weboldalról'; $mail->Body = 'Tárgy: <strong>' . $targy . '</strong><br/>Üzenet:<br/>' . $uzenet; $mail->AltBody = $uzenet; $mail->send(); $vissza = '<div id="form-message-success" class="mb-4">Üzenetét továbbítottuk.</div>'; } catch (Exception $e) { $vissza = "<div id='form-message-warning' class='mb-4'>Az üzenet küldése sikertelen. Hibaüzenet: {$mail->ErrorInfo}</div>"; }; }; header("Location: https://somedomain.hu/"); exit;
from phpmailer.
And what happens when you put the "use" and "require" lines on the top of your file, so as the first lines?
<?php
require ...
use...
from phpmailer.
And what happens when you put the "use" and "require" lines on the top of your file, so as the first lines?
<?php require ... use...
It seems this works.
Thank you!
:)
from phpmailer.
And what happens when you put the "use" and "require" lines on the top of your file, so as the first lines?
<?php require ... use...
It seems this works. Thank you! :)
You're welcome!
That was what I was saying all along!
"Use" is handled compile-time, not run-time.
I guess TS has the same problem
from phpmailer.
Related Issues (20)
- Setting Up PHPMailer with Microsoft Azure - Encountering Errors HOT 3
- addBCC doesn't work without AddAddress HOT 10
- SMTP Error: Could not connect to SMTP host. STARTTLS command failed Unrecognized command 'STARTTLS' HOT 9
- "Invalid address: (cc): " HOT 2
- Gmail
- hi i have problem- -function open ssl_pkey_free is depraced since php 8.0 HOT 2
- send differents body for mulitple bcc addresses in one send command HOT 4
- T
- Add the possibility of changing @phpmailer.0 string in asHtml method when composing image cid. HOT 2
- Missing header termination in multipart/mixed container HOT 2
- Issues with sending emails HOT 13
- 405 Not Allowed HOT 4
- SMTP Authentication not working with special characters like "§" HOT 1
- Withdraw/recall HOT 2
- edebug() should probably also get ride of "\r\n" when using LoggerInterface HOT 1
- postfix installation problem in PHP 5.5 test suite
- Not sending email when subject has first and last name in it HOT 7
- How do I only get the SMTP ERROR from debugging? HOT 1
- Can you add possibility to customized dsn by recipient ? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpmailer.