|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to send bug reports. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode # __IGNORE_EXCEPTION__ |
|
13 except NameError: |
|
14 pass |
|
15 |
|
16 import os |
|
17 import sys |
|
18 import base64 |
|
19 |
|
20 import httplib2 |
|
21 |
|
22 import oauth2client.file |
|
23 from oauth2client import client, tools |
|
24 |
|
25 from googleapiclient import discovery |
|
26 |
|
27 import Globals |
|
28 |
|
29 |
|
30 SCOPES = 'https://www.googleapis.com/auth/gmail.send' |
|
31 CLIENT_SECRET_FILE = 'eric_client_secret.json' |
|
32 APPLICATION_NAME = 'Eric Python Send Email' |
|
33 |
|
34 |
|
35 def getCredentials(): |
|
36 """ |
|
37 Module function to get the Google credentials. |
|
38 |
|
39 @return Google Mail credentials |
|
40 """ |
|
41 homeDir = os.path.expanduser('~') |
|
42 credentialDir = os.path.join(homeDir, '.credentials') |
|
43 if not os.path.exists(credentialDir): |
|
44 os.makedirs(credentialDir) |
|
45 credentialPath = os.path.join(credentialDir, |
|
46 'eric-python-email-send.json') |
|
47 store = oauth2client.file.Storage(credentialPath) |
|
48 credentials = store.get() |
|
49 if not credentials or credentials.invalid: |
|
50 flow = client.flow_from_clientsecrets( |
|
51 os.path.join(Globals.getConfigDir(), CLIENT_SECRET_FILE), |
|
52 SCOPES) |
|
53 flow.user_agent = APPLICATION_NAME |
|
54 credentials = tools.run_flow(flow, store) |
|
55 return credentials |
|
56 |
|
57 |
|
58 def GoogleMailSendMessage(message): |
|
59 """ |
|
60 Module function to send an email message via Google Mail. |
|
61 |
|
62 @param message email message to be sent |
|
63 @type email.mime.text.MIMEBase |
|
64 @return tuple containing a success flag and a result or error message |
|
65 @rtype tuple of (bool, str) |
|
66 """ |
|
67 try: |
|
68 credentials = getCredentials() |
|
69 http = credentials.authorize(httplib2.Http()) |
|
70 service = discovery.build('gmail', 'v1', http=http) |
|
71 if sys.version_info[0] == 2: |
|
72 message1 = _prepareMessage_v2(message) |
|
73 else: |
|
74 message1 = _prepareMessage_v3(message) |
|
75 result = service.users().messages()\ |
|
76 .send(userId="me", body=message1).execute() |
|
77 |
|
78 return True, result |
|
79 except Exception as error: |
|
80 return False, str(error) |
|
81 |
|
82 |
|
83 def _prepareMessage_v2(message): |
|
84 """ |
|
85 Module function to prepare the message for sending (Python2 Variant). |
|
86 |
|
87 @param message message to be prepared |
|
88 @type email.mime.text.MIMEBase |
|
89 @return prepared message dictionary |
|
90 @rtype dict |
|
91 """ |
|
92 raw = base64.urlsafe_b64encode(message.as_string()) |
|
93 return {'raw': raw} |
|
94 |
|
95 |
|
96 def _prepareMessage_v3(message): |
|
97 """ |
|
98 Module function to prepare the message for sending (Python2 Variant). |
|
99 |
|
100 @param message message to be prepared |
|
101 @type email.mime.text.MIMEBase |
|
102 @return prepared message dictionary |
|
103 @rtype dict |
|
104 """ |
|
105 messageAsBase64 = base64.urlsafe_b64encode(message.as_bytes()) |
|
106 raw = messageAsBase64.decode() |
|
107 return {'raw': raw} |
|
108 |
|
109 |
|
110 def GoogleMailHelp(): |
|
111 """ |
|
112 Module function to get some help about how to enable the Google Mail |
|
113 OAuth2 service. |
|
114 |
|
115 @return help text |
|
116 @rtype str |
|
117 """ |
|
118 return ( |
|
119 "<h2>Steps to turn on the Gmail API</h2>" |
|
120 "<ol>" |
|
121 "<li>Use <a href='{0}'>this wizard</a> to create or select a project" |
|
122 " in the Google Developers Console and automatically turn on the API." |
|
123 " Click <b>Continue</b>, then <b>Go to credentials</b>.</li>" |
|
124 "<li>At the top of the page, select the <b>OAuth consent screen</b>" |
|
125 " tab. Select an <b>Email address</b>, enter a <b>Product name</b> if" |
|
126 " not already set, and click the <b>Save</b> button.</li>" |
|
127 "<li>Select the <b>Credentials</b> tab, click the <b>Add credentials" |
|
128 "</b> button and select <b>OAuth 2.0 client ID</b>.</li>" |
|
129 "<li>Select the application type <b>Other</b>, enter the name "" |
|
130 "{1}", and click the <b>Create</b>" |
|
131 " button.</li>" |
|
132 "<li>Click <b>OK</b> to dismiss the resulting dialog.</li>" |
|
133 "<li>Click the (Download JSON) button to the right of the client ID." |
|
134 "</li>" |
|
135 "<li>Move this file to the eric configuration directory" |
|
136 " <code>{2}</code> and rename it <code>{3}</code>.</li>" |
|
137 "</ol>".format( |
|
138 "https://console.developers.google.com/start/api?id=gmail", |
|
139 APPLICATION_NAME, |
|
140 Globals.getConfigDir(), |
|
141 CLIENT_SECRET_FILE |
|
142 ) |
|
143 ) |