|
31 | 31 |
|
32 | 32 | from simplegmail import label |
33 | 33 | from simplegmail.attachment import Attachment |
| 34 | +from simplegmail.draft import Draft |
34 | 35 | from simplegmail.label import Label |
35 | 36 | from simplegmail.message import Message |
36 | 37 |
|
@@ -176,6 +177,63 @@ def send_message( |
176 | 177 | # Pass along the error |
177 | 178 | raise error |
178 | 179 |
|
| 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 | + |
179 | 237 | def get_unread_inbox( |
180 | 238 | self, |
181 | 239 | user_id: str = 'me', |
@@ -849,6 +907,56 @@ def _build_message_from_ref( |
849 | 907 | bcc |
850 | 908 | ) |
851 | 909 |
|
| 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 | + |
852 | 960 | def _evaluate_message_payload( |
853 | 961 | self, |
854 | 962 | payload: dict, |
|
0 commit comments