PHP 5 telah mencapai akhir dukungan dan akan
dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi PHP 5, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi PHP 5 yang sudah ada akan terus berjalan dan menerima traffic setelah
tanggal penghentiannya. Sebaiknya Anda bermigrasi ke versi PHP terbaru yang didukung.
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Panduan ini menjelaskan cara menggunakan Mail API untuk mengirim dan menerima email.
Sebelum memulai
Anda harus mendaftarkan email pengirim sebagai pengirim yang sah. Untuk mengetahui informasi selengkapnya, lihat siapa yang dapat mengirim email.
Mengirim email
Fungsi mail()
bawaan PHP dapat mengirim email melalui Mail API App Engine. Ini akan berfungsi baik dengan
sebagian besar kode yang ada selama sesuai dengan batasan yang tercantum dalam
Mengirim email.
Atau, Anda dapat melakukan panggilan langsung ke 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';}
Menerima email
Anda dapat menyiapkan aplikasi untuk menerima email masuk di alamat dalam format berikut:
anything@appid.appspotmail.com
Untuk menerima email:
Aktifkan email masuk di file app.yaml aplikasi Anda. Tambahkan kode berikut ke
inbound_services.
-mail
Di file konfigurasi, buat pemetaan dari jalur URL yang mewakili
alamat email ke pengendali dalam kode aplikasi Anda. Pola /_ah/mail/.+
cocok dengan semua alamat email masuk:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]