Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta guía, se describe cómo usar la API de correo para enviar y recibir correos electrónicos.
Antes de comenzar
Debes registrar los correos electrónicos de tus remitentes como remitentes autorizados. Para obtener más información, consulta quién puede enviar correos electrónicos.
Envía correos electrónicos
Para enviar un correo electrónico desde la aplicación, realiza los siguientes pasos:
Usa el tipo mail.Message para configurar el remitente, destinatario, asunto y cuerpo del mensaje.
Envía el correo electrónico con la función mail.Send.
En el siguiente ejemplo, se envía un mensaje de correo electrónico al usuario como confirmación de que creó una cuenta nueva con la aplicación:
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`
Cómo recibir correos electrónicos
Puedes configurar la aplicación para que reciba correos electrónicos entrantes en las direcciones con el siguiente formato:
anything@appid.appspotmail.com
Para recibir correos electrónicos, realiza los siguientes pasos:
Habilita el correo electrónico entrante en el archivo app.yaml de tu app:
inbound_services:-mail
Configura un controlador para procesar los correos electrónicos entrantes, que se suministran a la app como datos MIME en una solicitud POST HTTP.
En la app, registra un controlador para la ruta de acceso /_ah/mail/:
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 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."]]