Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Este guia descreve como usar a Mail API para enviar e receber e-mails.
Antes de começar
Você precisa registrar seus remetentes de e-mails como remetentes autorizados. Para mais informações, consulte quem pode enviar e-mails.
Como enviar e-mails
A função integrada mail() do PHP pode enviar e-mails por meio da API Mail do App Engine. Isso deve funcionar bem com a maioria dos códigos existentes, desde que esteja em conformidade com as restrições listadas no Envio de e-mails.
Como alternativa, é possível fazer chamadas diretas para a API Mail:
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';}
Como receber e-mails
É possível configurar seu aplicativo para receber e-mails de endereços no seguinte formato:
anything@appid.appspotmail.com
Para receber e-mails:
Ative os e-mails recebidos no arquivo app.yaml do seu aplicativo. Adicione o seguinte a inbound_services:
-mail
No arquivo de configuração, crie mapeamentos de caminhos de URL que representem endereços de e-mail para gerenciadores no código do aplicativo. O padrão /_ah/mail/.+ corresponde a todos os endereços de recebimento de e-mail:
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-11 UTC."],[[["\u003cp\u003eThis guide outlines how to utilize the Mail API for sending and receiving emails within the context of App Engine, particularly for first-generation runtimes.\u003c/p\u003e\n"],["\u003cp\u003ePHP's built-in \u003ccode\u003email()\u003c/code\u003e function can send emails using the App Engine Mail API, and direct calls to the API are also possible using the \u003ccode\u003egoogle\\appengine\\api\\mail\\Message\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eBefore sending emails, you must register your sender email addresses as authorized senders, according to the Mail API's guidelines.\u003c/p\u003e\n"],["\u003cp\u003eTo receive emails, enable incoming mail in your \u003ccode\u003eapp.yaml\u003c/code\u003e 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.\u003c/p\u003e\n"],["\u003cp\u003eYour app can receive incoming emails at addresses formatted as \u003ccode\u003eanything@appid.appspotmail.com\u003c/code\u003e, even if your app is deployed on a custom domain, as you cannot use custom domain addresses to receive emails.\u003c/p\u003e\n"]]],[],null,["# Sending and Receiving Mail with the Mail API\n\nThis guide describes how to use the Mail API to send and receive mail.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| php-gen2\n|\n| /services/access). If you are updating to the App Engine PHP 7/8 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/php-differences) to learn about your migration options for legacy bundled services.\n\nBefore you begin\n----------------\n\nYou must register your sender emails as authorized senders. For more\ninformation, see\n[who can send email](/appengine/docs/legacy/standard/python/mail#who_can_send_mail).\n\nSending mail\n------------\n\nPHP's built-in [`mail()`](https://php.net/manual/en/function.mail.php)\nfunction can send emails via App Engine Mail API. This should work well with\nmost existing code as long as it conforms to the restrictions listed in the\n[Sending mail](#Sending_mail).\n\nAlternatively, you can make direct calls to the Mail API: \n\n use google\\appengine\\api\\mail\\Message;\n\n // Notice that $image_content_id is the optional Content-ID header value of the\n // attachment. Must be enclosed by angle brackets (\u003c\u003e)\n $image_content_id = '\u003cimage-content-id\u003e';\n\n // Pull in the raw file data of the image file to attach it to the message.\n $image_data = file_get_contents('image.jpg');\n\n try {\n $message = new Message();\n $message-\u003esetSender('from@example.com');\n $message-\u003eaddTo('to@example.com');\n $message-\u003esetSubject('Example email');\n $message-\u003esetTextBody('Hello, world!');\n $message-\u003eaddAttachment('image.jpg', $image_data, $image_content_id);\n $message-\u003esend();\n echo 'Mail Sent';\n } catch (InvalidArgumentException $e) {\n echo 'There was an error';\n }\n\nReceiving mail\n--------------\n\nYou can set up your app to receive incoming email at addresses in the following\nformat: \n\n anything@appid.appspotmail.com\n\n| **Note:** Even if your app is deployed on a custom domain, you can't receive email sent to addresses in that domain.\n\nTo receive email:\n\n1. Enable incoming mail in your app's `app.yaml` file. Add the following to the\n `inbound_services`:\n\n - mail\n\n2. In your configuration file, create mappings from URL paths that represent\n email addresses to handlers in your app's code. The pattern `/_ah/mail/.+`\n matches all incoming email addresses:\n\n - url: /_ah/mail/.+\n script: handle_incoming_email.php\n login: admin\n\n3. Implement code for the handlers you specified in your application code.\n\n You can read the MIME data from\n [php://input](https://php.net/manual/en/wrappers.php.php#wrappers.php.input) and parse the email\n content using [Mailparse](http://php.net/manual/en/book.mailparse.php)."]]