Skip to content

Commit 26cbca6

Browse files
RajeshGovosqrrrlanuraggoogler
authored
Gmail patch 3 (googleworkspace#261)
* Added pull request template Added pull request template * updated header updated header * Update pull_request_template.md * Update pull_request_template.md * Add unit/integration test to checklist * Gmail snippets original code from devral repo. * Gmail snippets original code from devral repo. * Create and send an email message with and without attachment * Update send_message.py * Update send_message_with_attachment.py * Update send_message.py * Delete send_message.py * Delete send_message_with_attachment.py * Create and update signature in gmail Co-authored-by: Steve Bazyl <sqrrrl@gmail.com> Co-authored-by: anuraggoogler <aanursharm@google.com>
1 parent 71823c1 commit 26cbca6

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Copyright 2018 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
"""
15+
# [START gmail_update_signature]
16+
17+
from __future__ import print_function
18+
19+
import google.auth
20+
from googleapiclient.discovery import build
21+
from googleapiclient.errors import HttpError
22+
23+
24+
def update_signature():
25+
"""Create and update signature in gmail.
26+
Returns:Draft object, including updated signature.
27+
28+
Load pre-authorized user credentials from the environment.
29+
TODO(developer) - See https://developers.google.com/identity
30+
for guides on implementing OAuth2 for the application.
31+
"""
32+
creds, _ = google.auth.default()
33+
34+
try:
35+
# create gmail api client
36+
service = build('gmail', 'v1', credentials=creds)
37+
38+
primary_alias = None
39+
40+
# pylint: disable=E1101
41+
aliases = service.users().settings().sendAs().list(userId='me')\
42+
.execute()
43+
for alias in aliases.get('sendAs'):
44+
if alias.get('isPrimary'):
45+
primary_alias = alias
46+
break
47+
48+
send_as_configuration = {
49+
'displayName': primary_alias.get('sendAsEmail'),
50+
'signature': 'Automated Signature'
51+
}
52+
53+
# pylint: disable=E1101
54+
result = service.users().settings().sendAs() \
55+
.patch(userId='me', sendAsEmail=primary_alias.get('sendAsEmail'),
56+
body=send_as_configuration).execute()
57+
print(F'Updated signature for: {result.get("displayName")}')
58+
59+
except HttpError as error:
60+
print(F'An error occurred: {error}')
61+
result = None
62+
63+
return result.get('signature')
64+
65+
66+
if __name__ == '__main__':
67+
update_signature()
68+
# [END gmail_update_signature]

0 commit comments

Comments
 (0)