Python 2.7 ha raggiunto la fine del supporto
e verrà ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment di applicazioni Python 2.7, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment di runtime legacy. Le tue applicazioni Python 2.7 esistenti continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti consigliamo di eseguire la migrazione all'ultima versione supportata di Python.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
L'API Mail fornisce due modi per inviare un messaggio email: la funzione
mail.send_mail() e la classe EmailMessage.
L'invio è asincrono: la funzione mail.send_mail() e il metodo EmailMessage.send() trasmettono i dati del messaggio al servizio di posta e poi vengono restituiti. Il servizio di posta mette in coda il messaggio, quindi tenta di
inviarlo, riprovando se il server di posta di destinazione non è disponibile. Gli errori e
i messaggi di mancato recapito vengono inviati all'indirizzo del mittente del messaggio email.
Prima di iniziare
Devi registrare le email del mittente come mittenti autorizzati. Per ulteriori
informazioni, vedi
Chi può inviare email.
Invio di posta con mail.send_mail()
Per inviare email utilizzando la funzione mail.send_mail(), utilizza i campi del messaggio email come parametri, inclusi il mittente, i destinatari, l'oggetto e il corpo del messaggio. Ad esempio:
mail.send_mail(sender=sender_address,to="Albert Johnson <Albert.Johnson@example.com>",subject="Your account has been approved",body="""Dear Albert:Your example.com account has been approved. You can now visithttp://www.example.com/ and sign in using your Google Account toaccess new features.Please let us know if you have any questions.The example.com Team""")
Invio di posta con EmailMessage
Per inviare posta utilizzando oggetti con la classe
EmailMessage, passa i campi del messaggio email al
costruttore EmailMessage e utilizza gli attributi dell'istanza per aggiornare
il messaggio.
Il metodo EmailMessage.send() invia il messaggio email
rappresentato dagli attributi dell'istanza. Un'applicazione può riutilizzare un'istanza EmailMessage modificando gli attributi e chiamando di nuovo il metodo send().
message=mail.EmailMessage(sender=sender_address,subject="Your account has been approved")message.to="Albert Johnson <Albert.Johnson@example.com>"message.body="""Dear Albert:Your example.com account has been approved. You can now visithttp://www.example.com/ and sign in using your Google Account toaccess new features.Please let us know if you have any questions.The example.com Team"""message.send()
L'esempio seguente mostra l'invio di un messaggio per confermare un indirizzo
email:
classUserSignupHandler(webapp2.RequestHandler):"""Serves the email address sign up form."""defpost(self):user_address=self.request.get('email_address')ifnotmail.is_email_valid(user_address):self.get()# Show the form again.else:confirmation_url=create_new_user_confirmation(user_address)sender_address=('Example.com Support <{}@appspot.gserviceaccount.com>'.format(app_identity.get_application_id()))subject='Confirm your registration'body="""Thank you for creating an account!Please confirm your email address by clicking on the link below:{}""".format(confirmation_url)mail.send_mail(sender_address,user_address,subject,body)
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eThe mail API offers two methods for sending emails: the \u003ccode\u003email.send_mail()\u003c/code\u003e function and the \u003ccode\u003eEmailMessage\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eEmail sending is asynchronous, meaning the function or method returns immediately after sending the message data to the mail service, which then queues and attempts delivery.\u003c/p\u003e\n"],["\u003cp\u003eTo use this API you need to register your sender emails as authorized senders.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003email.send_mail()\u003c/code\u003e function takes email fields as parameters, whereas the \u003ccode\u003eEmailMessage\u003c/code\u003e class uses attributes of an instance to construct and send emails.\u003c/p\u003e\n"],["\u003cp\u003eThis API can only run in first-generation runtimes in the App Engine standard environment.\u003c/p\u003e\n"]]],[],null,["# Sending Mail\n\nThe mail API provides two ways to send an email message: the\n[`mail.send_mail()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.mail#google.appengine.api.mail.send_mail) function and the [`EmailMessage`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.mail#google.appengine.api.mail.EmailMessage) class.\n| This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nSending is asynchronous: the `mail.send_mail()` function and the\n`EmailMessage.send()` method transmit the message data to the mail\nservice, then return. The mail service queues the message, then attempts to\nsend it, retrying if the destination mail server is unavailable. Errors and\nbounce messages are sent to the sender address for the email message.\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 with `mail.send_mail()`\n------------------------------------\n\nTo send mail using the [`mail.send_mail()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.mail#google.appengine.api.mail.send_mail) function, use the fields of the\nemail message as parameters, including the sender, the recipients, the subject\nand the body of the message. For example: \n\n mail.send_mail(sender=sender_address,\n to=\"Albert Johnson \u003cAlbert.Johnson@example.com\u003e\",\n subject=\"Your account has been approved\",\n body=\"\"\"Dear Albert:\n\n Your example.com account has been approved. You can now visit\n http://www.example.com/ and sign in using your Google Account to\n access new features.\n\n Please let us know if you have any questions.\n\n The example.com Team\n \"\"\")\n\nSending mail with `EmailMessage`\n--------------------------------\n\nTo send mail using objects with the\n[`EmailMessage`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.mail#google.appengine.api.mail.EmailMessage) class, pass the fields of the email message to\nthe EmailMessage constructor and use attributes of the instance to update\nmessage.\n\nThe [`EmailMessage.send()`](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/mail#EmailMessage) method sends the email message\nrepresented by the instance's attributes. An application can re-use an\n`EmailMessage` instance by modifying attributes and calling the `send()` method\nagain. \n\n message = mail.EmailMessage(\n sender=sender_address,\n subject=\"Your account has been approved\")\n\n message.to = \"Albert Johnson \u003cAlbert.Johnson@example.com\u003e\"\n message.body = \"\"\"Dear Albert:\n\n Your example.com account has been approved. You can now visit\n http://www.example.com/ and sign in using your Google Account to\n access new features.\n\n Please let us know if you have any questions.\n\n The example.com Team\n \"\"\"\n message.send()\n\nThe following example demonstrates sending a message to confirm an email\naddress: \n\n class UserSignupHandler(webapp2.RequestHandler):\n \"\"\"Serves the email address sign up form.\"\"\"\n\n def post(self):\n user_address = self.request.get('email_address')\n\n if not mail.is_email_valid(user_address):\n self.get() # Show the form again.\n else:\n confirmation_url = create_new_user_confirmation(user_address)\n sender_address = (\n 'Example.com Support \u003c{}@appspot.gserviceaccount.com\u003e'.format(\n app_identity.get_application_id()))\n subject = 'Confirm your registration'\n body = \"\"\"Thank you for creating an account!\n Please confirm your email address by clicking on the link below:\n\n {}\n \"\"\".format(confirmation_url)\n mail.send_mail(sender_address, user_address, subject, body)\n\nSending bulk mail\n-----------------\n\nSee the [Bulk mail guidelines](/appengine/docs/legacy/standard/python/mail/bulk-mail) for considerations around sending bulk\nemail."]]