Skip to content

Commit 7e3d62d

Browse files
committed
Add method to create a draft
1 parent b623705 commit 7e3d62d

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ params = {
120120
message = gmail.send_message(**params) # equivalent to send_message(to="you@youremail.com", sender=...)
121121
```
122122

123+
### Create a draft:
124+
125+
```python
126+
from simplegmail import Gmail
127+
128+
gmail = Gmail() # will open a browser window to ask you to log in and authenticate
129+
130+
params = {
131+
"to": "you@youremail.com",
132+
"sender": "me@myemail.com",
133+
"subject": "My first email",
134+
"msg_html": "<h1>Woah, my first email!</h1><br />This is an HTML email.",
135+
"msg_plain": "Hi\nThis is a plain text email.",
136+
"signature": True # use my account signature
137+
}
138+
draft = gmail.create_draft(**params) # equivalent to create_draft(to="you@youremail.com", sender=...)
139+
```
140+
123141
It couldn't be easier!
124142

125143
### Retrieving messages:

simplegmail/draft.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from simplegmail import label
1414
from simplegmail.attachment import Attachment
1515
from simplegmail.label import Label
16+
from simplegmail.message import Message
1617

1718

1819
class Draft(object):

simplegmail/gmail.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from simplegmail import label
3333
from simplegmail.attachment import Attachment
34+
from simplegmail.draft import Draft
3435
from simplegmail.label import Label
3536
from simplegmail.message import Message
3637

@@ -176,6 +177,63 @@ def send_message(
176177
# Pass along the error
177178
raise error
178179

180+
def create_draft(
181+
self,
182+
sender: str,
183+
to: str,
184+
subject: str = '',
185+
msg_html: Optional[str] = None,
186+
msg_plain: Optional[str] = None,
187+
cc: Optional[List[str]] = None,
188+
bcc: Optional[List[str]] = None,
189+
attachments: Optional[List[str]] = None,
190+
signature: bool = False,
191+
user_id: str = 'me'
192+
) -> Message:
193+
"""
194+
Creates a draft.
195+
196+
Args:
197+
sender: The email address the draft is being sent from.
198+
to: The email address the draft is being sent to.
199+
subject: The subject line of the email.
200+
msg_html: The HTML message of the email.
201+
msg_plain: The plain text alternate message of the email. This is
202+
often displayed on slow or old browsers, or if the HTML message
203+
is not provided.
204+
cc: The list of email addresses to be cc'd.
205+
bcc: The list of email addresses to be bcc'd.
206+
attachments: The list of attachment file names.
207+
signature: Whether the account signature should be added to the
208+
draft.
209+
user_id: The address of the sending account. 'me' for the
210+
default address associated with the account.
211+
212+
Returns:
213+
The Draft object representing the created draft.
214+
215+
Raises:
216+
googleapiclient.errors.HttpError: There was an error executing the
217+
HTTP request.
218+
219+
"""
220+
221+
msg = {
222+
'message': self._create_message(
223+
sender, to, subject, msg_html, msg_plain, cc=cc, bcc=bcc,
224+
attachments=attachments, signature=signature, user_id=user_id
225+
)
226+
}
227+
228+
try:
229+
req = self.service.users().drafts().create(userId='me', body=msg)
230+
res = req.execute()
231+
return self._build_draft_from_ref(user_id, res, 'reference')
232+
233+
except HttpError as error:
234+
# Pass along the error
235+
raise error
236+
179237
def get_unread_inbox(
180238
self,
181239
user_id: str = 'me',
@@ -849,6 +907,56 @@ def _build_message_from_ref(
849907
bcc
850908
)
851909

910+
def _build_draft_from_ref(
911+
self,
912+
user_id: str,
913+
draft_ref: dict,
914+
attachments: str = 'reference'
915+
) -> Draft:
916+
"""
917+
Creates a Draft object from a reference.
918+
919+
Args:
920+
user_id: The username of the account the draft belongs to.
921+
draft_ref: The draft reference object returned from the Gmail
922+
API.
923+
attachments: Accepted values are 'ignore' which completely ignores
924+
all attachments, 'reference' which includes attachment
925+
information but does not download the data, and 'download' which
926+
downloads the attachment data to store locally. Default
927+
'reference'.
928+
929+
Returns:
930+
The Draft object.
931+
932+
Raises:
933+
googleapiclient.errors.HttpError: There was an error executing the
934+
HTTP request.
935+
936+
"""
937+
938+
try:
939+
# Get draft JSON
940+
draft = self.service.users().drafts().get(
941+
userId=user_id, id=draft_ref['id']
942+
).execute()
943+
944+
except HttpError as error:
945+
# Pass along the error
946+
raise error
947+
948+
else:
949+
id = draft['id']
950+
message = self._build_message_from_ref(user_id, draft['message'], attachments)
951+
952+
return Draft(
953+
self.service,
954+
self.creds,
955+
user_id,
956+
id,
957+
message
958+
)
959+
852960
def _evaluate_message_payload(
853961
self,
854962
payload: dict,

0 commit comments

Comments
 (0)