0

I am using Google App Engine and am trying to send email alerts with the Mail PHP API. I have defined a class with a public function but whenever I run it I get this error:

PHP Fatal error: Class 'Message' not found in...

PHP Code:

use \google\appengine\api\mail\Message;

    class crawls {
        public function check() {
                   try {
                    $message = new Message();
                    $message->setSender('Name <test@domain.com>');
                    $message->addTo($recipients);
                    $message->setSubject('Subject');
                    $message->setHTMLBody("<p>Message</p>");
                    $message->send();
                } catch (InvalidArgumentException $e) {
                    $error = "Unable to send mail. $e";
                }
    }
}

Everything works when I move the code outside of the class, but I want it inside the class.

Zeehad
  • 1,012
  • 1
  • 9
  • 11
ajgisme
  • 1,615
  • 2
  • 14
  • 28
  • try to include file and check the file if its in the folder – Mo Shal Feb 22 '16 at 10:46
  • It's not a file to be included. – ajgisme Feb 22 '16 at 15:33
  • @user5331188 were you able to resolve this issue? If so it is recommended to post your solution as the answer here to better help the community. If not it is now recommended to use [specific mail sending APIs](https://cloud.google.com/appengine/docs/standard/php/mail/sendgrid) outside of App Engine that are built for large mail distribution. – Jordan Sep 11 '17 at 14:06
  • @jordan yeah I couldn't fix this. Thanks for the info! – ajgisme Sep 12 '17 at 14:51

2 Answers2

0

Inherit Message class :

Define namespace if required.

class crawls extends \google\appengine\api\mail\Message {
    // your code
}
Monty
  • 1,110
  • 7
  • 15
  • Thanks, I tried that but I still get the error: PHP Fatal error: Class 'Message' not found in... – ajgisme Feb 23 '16 at 10:30
  • Thanks, now I get this error: PHP Fatal error: Class crawls may not inherit from final class (google\appengine\api\mail\Message) – ajgisme Feb 23 '16 at 11:56
0

Try this may it works for you:

class crawls {
    public function check() {
           try {
                $message = new \google\appengine\api\mail\Message();
                $message->setSender('Name <test@domain.com>');
                $message->addTo($recipients);
                $message->setSubject('Subject');
                $message->setHTMLBody("<p>Message</p>");
                $message->send();
            } catch (InvalidArgumentException $e) {
                $error = "Unable to send mail. $e";
            }
    }
}

Hope it helps