22 import oauth2client.file |
22 import oauth2client.file |
23 from oauth2client import client, tools |
23 from oauth2client import client, tools |
24 |
24 |
25 from googleapiclient import discovery |
25 from googleapiclient import discovery |
26 |
26 |
|
27 from PyQt5.QtCore import QCoreApplication |
|
28 |
27 import Globals |
29 import Globals |
28 |
30 |
29 |
31 |
30 SCOPES = 'https://www.googleapis.com/auth/gmail.send' |
32 SCOPES = 'https://www.googleapis.com/auth/gmail.send' |
31 CLIENT_SECRET_FILE = 'eric_client_secret.json' |
33 CLIENT_SECRET_FILE = 'eric_client_secret.json' |
32 APPLICATION_NAME = 'Eric Python Send Email' |
34 APPLICATION_NAME = 'Eric Python Send Email' |
|
35 |
|
36 |
|
37 def isCredentialsFileAvailable(): |
|
38 """ |
|
39 Module function to check, if the credentials file has been installed. |
|
40 |
|
41 @return flag indicating, that the credentials file is there |
|
42 @rtype bool |
|
43 """ |
|
44 return os.path.exists( |
|
45 os.path.join(Globals.getConfigDir(), CLIENT_SECRET_FILE)) |
33 |
46 |
34 |
47 |
35 def getCredentials(): |
48 def getCredentials(): |
36 """ |
49 """ |
37 Module function to get the Google credentials. |
50 Module function to get the Google credentials. |
62 @param message email message to be sent |
75 @param message email message to be sent |
63 @type email.mime.text.MIMEBase |
76 @type email.mime.text.MIMEBase |
64 @return tuple containing a success flag and a result or error message |
77 @return tuple containing a success flag and a result or error message |
65 @rtype tuple of (bool, str) |
78 @rtype tuple of (bool, str) |
66 """ |
79 """ |
|
80 # check for secrets file first |
|
81 if not os.path.exists(os.path.join(Globals.getConfigDir(), |
|
82 CLIENT_SECRET_FILE)): |
|
83 return False, QCoreApplication.translate( |
|
84 "GoogleMailSendMessage", |
|
85 "The credentials file is not present. Has the Gmail API" |
|
86 " been enabled?") |
|
87 |
67 try: |
88 try: |
68 credentials = getCredentials() |
89 credentials = getCredentials() |
69 http = credentials.authorize(httplib2.Http()) |
90 http = credentials.authorize(httplib2.Http()) |
70 service = discovery.build('gmail', 'v1', http=http) |
91 service = discovery.build('gmail', 'v1', http=http) |
71 if sys.version_info[0] == 2: |
92 if sys.version_info[0] == 2: |