Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to create a draft #90

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ params = {
message = gmail.send_message(**params) # equivalent to send_message(to="[email protected]", sender=...)
```

### Create a draft:

```python
from simplegmail import Gmail

gmail = Gmail() # will open a browser window to ask you to log in and authenticate

params = {
"to": "[email protected]",
"sender": "[email protected]",
"subject": "My first email",
"msg_html": "<h1>Woah, my first email!</h1><br />This is an HTML email.",
"msg_plain": "Hi\nThis is a plain text email.",
"signature": True # use my account signature
}
draft = gmail.create_draft(**params) # equivalent to create_draft(to="[email protected]", sender=...)
```

It couldn't be easier!

### Retrieving messages:
Expand Down
65 changes: 65 additions & 0 deletions simplegmail/draft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
File: draft.py
----------------
This module contains the implementation of the Draft object.

"""

from typing import List, Optional, Union

from httplib2 import Http
from googleapiclient.errors import HttpError

from simplegmail import label
from simplegmail.attachment import Attachment
from simplegmail.label import Label
from simplegmail.message import Message


class Draft(object):
"""
The Draft class for drafts in your Gmail mailbox. This class should not
be manually constructed. Contains all information about the associated
draft.

Args:
service: the Gmail service object.
user_id: the username of the account the draft belongs to.
id: the draft id.
message: the message.

Attributes:
_service (googleapiclient.discovery.Resource): the Gmail service object.
user_id (str): the username of the account the message belongs to.
id (str): the draft id.
message (Message): the message.

"""

def __init__(
self,
service: 'googleapiclient.discovery.Resource',
creds: 'oauth2client.client.OAuth2Credentials',
user_id: str,
id: str,
message: Message
) -> None:
self._service = service
self.creds = creds
self.user_id = user_id
self.id = id
self.message = message

@property
def service(self) -> 'googleapiclient.discovery.Resource':
if self.creds.access_token_expired:
self.creds.refresh(Http())

return self._service

def __repr__(self) -> str:
"""Represents the object by its sender, recipient, and id."""

return (
f'Draft(to: {self.recipient}, from: {self.sender}, id: {self.id})'
)
Loading