PHP 的内置 mail() 函数可以通过 App Engine Mail API 发送电子邮件。这应该适用于大多数现有代码,前提是代码符合发送邮件中列出的限制条件。
或者,您可以直接调用 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';}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\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)."]]