Stay organized with collections
Save and categorize content based on your preferences.
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:
Enable incoming mail in your app's app.yaml file. Add the following to the
inbound_services:
-mail
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:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-03-05 UTC."],[[["This guide outlines how to utilize the Mail API for sending and receiving emails within the context of App Engine, particularly for first-generation runtimes."],["PHP's built-in `mail()` function can send emails using the App Engine Mail API, and direct calls to the API are also possible using the `google\\appengine\\api\\mail\\Message` class."],["Before sending emails, you must register your sender email addresses as authorized senders, according to the Mail API's guidelines."],["To receive emails, enable incoming mail in your `app.yaml` file, create URL mappings for incoming email addresses, and implement handlers in your app's code to process the received mail using the Mailparse extension."],["Your app can receive incoming emails at addresses formatted as `anything@appid.appspotmail.com`, even if your app is deployed on a custom domain, as you cannot use custom domain addresses to receive emails."]]],[]]