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
Para enviar e-mails do seu aplicativo:
Use o tipo mail.Message para definir o remetente, o destinatário, o assunto e o corpo
da mensagem.
Envie o e-mail com a função mail.Send.
O exemplo a seguir mostra como enviar uma mensagem de e-mail ao usuário como uma confirmação de que foi criada uma nova conta com o aplicativo:
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`
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 app:
inbound_services:-mail
Configure um gerenciador para processar e-mails recebidos, fornecidos ao seu aplicativo como dados MIME em uma solicitação HTTP POST.
No seu app, registre um gerenciador para o caminho /_ah/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-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."]]