Title: PHP on Google App Engine fails to send a mail
Tags: php;google-app-engine
Question: I am trying to send email in php using this code as it is given in documentation
require_once 'google/appengine/api/mail/Message.php';
use google\appengine\api\mail\Message;   //line 6 in my code
// ...
$message_body = "...";
$mail_options = [
"sender" =&gt; "lorrainegay@example.net",
"to" =&gt; "lorrainegay@example.net",
"subject" =&gt; "Your example.com account has been activated.",
"textBody" =&gt; $message_body
$message = new Message($mail_options);
$message-&gt;send();
} catch (InvalidArgumentException $e) {
// ...
and i am getting the following error
PHP Parse error:  syntax error, unexpected 'use' (T_USE) in /base/data/home/apps/s~myapp-home/mail.371603101929416156/save-email.php on line 6
Somehow appengine is throwing error on this line
use google\appengine\api\mail\Message;
Comment: AppEngine currently uses PHP 5.4.19. I have use statements in my code which work fine as long as they are before any other code in the file (only require_once etc before).

Comment: Might be that you're using an older version of PHP.  Check out http://php.net/manual/en/language.namespaces.importing.php where it says it requires >= 5.3.0

Comment: It is hosted on Google App Engine with runtime as "php" and version as "1" in app.yaml

Here is the accepted answer: Make sure your use statements are at the top of your code file, after the require_once but before any other code.

Here is another answer: Based on the suggestions of IanGSY i have now put the import statements on the top
<?
require_once 'google/appengine/api/mail/Message.php';
use php\sdk\google\appengine\api\mail\Message;
and that error is gone but now i am getting another error on this line
$message = new Message($mail_options);
PHP Fatal error:  Class 'php\sdk\google\appengine\api\mail\Message' not found in /base/data/home/apps/s~myapp-home/mail.371604278238118648/save-email.php on line 29
Comment for this answer: The use statement should be:
use google\appengine\api\mail\Message;
Which you had correct in your original question.
Comment for this answer: The use statement should be: use google\appengine\api\mail\Message;
Comment for this answer: Can you tell us all what you did to get it working? I'm also scratching my head with the same problem

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:11/13/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><google-app-engine>', 'Title': 'PHP on Google App Engine fails to send a mail', 'ViewCount': 1990, 'id': 19948471, 'user_AboutMe': '<p>Tech Entrepreneur who prefers to work from Himalayas.  </p>\n', 'user_AccountId': 1385145.0, 'user_DisplayName': 'HimalayanCoder', 'user_Reputation': 9261.0, 'user_Views': 727.0, 'user_id': 1317348.0}}