yagmail

repository·master·Indexed 25 days ago

https://github.com/kootenpv/yagmail

A Python library designed to simplify sending emails via Gmail or other SMTP servers. It provides synchronous and asynchronous clients (yagmail.Client and yagmail.AsyncClient), supports OAuth2 and system keyring authentication, and includes features for DKIM signing, automatic content type detection (HTML, text, and attachments), and flexible recipient management.

Tokens
7.6K
Snippets
18
Records
72
Agent score
82%

What's inside yagmail

  1. Manage yagmail.Client connections

    master

    In CPython, yagmail.Client cleans up after itself automatically. However, for other implementations like PyPy, or to ensure reliable cleanup, use the client as an asynchronous context manager with the with statement.

    You can also manually manage the connection lifecycle using .close() and .login() methods.

    # Using a context manager (recommended for PyPy or explicit cleanup)
    with yagmail.Client('mygmailusername', 'mygmailpassword') as yag:
        yag.send(to='you@gmail.com', contents="Hello!")
    
    # Manual management
    yag = yagmail.Client('mygmailusername', 'mygmailpassword')
    yag.send(to="you@gmail.com", contents="Hello!")
  2. Initialize yagmail.Client with credentials

    master
    By default, yagmail.Client() attempts to read your password securely from your system keyring. If you prefer to provide credentials explicitly, you can pass the username and password directly to the constructor. However, using the keyring is the recommended secure method.
  3. Authenticate with yagmail using passwords or keyring

    master
    You can authenticate by passing a password directly to yagmail.Client or yagmail.AsyncClient. If you do not provide a password, yagmail will prompt you for one in the terminal and then store it in your system's keyring for future use.
  4. Authenticate using OAuth2

    master

    For a more secure authentication method that allows for easy token revocation, use OAuth2 by providing the path to a credentials file via the oauth2_file parameter in yagmail.Client.

    Setup Steps:

    1. Pass the path to your JSON credentials file to oauth2_file.
    2. If the file is not found, the library will prompt for your google_client_id and google_client_secret.
    3. Follow the terminal link to obtain a google_refresh_token and paste it back into the terminal.

    Google Cloud Platform (GCP) Configuration:

    To prevent OAuth tokens from expiring every 7 days, ensure your GCP project settings are configured as follows:

    • Set the publishing status to "In production" (Publish App).
    • Set the OAuth Client ID type to "Desktop".
    import yagmail
    
    yag = yagmail.Client('user@gmail.com', oauth2_file='~/oauth2_creds.json')
    yag.send(subject="Great!")
  5. Initialize a yagmail.Client connection

    master

    To start a connection, instantiate yagmail.Client. You can authenticate using an App Password or OAuth2.

    Using an App Password (Gmail): Pass your username and a Google Application-Specific Password. Note that your regular Google account password will not work.

    Using OAuth2: Pass your email and the path to an OAuth2 credentials file using the oauth2_file parameter.

  6. Omit username using a .yagmail file

    master
    To instantiate yagmail.Client() without passing any arguments, create a file named .yagmail in your home folder containing only your username. If this file is missing and no username is provided to the constructor, a ValueError will be raised.
  7. Install and use yagmail to send emails

    master

    To send an email using yagmail, initialize a yagmail.Client and use the .send() method. The .send() method accepts a recipient address, a subject string, and a contents list. The contents list can contain strings (for body text or URLs to images) and local file paths (for attachments).

    import yagmail
    yag = yagmail.Client()
    contents = [
        "This is the body, and here is just text http://somedomain/image.png",
        "You can find an audio file attached.", '/local/path/to/song.mp3'
    ]
    yag.send('to@someone.com', 'subject', contents)
  8. Configure credentials using the system keyring

    master

    Instead of hardcoding passwords in your scripts, you can register your credentials with yagmail using the system keyring. This allows you to instantiate the client with only a username.

    Important for Gmail users: You must use an Application-Specific Password rather than your regular Google account password.

    import yagmail
    
    # Register your credentials once
    yagmail.register('mygmailusername', 'mygmailpassword')
    
    # Later, instantiate the client using only the username
    yag = yagmail.Client('mygmailusername')
  9. Install yagmail via PyPI

    master
    Install yagmail using pip. It is recommended to install the [all] extra to include the keyring library, which allows for secure credential storage via your system keyring service. If keyring causes issues, you can install without it by omitting [all].