{"id":13498116,"url":"https://github.com/kootenpv/yagmail","last_synced_at":"2025-05-14T02:05:38.354Z","repository":{"id":30586267,"uuid":"34141361","full_name":"kootenpv/yagmail","owner":"kootenpv","description":"Send email in Python conveniently for gmail using yagmail","archived":false,"fork":false,"pushed_at":"2022-09-28T20:43:53.000Z","size":309,"stargazers_count":2687,"open_issues_count":107,"forks_count":267,"subscribers_count":53,"default_branch":"master","last_synced_at":"2025-05-06T02:39:26.635Z","etag":null,"topics":["email","hacktoberfest","hacktoberfest2021","magic","mime-types","python","smtp","yagmail"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kootenpv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-17T21:39:24.000Z","updated_at":"2025-05-04T10:46:29.000Z","dependencies_parsed_at":"2022-07-08T07:39:56.189Z","dependency_job_id":null,"html_url":"https://github.com/kootenpv/yagmail","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kootenpv%2Fyagmail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kootenpv%2Fyagmail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kootenpv%2Fyagmail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kootenpv%2Fyagmail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kootenpv","download_url":"https://codeload.github.com/kootenpv/yagmail/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253505664,"owners_count":21918942,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["email","hacktoberfest","hacktoberfest2021","magic","mime-types","python","smtp","yagmail"],"created_at":"2024-07-31T20:00:51.328Z","updated_at":"2025-05-14T02:05:33.344Z","avatar_url":"https://github.com/kootenpv.png","language":"Python","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=Y7QCCEPGC6R5E"],"categories":["Email","Python","电子邮件","目录","Email [🔝](#readme)","Awesome Python"],"sub_categories":["Email"],"readme":"\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./docs/_static/icon.png\" width=\"48px\"/\u003e\n\u003c/p\u003e\n\n# yagmail -- Yet Another GMAIL/SMTP client\n\n[![Join the chat at https://gitter.im/kootenpv/yagmail](https://badges.gitter.im/kootenpv/yagmail.svg)](https://gitter.im/kootenpv/yagmail?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![PyPI](https://img.shields.io/pypi/v/yagmail.svg?style=flat-square)](https://pypi.python.org/pypi/yagmail/)\n[![PyPI](https://img.shields.io/pypi/pyversions/yagmail.svg?style=flat-square)](https://pypi.python.org/pypi/yagmail/)\n\n*For the asynchronous asyncio version, look here*: https://github.com/kootenpv/aioyagmail\n\nThe goal here is to make it as simple and painless as possible to send emails.\n\nIn the end, your code will look something like this:\n\n```python\nimport yagmail\nyag = yagmail.SMTP('mygmailusername', 'mygmailpassword')\ncontents = ['This is the body, and here is just text http://somedomain/image.png',\n            'You can find an audio file attached.', '/local/path/song.mp3']\nyag.send('to@someone.com', 'subject', contents)\n```\n\nIn 2020, I personally prefer: using an [Application-Specific Password](https://support.google.com/accounts/answer/185833)\n\n### Table of Contents\n\n|Section|Explanation|\n|---------------------------------------------------------------|---------------------------------------------------------------------|\n|[Install](#install)                                            |   Find the instructions on how to install yagmail here              |\n|[Start a connection](#start-a-connection)                      |   Get started                                                       |\n|[Usability](#usability)                                        |   Shows some usage patterns for sending                             |\n|[Recipients](#recipients)                                      |   How to send to multiple people, give an alias or send to self     |\n|[Magical contents](#magical-contents)                          |   Really easy to send text, html, images and attachments            |\n|[Attaching files](#attaching-files)                            |   How attach files to the email                                     |\n|[DKIM Support](#dkim-support)                                  |   Add DKIM signature to your emails with your private key           |\n|[Feedback](#feedback)                                          |   How to send me feedback                                           |\n|[Roadmap (and priorities)](#roadmap-and-priorities)            |   Yup                                                               |\n|[Errors](#errors)                                              |   List of common errors for people dealing with sending emails      |\n\n\n### Install\n\nFor Python 2.x and Python 3.x respectively:\n\n```python\npip install yagmail[all]\npip3 install yagmail[all]\n\n```\n\nAs a side note, `yagmail` can now also be used to send emails from the command line.\n\n### Start a connection\n\n```python\nyag = yagmail.SMTP('mygmailusername', 'mygmailpassword')\n```\n\nNote that this connection is reusable, closable and when it leaves scope it will **clean up after itself in CPython**.\n\nAs [tilgovi](https://github.com/tilgovi) points out in [#39](https://github.com/kootenpv/yagmail/issues/39), SMTP does not automatically close in **PyPy**. The context manager `with` should be used in that case.\n\n\n### Usability\n\nDefining some variables:\n\n```python\nto = 'santa@someone.com'\nto2 = 'easterbunny@someone.com'\nto3 = 'sky@pip-package.com'\nsubject = 'This is obviously the subject'\nbody = 'This is obviously the body'\nhtml = '\u003ca href=\"https://pypi.python.org/pypi/sky/\"\u003eClick me!\u003c/a\u003e'\nimg = '/local/file/bunny.png'\n```\n\nAll variables are optional, and know that not even `to` is required (you'll send an email to yourself):\n\n```python\nyag.send(to = to, subject = subject, contents = body)\nyag.send(to = to, subject = subject, contents = [body, html, img])\nyag.send(contents = [body, img])\n```\n\nFurthermore, if you do not want to be explicit, you can do the following:\n\n```python\nyag.send(to, subject, [body, img])\n```\n\n### Recipients\n\nIt is also possible to send to a group of people by providing a list of email strings rather than a single string:\n\n```python\nyag.send(to = to)\nyag.send(to = [to, to2]) # List or tuples for emailadresses *without* aliases\nyag.send(to = {to : 'Alias1'}) # Dictionary for emailaddress *with* aliases\nyag.send(to = {to : 'Alias1', to2 : 'Alias2'}\n```\n\nGiving no `to` argument will send an email to yourself. In that sense, `yagmail.SMTP().send()` can already send an email.\nBe aware that if no explicit `to = ...` is used, the first argument will be used to send to. Can be avoided like:\n\n```python\nyag.send(subject = 'to self', contents = 'hi!')\n```\n\nNote that by default all email addresses are conservatively validated using `soft_email_validation==True` (default).\n\n### Oauth2\n\nIt is even safer to use Oauth2 for authentication, as you can revoke the rights of tokens.\n\n[This](http://blog.macuyiko.com/post/2016/how-to-send-html-mails-with-oauth2-and-gmail-in-python.html) is one of the best sources, upon which the oauth2 code is heavily based.\n\nThe code:\n\n```python\nyag = yagmail.SMTP(\"user@gmail.com\", oauth2_file=\"~/oauth2_creds.json\")\nyag.send(subject=\"Great!\")\n```\n\nIt will prompt for a `google_client_id` and a `google_client_secret`, when the file cannot be found. These variables can be obtained following [the previous link](http://blog.macuyiko.com/post/2016/how-to-send-html-mails-with-oauth2-and-gmail-in-python.html).\n\nAfter you provide them, a link will be shown in the terminal that you should followed to obtain a `google_refresh_token`. Paste this again, and you're set up!\n\nNote that people who obtain the file can send emails, but nothing else. As soon as you notice, you can simply disable the token.\n\n#### Preventing OAuth authorization from expiring after 7 days\n\nYour Google Cloud Platform project's OAuth consent screen must be in **\"In production\" publishing status** before authorizing to not have the authorization expire after 7 days. See status at https://console.cloud.google.com/apis/credentials/consent\n\nYour OAuth **client ID must be of type \"Desktop\"**. Check at https://console.cloud.google.com/apis/credentials\n\n### Magical `contents`\n\nThe `contents` argument will be smartly guessed. It can be passed a string (which will be turned into a list); or a list. For each object in the list:\n\n- If it is a dictionary it will assume the key is the content, and the value is an alias (only for images currently!)\n  e.g. {'/path/to/image.png' : 'MyPicture'}\n- It will try to see if the content (string) can be read as a file locally,\n  e.g. '/path/to/image.png'\n- if impossible, it will check if the string is valid html\n  e.g. `\u003ch1\u003eThis is a big title\u003c/h1\u003e`\n- if not, it must be text.\n  e.g. 'Hi Dorika!'\n\nNote that local files can be html (inline); everything else will be attached.\n\nLocal files require to have an extension for their content type to be inferred.\n\nAs of version 0.4.94, `raw` and `inline` have been added.\n\n- `raw` ensures a string will not receive any \"magic\" (inlining, html, attaching)\n- `inline` will make an image appear in the text.\n\n### Attaching Files\nThere are multiple ways to attach files in the `attachments` parameter (in addition to magical `contents` parameter).\n1. One can pass a list of paths i.e.\n```python\nyag.send(to=recipients,\n         subject=email_subject,\n         contents=contents,\n         attachments=['path/to/attachment1.png', 'path/to/attachment2.pdf', 'path/to/attachment3.zip']\n)\n```\n2. One can pass an instance of [`io.IOBase`](https://docs.python.org/3/library/io.html#io.IOBase).\n```python\nwith open('path/to/attachment', 'rb') as f:\n    yag.send(to=recipients,\n             subject=email_subject,\n             contents=contents,\n             attachments=f\n             )\n```\nIn this example `f` is an instance of `_io.BufferedReader` a subclass of the abstract class `io.IOBase`.\n\n`f` has in this example the attribute `.name`, which is used by yagmail as filename as well as to detect the correct MIME-type.\nNot all `io.IOBase` instances have the `.name` attribute in which case yagmail names the attachments `attachment1`, `attachment2`, ... without a file extension!\nTherefore, it is highly recommended setting the filename with extension manually e.g. `f.name = 'my_document.pdf'`\n\nA real-world example would be if the attachment is retrieved from a different source than the disk (e.g. downloaded from the internet or [uploaded by a user in a web-application](https://docs.streamlit.io/en/stable/api.html#streamlit.file_uploader))\n\n### DKIM Support\n\nTo send emails with dkim signature, you need to install the package with all related packages.\n```\npip install yagmail[all]\n# or\npip install yagmail[dkim]\n```\n\nUsage:\n```python\nfrom yagmail import SMTP\nfrom yagmail.dkim import DKIM\nfrom pathlib import Path\n\n# load private key from file/secrets manager\nprivate_key = Path(\"privkey.pem\").read_bytes()\n\ndkim_obj = DKIM(\n  domain=b\"a.com\",\n  selector=b\"selector\",\n  private_key=private_key,\n  include_headers=[b\"To\", b\"From\", b\"Subject\"],\n  # To include all default headers just pass None instead\n  # include_headers=None,\n)\n\nyag = SMTP(dkim=dkim_obj)\n\n# all the rest is the same\n```\n### Feedback\n\nI'll try to respond to issues within 24 hours at Github.....\n\nAnd please send me a line of feedback with `SMTP().feedback('Great job!')` :-)\n\n### Roadmap (and priorities)\n\n- ~~Added possibility of Image~~\n- ~~Optional SMTP arguments should go with \\**kwargs to my SMTP~~\n- ~~CC/BCC (high)~~\n- ~~Custom names (high)~~\n- ~~Allow send to return a preview rather than to actually send~~\n- ~~Just use attachments in \"contents\", being smart guessed (high, complex)~~\n- ~~Attachments (contents) in a list so they actually define the order (medium)~~\n- ~~Use lxml to see if it can parse the html (low)~~\n- ~~Added tests (high)~~\n- ~~Allow caching of content (low)~~\n- ~~Extra other types (low)~~ (for example, mp3 also works, let me know if something does not work)\n- ~~Probably a naming issue with content type/default type~~\n- ~~Choose inline or not somehow (high)~~\n- ~~Make lxml module optional magic (high)~~\n- ~~Provide automatic fallback for complex content(medium)~~ (should work)\n- ~~`yagmail` as a command on CLI upon install~~\n- ~~Added `feedback` function on SMTP to be able to send me feedback directly :-)~~\n- ~~Added the option to validate emailaddresses...~~\n- ~~however, I'm unhappy with the error handling/logging of wrong emails~~\n- ~~Logging count \u0026 mail capability (very low)~~\n- ~~Add documentation to exception classes (low)~~\n- ~~add `raw` and `inline`~~\n- ~~oauth2~~\n- ~~Travis CI integration ~~\n- ~~ Add documentation to all functions (high, halfway) ~~\n- Prepare for official 1.0\n- Go over documentation again (medium)\n- Allow `.yagmail` file to contain more parameters (medium)\n- Add option to shrink images (low)\n\n### Errors\n\n- [`smtplib.SMTPException: SMTP AUTH extension not supported by server`](http://stackoverflow.com/questions/10147455/trying-to-send-email-gmail-as-mail-provider-using-python)\n\n- [`SMTPAuthenticationError: Application-specific password required`](https://support.google.com/accounts/answer/185833)\n\n- **YagAddressError**: This means that the address was given in an invalid format. Note that `From` can either be a string, or a dictionary where the key is an `email`, and the value is an `alias` {'sample@gmail.com': 'Sam'}. In the case of 'to', it can either be a string (`email`), a list of emails (email addresses without aliases) or a dictionary where keys are the email addresses and the values indicate the aliases.\n\n- **YagInvalidEmailAddress**: Note that this will only filter out syntax mistakes in emailaddresses. If a human would think it is probably a valid email, it will most likely pass. However, it could still very well be that the actual emailaddress has simply not be claimed by anyone (so then this function fails to devalidate).\n\n- Click to enable the email for being used externally https://www.google.com/settings/security/lesssecureapps\n\n- Make sure you have a working internet connection\n\n- If you get an `ImportError` try to install with `sudo`, see issue #13\n\n### Donate\n\nIf you like `yagmail`, feel free (no pun intended) to donate any amount you'd like :-)\n\n[![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=Y7QCCEPGC6R5E)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkootenpv%2Fyagmail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkootenpv%2Fyagmail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkootenpv%2Fyagmail/lists"}