Go 1.11 telah mencapai akhir dukungan
dan akan dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi Go 1.11, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi Go 1.11 yang ada akan terus berjalan dan menerima traffic setelah tanggal penghentiannya. Sebaiknya Anda bermigrasi ke Go versi 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
Untuk mengirim email dari aplikasi:
Gunakan jenis mail.Message untuk menetapkan pengirim, penerima, subjek, dan isi pesan.
Kirim email dengan fungsi mail.Send.
Contoh berikut mengirim pesan email kepada pengguna sebagai konfirmasi bahwa mereka telah membuat akun baru dengan aplikasi:
import("bytes""fmt""net/http""google.golang.org/appengine""google.golang.org/appengine/log""google.golang.org/appengine/mail")funcconfirm(whttp.ResponseWriter,r*http.Request){ctx:=appengine.NewContext(r)addr:=r.FormValue("email")url:=createConfirmationURL(r)msg:=&mail.Message{Sender:"Example.com Support <support@example.com>",To:[]string{addr},Subject:"Confirm your registration",Body:fmt.Sprintf(confirmMessage,url),}iferr:=mail.Send(ctx,msg);err!=nil{log.Errorf(ctx,"Couldn't send email: %v",err)}}constconfirmMessage=`Thank you for creating an account!Please confirm your email address by clicking on the link below:%s`
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:
inbound_services:-mail
Siapkan pengendali untuk memproses email masuk, yang diberikan ke aplikasi Anda sebagai data MIME dalam permintaan POST HTTP.
Di aplikasi Anda, daftarkan pengendali ke jalur /_ah/mail/:
[[["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-09-04 UTC."],[[["\u003cp\u003eThis guide provides instructions on how to utilize the Mail API for sending and receiving emails in first-generation runtimes, with guidance for migration options if updating to App Engine Go 1.12+.\u003c/p\u003e\n"],["\u003cp\u003eSending mail involves using the \u003ccode\u003email.Message\u003c/code\u003e type to define the email's details and the \u003ccode\u003email.Send\u003c/code\u003e function to dispatch the message, as demonstrated with a confirmation email example.\u003c/p\u003e\n"],["\u003cp\u003eTo receive mail, you must enable incoming mail services in the \u003ccode\u003eapp.yaml\u003c/code\u003e file and set up a handler to process emails delivered as MIME data via HTTP \u003ccode\u003ePOST\u003c/code\u003e requests to the \u003ccode\u003e/_ah/mail/\u003c/code\u003e path.\u003c/p\u003e\n"],["\u003cp\u003eBefore sending any mail, all sender email addresses must be registered as authorized senders.\u003c/p\u003e\n"],["\u003cp\u003eReceiving email is only possible to addresses formatted as \u003ccode\u003eanything@appid.appspotmail.com\u003c/code\u003e, and this is independent of whether or not the app is on a custom domain.\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| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-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\nTo send mail from your application:\n\n1. Use the `mail.Message` type to set the sender, recipient, subject, and body\n of the message.\n\n2. Send the email with the `mail.Send` function.\n\nThe following example sends an email message to the user as a confirmation that\nthey have created a new account with the application: \n\n import (\n \t\"bytes\"\n \t\"fmt\"\n \t\"net/http\"\n\n \t\"google.golang.org/appengine\"\n \t\"google.golang.org/appengine/log\"\n \t\"google.golang.org/appengine/mail\"\n )\n\n func confirm(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \taddr := r.FormValue(\"email\")\n \turl := createConfirmationURL(r)\n \tmsg := &mail.Message{\n \t\tSender: \"Example.com Support \u003csupport@example.com\u003e\",\n \t\tTo: []string{addr},\n \t\tSubject: \"Confirm your registration\",\n \t\tBody: fmt.Sprintf(confirmMessage, url),\n \t}\n \tif err := mail.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/mail.html#google_golang_org_appengine_mail_Send(ctx, msg); err != nil {\n \t\tlog.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/log.html#google_golang_org_appengine_log_Errorf(ctx, \"Couldn't send email: %v\", err)\n \t}\n }\n\n const confirmMessage = `\n Thank you for creating an account!\n Please confirm your email address by clicking on the link below:\n\n %s\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:\n\n inbound_services:\n - mail\n\n2. Set up a handler to process incoming emails, which are supplied to your app\n as MIME data in an HTTP `POST` request.\n\n 1. In your app, register a handler to the `/_ah/mail/` path:\n\n func init() {\n \thttp.HandleFunc(\"/_ah/mail/\", incomingMail)\n }\n\n 2. In the handler, read the email's data from the `*http.Request`:\n\n func incomingMail(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \tdefer r.Body.Close()\n \tvar b bytes.Buffer\n \tif _, err := b.ReadFrom(r.Body); err != nil {\n \t\tlog.Errorf(ctx, \"Error reading body: %v\", err)\n \t\treturn\n \t}\n \tlog.Infof(ctx, \"Received mail: %v\", b)\n }\n\n You can use the [`net/mail`](http://golang.org/pkg/net/mail/) package in the\n standard library to parse mail messages."]]