PHP 5 has reached end of support and will be deprecated on January 31, 2026. After deprecation, you won't be able to deploy PHP 5 applications, even if your organization previously used an organization policy to re-enable deployments of legacy runtimes. Your existing PHP 5 applications will continue to run and receive traffic after their deprecation date. We recommend that you migrate to the latest supported version of PHP.

Sending and Receiving Mail with the Mail API

This guide describes how to use the Mail API to send and receive mail.

Before you begin

You must register your sender emails as authorized senders. For more information, see who can send email.

Sending mail

PHP's built-in mail() function can send emails via App Engine Mail API. This should work well with most existing code as long as it conforms to the restrictions listed in the Sending mail.

Alternatively, you can make direct calls to the Mail API:

use google\appengine\api\mail\Message;
// Notice that $image_content_id is the optional Content-ID header value of the
// attachment. Must be enclosed by angle brackets (<>)
$image_content_id = '<image-content-id>';
// Pull in the raw file data of the image file to attach it to the message.
$image_data = file_get_contents('image.jpg');
try {
 $message = new Message();
 $message->setSender('from@example.com');
 $message->addTo('to@example.com');
 $message->setSubject('Example email');
 $message->setTextBody('Hello, world!');
 $message->addAttachment('image.jpg', $image_data, $image_content_id);
 $message->send();
 echo 'Mail Sent';
} catch (InvalidArgumentException $e) {
 echo 'There was an error';
}

Receiving mail

You can set up your app to receive incoming email at addresses in the following format:

anything@appid.appspotmail.com

To receive email:

  1. Enable incoming mail in your app's app.yaml file. Add the following to the inbound_services:

    -mail
  2. In your configuration file, create mappings from URL paths that represent email addresses to handlers in your app's code. The pattern /_ah/mail/.+ matches all incoming email addresses:

    -url:/_ah/mail/.+
    script:handle_incoming_email.php
    login:admin
  3. Implement code for the handlers you specified in your application code.

    You can read the MIME data from php://input and parse the email content using Mailparse.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年01月02日 UTC.