We’ll define these in a list, because we’ll need the list later: recipients = MIMEText(email_text) This can be our own address, and if the email is going to more than one person, we need to separate the addresses by commas. From will be set to our Gmail address, which we’ve already defined, and To should be a string containing the email address the email is being sent to. An email can have many different properties, but the important ones here (in addition to the text of the body) are To, From, and Subject. Next, we’ll create the message as an object using the email module. We’ll also define two variables to hold our Gmail account details: the account name (which is the part of our Gmail address before and the app password we just created: GMAIL_USERNAME = "mygmailaccount12345" We have added 1 + 2 and gotten the answer. Here, we’ll use a simple example: email_text = f""" It’s a Python string, so we can substitute values in, use a templating language, or build it up from a list whatever’s convenient. First, we need to define the content of our email. To send an email, we’ll use the smtplib module. We’ll be shown a screen that lists our new app password. Go to the App passwords of your Google account, and under Select app, choose Mail, and under Select device choose Other (custom name) and fill in a name (such as “My Python Script”). To use our Gmail account to send email this way, we first have to set up an app password for our Python script to use. But as an email notification after a scheduled task, using Gmail from Python can be the ideal personal solution. If we want to send tens or hundreds of emails to many different recipients, it’s best to investigate the programmatic email services mentioned above. We can’t send too many emails this way, though. We can even use them to send email from ourself to ourself. Python comes with the smtplib and email modules as part of the built-in library, and these are perfectly capable of sending email via Gmail after a little setup. There’s an official Google Gmail Python API and module, but it’s quite annoying to set up and use. But for something small or personal, this can seem like a lot of effort, and there’s an alternative if we have a Gmail account (as many people do). They have official APIs and paid plans, and if we’re setting up a large-scale operation, it’s certainly worth looking into signing up to one of these services and using their Python libraries to send email. These are useful if we’re generating a lot of email. There are many programmatic email services, such as SendGrid, Mandrill, and Mailgun. Something that regularly downloads data from an HTTP API and then processes it can email us a description of what it found, so that we can go and read its results.įor details on setting up scheduled tasks, see the Windows, macOS, or Linux cron documentation. For example, if we build a script that’s run as a scheduled task on a regular basis, having it email a summary report to us after it’s run can be a good way to check that the script did what it was supposed to, and also to see what it found. When writing scripts for our own use, it’s useful to be able to send emails from them. This can be useful for sending status reports and summaries of activities, for example. In this quick tip, excerpted from Useful Python, Stuart shows you how you can use Python to send emails via Gmail.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |