yagmail Documentation

Similar documents
pyoutlook Documentation

Flask-Sendmail Documentation

django-oauth2-provider Documentation

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Django QR Code Documentation

pushka Documentation Release Alexey Kinëv

Introduction & Basics! Technical Foundation! Authentication! Obtaining a token!... 4 Using the token! Working with notes!...

Python easy mail library Documentation

Django PAM Documentation

htmlmin Documentation

petfinder-api Documentation

f5-icontrol-rest Documentation

PyZabbixObj Documentation

pybtsync Documentation

Webmail Documentation

databuild Documentation

06ESFContacts 1 message

GitHub-Flask Documentation

redis-lua Documentation

Wifiphisher Documentation

streamio Documentation

python-aspectlib Release 0.4.1

pyftpsync Documentation

Flask Web Development Course Catalog

flask-jwt-simple Documentation

DCLI User's Guide. Data Center Command-Line Interface 2.9.1

(electronic mail) is the exchange of computer-stored messages by telecommunication.

How to Secure SSH with Google Two-Factor Authentication

Use Mail Merge with Gmail to send personalized messages to a large group of people from your Gmail account.

solrq Documentation Release Michał Jaworski

Configuring JANOS for

bottle-rest Release 0.5.0

pyshk Documentation Release Jeremy Low

SharePointBoost Password Change Tutorial for SharePoint 2007

python-aspectlib Release 0.5.0

Intro. Scheme Basics. scm> 5 5. scm>

django-openid Documentation

DCLI User's Guide. Modified on 20 SEP 2018 Data Center Command-Line Interface

DCLI User's Guide. Data Center Command-Line Interface

moth Documentation Release Charles Thomas

keepassx Release 0.1.0

Django with Python Course Catalog

ejpiaj Documentation Release Marek Wywiał

Scheduling Java Objects Using the SDK. Matthias Nott, Business Objects

E-Postal Services 1. The E-Postal Service is used to targeted groups at NMU. It is NOT used to all students or employee groups.

Seshat Documentation. Release Joshua P Ashby

User Guide Online Backup

LECTURE 2. Python Basics

Gearthonic Documentation

nacelle Documentation

kvkit Documentation Release 0.1 Shuhao Wu

Python STL. Release dev

Emptying the trash...18 Webmail Settings...19 Displayed Name...19 Sort by...19 Default font style...19 Service language...

semidbm Documentation

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

ipython-gremlin Documentation

Django Test Utils Documentation

Gmail Client Documentation

icontact for Salesforce Installation Guide

bzz Documentation Release Rafael Floriano and Bernardo Heynemann

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Perceptive Content Agent

Bitnami OSQA for Huawei Enterprise Cloud

Accessing WebMail. Logging In. Check your Mail. Main Mail Screen. Reading Messages. Search. Sending a new message

certbot-dns-route53 Documentation

Torndb Release 0.3 Aug 30, 2017

Install & Configure Thunderbird E- mail

The CHECKBOX Quick Start Guide

I hate money. Release 1.0

Getting Started with Universal

eventbrite-sdk-python Documentation

jumpssh Documentation

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Mail Merge for Gmail v2.0

django-ratelimit-backend Documentation

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

spacetrack Documentation

Advanced CGI Scripts. personalized web browsing using cookies: count number of visits. secure hash algorithm using cookies for login data the scripts

supernova Documentation

Spam Manager User Guide MailStreet Boundary Defense for Anti-Spam End User Guide

python-anyvcs Documentation

Introduction to

GMusicProcurator Documentation

Queries Documentation

Django-CSP Documentation

EPHP a tool for learning the basics of PHP development. Nick Whitelegg School of Media Arts and Technology Southampton Solent University

django-mama-cas Documentation

dota2api Documentation

Lecture 02, Fall 2018 Friday September 7

Unifer Documentation. Release V1.0. Matthew S

OpenProject AdminGuide

0. Introduction On-demand. Manual Backups Full Backup Custom Backup Store Your Data Only Exclude Folders.

Flask-Cors Documentation

NIELSEN API PORTAL USER REGISTRATION GUIDE

BIDMC Multi-Factor Authentication Enrollment Guide Table of Contents

TPS Documentation. Release Thomas Roten

tld Documentation Release 0.9 Artur Barseghyan

Plumeria Documentation

argcomplete Documentation Andrey Kislyuk

Pulp Python Support Documentation

Transcription:

yagmail Documentation Release 0.10.189 kootenpv Feb 08, 2018

Contents 1 API Reference 3 1.1 Authentication.............................................. 3 1.2 SMTP Client............................................... 3 1.3 E-Mail Contents............................................. 4 1.4 Exceptions................................................ 4 1.5 Utilities.................................................. 4 2 Setup 7 2.1 Installing from PyPI........................................... 7 2.2 Installing from GitHub.......................................... 7 2.3 Configuring Credentials......................................... 7 2.4 Using OAuth2.............................................. 8 3 Usage 9 3.1 Start a Connection............................................ 9 3.2 Closing and reusing the Connection................................... 9 3.3 Sending E-Mails............................................. 10 3.4 E-Mail recipients............................................. 10 3.5 Magical contents........................................... 10 3.6 Using yagmail from the command line................................. 11 4 Indices and tables 13 Python Module Index 15 i

ii

yagmail is a GMAIL/SMTP client that aims to make it as simple as possible to send emails. Contents 1

2 Contents

CHAPTER 1 API Reference This page displays a full reference of yagmail s API. 1.1 Authentication yagmail.register(username, password) Use this to add a new gmail account to your OS keyring so it can be used in yagmail Another way of authenticating is by passing an oauth2_file to yagmail.smtp, which is among the safest methods of authentication. Please see the OAuth2 section of the README for further details. It is also possible to simply pass the password to yagmail.smtp. If no password is given, yagmail will prompt the user for a password and then store the result in the keyring. 1.2 SMTP Client class yagmail.smtp(user=none, password=none, host= smtp.gmail.com, port=none, smtp_starttls=none, smtp_ssl=true, smtp_set_debuglevel=0, smtp_skip_login=false, encoding= utf-8, oauth2_file=none, soft_email_validation=true, **kwargs) yagmail.smtp is a magic wrapper around smtplib s SMTP connection, and allows messages to be sent. close() Close the connection to the SMTP server feedback(message= Awesome features! You made my day! How can I contribute? ) Most important function. Please send me feedback :-) login(password) Login to the SMTP server using password. login only needs to be manually run when the connection to the SMTP server was closed by the user. 3

send(to=none, subject=none, contents=none, attachments=none, cc=none, bcc=none, preview_only=false, headers=none) Use this to send an email with gmail send_unsent() Emails that were not being able to send will be stored in self.unsent. Use this function to attempt to send these again set_logging(log_level=40, file_path_name=none) This function allows to change the logging backend, either output or file as backend It also allows to set the logging level (whether to display only critical/error/info/debug. for example: yag = yagmail.smtp() yag.set_logging(yagmail.logging.debug) # to see everything and: yagmail.set_logging(yagmail.logging.debug, 'somelocalfile.log') lastly, a log_level of None will make sure there is no I/O. 1.3 E-Mail Contents class yagmail.raw Ensure that a string is treated as text and will not receive magic. class yagmail.inline Only needed when wanting to inline an image rather than attach it 1.4 Exceptions Contains the exceptions exception yagmail.error.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. Furthermore, it does not do any validation of whether an email exists. exception yagmail.error.yagconnectionclosed The connection object has been closed by the user. This object can be used to send emails again after logging in, using self.login(). exception yagmail.error.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). 1.5 Utilities yagmail.validate.validate_email_with_regex(email_address) Note that this will only filter out syntax mistakes in emailaddresses. If a human would think it is probably a 4 Chapter 1. API Reference

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). 1.5. Utilities 5

6 Chapter 1. API Reference

CHAPTER 2 Setup This page shows you how to install yagmail and how to set it up to use your system keyring service. 2.1 Installing from PyPI The usual way of installing yagmail is through PyPI. It is recommended to install it together with the keyring library, by running the following (for Python 2.x and 3.x respectively): pip install yagmail[all] pip3 install yagmail[all] If installing yagmail with keyring causes issues, omit the [all] to install it without. 2.2 Installing from GitHub If you re not scared of things occasionally breaking, you can also install directly from the GitHub repository. You can do this by running the following (for Python 2.x and 3.x respectively): pip install -e git+https://github.com/kootenpv/yagmail#egg=yagmail[all] pip3 install -e git+https://github.com/kootenpv/yagmail#egg=yagmail[all] Just like with the PyPI installation method, if installing with keyring causes issues, simply omit the [all] to install yagmail without it. 2.3 Configuring Credentials While it s possible to put the username and password for your E-Mail address into your script, yagmail enables you to omit both. Quoting from keyrings README: 7

What is Python keyring lib? The Python keyring lib provides a easy way to access the system keyring service from python. It can be used in any application that needs safe password storage. If this sparked your interest, set up a Python interpreter and run the following to register your GMail credentials with yagmail: import yagmail yagmail.register('mygmailusername', 'mygmailpassword') (this is just a wrapper for keyring.set_password('yagmail', 'mygmailusername', 'mygmailpassword')) Now, instantiating yagmail.smtp is as easy as doing: yag = yagmail.smtp('mygmailusername') If you want to also omit your username, you can create a.yagmail file in your home folder, containing just your username. Then, you can instantiate the SMTP client without passing any arguments. 2.4 Using OAuth2 Another fairly safe method for authenticating using OAuth2, since you can revoke the rights of tokens. In order to use OAuth2, pass the location of the credentials file to yagmail.smtp: yag = yagmail.smtp('user@gmail.com', oauth2_file='~/oauth2_creds.json') yag.send(subject="great!") If the file could not be found, then it will prompt for a google_client_id and google_client_secret. You can obtain these on this OAauth2 Guide, upon which the OAauth2 code of yagmail is heavily based. After you have provided these, a link will be shown in the terminal that you should follow to obtain a google_refresh_token. Paste this again, and you re set up! If somebody obtains the file, they can send E-Mails, but nothing else. As soon as you notice, you can simply disable the token. 8 Chapter 2. Setup

CHAPTER 3 Usage This document aims to show how to use yagmail in your programs. Most of what is shown here is also available to see in the README, some content may be duplicated for completeness. 3.1 Start a Connection As mentioned in Configuring Credentials, there are three ways to initialize a connection by instantiating yagmail. SMTP: 1. With Username and Password: e.g. yagmail.smtp('mygmailusername', 'mygmailpassword') This method is not recommended, since you would be storing the full credentials to your account in your script in plain text. A better alternative is using keyring, as described in the following section: 2. With Username and keyring: After registering a keyring entry for yagmail, you can instantiate the client by simply passing your username, e.g. yagmail.smtp('mygmailusername'). 3. With keyring and.yagmail: As explained in the Setup documentation, you can also omit the username if you have a.yagmail file in your home folder, containing just your GMail username. This way, you can initialize yagmail. SMTP without any arguments. 4. With OAuth2: This is probably the safest method of authentication, as you can revoke the rights of tokens. To initialize with OAuth2 credentials (after obtaining these as shown in Setup), simply pass an oauth2_file to yagmail.smtp, for example yagmail.smtp('user@gmail.com', oauth2_file='~/oauth2_creds.json'). 3.2 Closing and reusing the Connection By default, yagmail.smtp will clean up after itself in CPython. This is an implementation detail of CPython and as such may not work in other implementations such as PyPy (reported in issue #39). In those cases, you can use yagmail.smtp with with instead. 9

Alternatively, you can close and re-use the connection with yagmail.smtp.close() and yagmail.smtp. login() (or yagmail.smtp.oauth2_file() if you are using OAuth2). 3.3 Sending E-Mails yagmail.smtp.send() is a fairly versatile method that allows you to adjust more or less anything about your Mail. First of all, all parameters for yagmail.smtp.send() are optional. If you omit the recipient (specified with to), you will send an E-Mail to yourself. Since the use of the (keyword) arguments of yagmail.smtp.send() are fairly obvious, they will simply be listed here: to subject contents attachments cc bcc preview_only headers Some of these - namely to and contents - have some magic associated with them which will be outlined in the following sections. 3.4 E-Mail recipients You can send an E-Mail to a single user by simply passing a string with either a GMail username (@gmail.com will be appended automatically), or with a full E-Mail address: yag.send(to='mike@gmail.com', contents="hello, Mike!") Alternatively, you can send E-Mails to a group of people by either passing a list or a tuple of E-Mail addresses as to: yag.send(to=['to@someone.com', 'for@someone.com'], contents="hello there!") These E-Mail addresses were passed without any aliases. If you wish to use aliases for the E-Mail addresses, provide a dictionary mapped in the form {address: alias}, for example: recipients = { 'aliased@mike.com': 'Mike', 'aliased@fred.com': 'Fred' } yag.send(to=recipients, contents="hello, Mike and Fred!") 3.5 Magical contents The contents argument of yagmail.smtp.send() will be smartly guessed. You can pass it a string with your contents or a list of elements which are either: 10 Chapter 3. Usage

If it is a dictionary, then it will be assumed that the key is the content and the value is an alias (currently, this only applies to images). For example: contents = [ "Hello Mike! Here is a picture I took last week:", {'path/to/my/image.png': 'PictureForMike'} ] If it is a string, then it will first check whether the content of the string can be read as a file locally, for example 'path/to/my/image.png'. These files require an extension for their content type to be inferred. If it could not be read locally, then it checks whether the string is valid HTML, such as <h1>this is a big title!</h1>. If it was not valid HTML either, then it must be text, such as "Hello, Mike!". If you want to ensure that a string is treated as text and should not be checked for any other content as described above, you can use yagmail.raw, a subclass of str. If you intend to inline an image instead of attaching it, you can use yagmail.inline. 3.6 Using yagmail from the command line yagmail includes a command-line application, simply called with yagmail after you installed it. To view a full reference on how to use this, run yagmail --help. 3.6. Using yagmail from the command line 11

12 Chapter 3. Usage

CHAPTER 4 Indices and tables genindex modindex search 13

14 Chapter 4. Indices and tables

Python Module Index y yagmail.error, 4 15

16 Python Module Index

Index C close() (yagmail.smtp method), 3 F feedback() (yagmail.smtp method), 3 I inline (class in yagmail), 4 L login() (yagmail.smtp method), 3 R raw (class in yagmail), 4 register() (in module yagmail), 3 S send() (yagmail.smtp method), 3 send_unsent() (yagmail.smtp method), 4 set_logging() (yagmail.smtp method), 4 SMTP (class in yagmail), 3 V validate_email_with_regex() (in module yagmail.validate), 4 Y YagAddressError, 4 YagConnectionClosed, 4 YagInvalidEmailAddress, 4 yagmail.error (module), 4 17