yagmail: a Python SMTP client that guesses what your attachments are
Send email in Python conveniently for gmail using yagmail
At a glance
- What is it?
- yagmail wraps smtplib and MIMEText so that sending a Gmail message with inline images and attachments takes three lines. It is convenient for scripts and cron jobs, less so if you need a general-purpose mail library or long-lived unattended OAuth.
- Who is it for?
- Adopt yagmail if you are writing Python scripts that send Gmail messages with attachments and you want the MIME assembly handled for you; the MIT licence and the pip install yagmail[all] path keep that cheap. Do not adopt it if you need a general-purpose MIME library, a mail server that is not SMTP, or unattended OAuth without publishing a Google Cloud consent screen.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 35 days ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.
Star history
- kootenpv/yagmail2,739 ★ · +4
| Repository | 2026-09-30 | 2026-11-24 |
|---|---|---|
| kootenpv/yagmail | 2.7k | 2.7k |
4 Stars in the last 55 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The problem yagmail removes: MIME assembly in Python
Sending mail from Python with the standard library means constructing a MIMEText or MIMEMultipart object, choosing a subtype, attaching parts, encoding file bytes, and then handing the result to smtplib. Every one of those steps is boilerplate that has nothing to do with the message you actually want to deliver. yagmail's stated goal is to make that as simple and painless as possible, and the README's opening example is three lines: construct a client, build a list of contents, call send.
The audience is narrow and practical. It is developers writing notification scripts, report senders, cron jobs and small internal tools where the recipient list is known and the mail provider is Gmail or another SMTP host. It is not aimed at people building a mail server, parsing inbound mail, or composing messages that need precise control over every header and MIME part. The package description in pyproject.toml calls it "Yet Another GMAIL client", and the classifiers list System Administrators and Customer Service alongside Developers, which matches the automation use case rather than application mail infrastructure.
How yagmail decides what a string in contents means
The mechanism that distinguishes yagmail from a thin smtplib wrapper is the contents list. You pass strings, and the library inspects each one. The README's example passes a body string containing a URL, a second string describing an audio file, and a local path. The URL is fetched and embedded as an inline image, the local path is attached as a file, and the plain text becomes the body. The README calls this section "Magical contents", and the magic is exactly this inference step: a value that looks like an existing local file becomes an attachment, a value that looks like a URL becomes inline content, and everything else is text or HTML.
Underneath, the package depends on premailer, which is listed in pyproject.toml as the single required dependency. Premailer's job is to inline CSS into HTML, which matters because the README's usability example passes an HTML string containing an anchor tag alongside a plain body. So the data flow is: Python strings in, classification and fetching, HTML preparation through premailer, MIME construction, then SMTP delivery through the client object. The optional dependencies keyring and dkimpy are not installed by default; they are pulled in by the all extra, and dkimpy alone by the dkim extra.
That inference is the trade-off. It is fast to write and it means a typo in a file path silently becomes body text rather than raising, unless you are watching the output. There is no explicit attachment argument in the README's examples to fall back on.
Installing yagmail and sending a first message
The README gives a single install command. The all extra pulls in keyring and dkimpy alongside premailer, which is what you want if you plan to use the keyring password storage or DKIM signing described later in the README.
pip install yagmail[all]After that, the README's preferred entry point is yagmail.Client. The legacy name yagmail.SMTP is kept as a backward-compatible alias, so older code you find in the wild still works. The README is explicit that for Gmail you must use an Application-Specific Password, because regular account passwords no longer work over SMTP.
yag = yagmail.Client('mygmailusername', 'mygmailpassword')From there, send takes a recipient, a subject and a contents list. The README's first full example mixes a body string containing a URL, a descriptive string, and a local file path, and expects the URL to be embedded and the path attached.
import yagmail
yag = yagmail.Client('mygmailusername', 'mygmailpassword')
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3']
yag.send('[email protected]', 'subject', contents)What you should see is a delivered message with the image inline and song.mp3 attached. If the path is wrong, expect the string to appear as text instead. The README also notes that yagmail can be used from the command line, though it does not show the invocation.
The README documents two ways to authenticate beyond a plain password. For OAuth2 you point the client at a credentials file, and if the file is missing it prompts for a google_client_id and a google_client_secret in the terminal, then shows a link to obtain a google_refresh_token.
yag = yagmail.Client("[email protected]", oauth2_file="~/oauth2_creds.json")
yag.send(subject="Great!")Note that no to argument appears in that example. The README states that sending with no recipient sends the email to yourself, so yagmail.Client().send() is already a working call.
Recipients, aliases and the argument order trap
Recipients accept three shapes. A single string for one address, a list or tuple for several addresses without aliases, and a dictionary mapping address to alias when you want display names. The README shows to = {to : 'Alias1', to2 : 'Alias2'} for the dictionary form.
The trap is positional. If you do not pass an explicit to = keyword, the first positional argument is treated as the recipient. The README demonstrates the workaround of passing subject and contents as keywords when you want to mail yourself. This is a small design decision with a real cost: yag.send('to self', 'hi!') reads as though the first string is the subject, and it is not. In code that builds arguments dynamically, that ambiguity is worth guarding against by always using keywords.
The README also notes that addresses are validated by default using soft_email_validation==True, described as conservative. It does not document what happens when validation fails, or how to turn the check off, so treat that default as a constraint you inherit rather than a knob you can tune from the README alone.
The async client, DKIM, and what the README does not cover
An asyncio version is built into the package. The README states this in its opening paragraph and gives the asynchronous client its own table-of-contents entry, but the available text does not include the async example body, so the call signature and the concurrency model are not something this article can describe. If you need that, read the section in the repository README directly. The same applies to the DKIM support section, which the table of contents lists and which the dkim extra exists to support, but whose configuration the available text does not show.
The README does document its errors, with a dedicated section listing common errors people hit when sending mail. It also has a roadmap section, which is a sign the author treats the project as ongoing rather than finished. The last push to the default branch was on 2026-05-26, so the repository is not archived and has been touched within the last six months.
What is thin is the operational side. There is no documented rollback or retry behaviour, no description of what the client does when the SMTP server rejects a recipient mid-send, and no guidance on rate limits. For a library whose main selling point is convenience in automation, the absence of documented failure handling is the gap to plan around.
Where yagmail is the wrong tool, and what to use instead
The clearest failure mode is PyPy. The README quotes issue #39 pointing out that SMTP/Client connections do not automatically close in PyPy, and says the with context manager should be used in that case. On CPython the README states the connection cleans up after itself when it leaves scope. So the convenience of not managing the connection is CPython-specific, and code that relies on it will leak connections on PyPy.
The second failure mode is OAuth expiry. The README warns that tokens expire after 7 days unless your Google Cloud project's OAuth consent screen is in "In production" publishing status, and that your client ID must be of type "Desktop". For personal use the README says hitting Publish App is enough, and you will see an unverified-app warning. That is a real constraint on unattended jobs: an OAuth setup left in testing status will break weekly, and the fix lives in the Google Cloud console, not in your Python code.
The natural alternative is smtplib plus email.mime from the standard library, which is also what the related searches compare yagmail against. The difference in approach is that smtplib gives you the transport and nothing else. You construct MIMEText and MIMEMultipart objects yourself, decide the subtype, attach file bytes, and set headers. You get exact control over the message and no dependency on premailer, but you write the MIME assembly that yagmail exists to hide. For a one-off script that sends a plain text line, smtplib is a handful of lines and no extra package. For a script that embeds an image and attaches a PDF, yagmail is doing work you would otherwise repeat. The choice is about how much MIME structure your messages actually have.
Licence and the cost of keeping yagmail current
yagmail is MIT licensed, stated both in the repository and in pyproject.toml as license = {text = "MIT"}. MIT is permissive, so embedding it in a closed-source internal tool is not a licensing obstacle. This is not legal advice; check your own obligations if you redistribute it.
The upgrade surface is small. The package requires Python 3.8 or newer, and the classifiers in pyproject.toml list 3.8 through 3.13, so the supported interpreter range is documented rather than implied. There is one required runtime dependency, premailer, plus two optional ones behind the all and dkim extras. That means the main upgrade risk is not yagmail's own API, which keeps yagmail.SMTP as a backward-compatible alias for yagmail.Client, but premailer's behaviour when it inlines CSS into your HTML. The repository carries tox.ini, a tests directory and a conftest.py, so there is a test suite to run against a new interpreter before you move.
Because the last push was on 2026-05-26, you should not assume a rapid release cadence. Pin the version you install and treat an upgrade as a deliberate act with the test suite as the check.
Editorial conclusion
Adopt yagmail if you are writing Python scripts that send Gmail messages with attachments and you want the MIME assembly handled for you; the MIT licence and the pip install yagmail[all] path keep that cheap. Do not adopt it if you need a general-purpose MIME library, a mail server that is not SMTP, or unattended OAuth without publishing a Google Cloud consent screen. Before committing, verify three things: that your Google account still accepts an Application-Specific Password, that your OAuth client ID is of type Desktop and the consent screen is in production status, and whether you run on PyPy, where the README says the connection does not close on its own.
Frequently asked questions
How do I install yagmail?
The README gives one command, pip install yagmail[all], which installs premailer plus the optional keyring and dkimpy packages. The package requires Python 3.8 or newer.
How do I use yagmail to send an email?
Instantiate yagmail.Client with your username and password, then call send with a recipient, a subject and a contents list. Strings in the contents list are classified: a local file path becomes an attachment, a URL is embedded, and the rest becomes body text.
Is yagmail safe to use?
The README treats OAuth2 as the safer option because tokens can be revoked, and notes that anyone who obtains the credentials file can send emails but nothing else. It also states that regular Gmail account passwords no longer work over SMTP, so you need an Application-Specific Password or OAuth2.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/kootenpv-yagmail)Star history chart for your README
[](https://hysenlabs.com/projects/kootenpv-yagmail)Was this useful?