src/eric7/EricNetwork/EricGoogleMail.py

Sat, 26 Apr 2025 12:34:32 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 26 Apr 2025 12:34:32 +0200
branch
eric7
changeset 11240
c48c615c04a3
parent 11090
f5f5f5803935
permissions
-rw-r--r--

MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.

5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
11090
f5f5f5803935 Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10926
diff changeset
3 # Copyright (c) 2017 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a dialog to send bug reports.
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9427
diff changeset
10 import base64
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import os
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9427
diff changeset
13 from google.auth.exceptions import RefreshError
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
14 from google.auth.transport.requests import Request
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9427
diff changeset
15 from google.oauth2.credentials import Credentials, UserAccessTokenCredentials
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
16 from google_auth_oauthlib.flow import InstalledAppFlow
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9427
diff changeset
17 from googleapiclient import discovery, errors
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9427
diff changeset
18 from PyQt6.QtCore import QObject, pyqtSignal
6559
1265efa7364f Some fixes for configuration pages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
19
10926
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
20 from eric7 import EricUtilities
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9427
diff changeset
22 from .EricGoogleMailHelpers import CLIENT_SECRET_FILE, SCOPES, TOKEN_FILE
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
8354
12ebd3934fef Renamed 'E5Utilities' to 'EricUtilities' and 'E5Network' to 'EricNetwork'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
25 class EricGoogleMail(QObject):
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
27 Class implementing the logic to send emails via Google Mail.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
28
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
29 @signal sendResult(bool, str) emitted to indicate the transmission result
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
30 and a result message
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
32
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
33 sendResult = pyqtSignal(bool, str)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
34
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
35 def __init__(self, parent=None):
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
36 """
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
37 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
38
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
39 @param parent reference to the parent object
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
40 @type QObject
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
41 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
42 super().__init__(parent=parent)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
43
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
44 self.__messages = []
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
45
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
46 self.__credentials = None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
47
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
48 def sendMessage(self, message):
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
49 """
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
50 Public method to send a message via Google Mail.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
51
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
52 @param message email message to be sent
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
53 @type email.mime.text.MIMEBase
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
54 """
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
55 self.__messages.append(message)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
56
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
57 if not self.__credentials:
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
58 self.__startSession()
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
59
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
60 self.__doSendMessages()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
61
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
62 def __prepareMessage(self, message):
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
63 """
7199
c71bd6f21748 Removed some more Python2 stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7198
diff changeset
64 Private method to prepare the message for sending.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
65
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
66 @param message message to be prepared
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
67 @type email.mime.text.MIMEBase
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
68 @return prepared message dictionary
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
69 @rtype dict
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
70 """
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
71 messageAsBase64 = base64.urlsafe_b64encode(message.as_bytes())
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
72 raw = messageAsBase64.decode()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
73 return {"raw": raw}
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
74
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
75 def __startSession(self):
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
76 """
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
77 Private method to start an authorized session and optionally execute the
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
78 authorization flow.
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
79 """
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
80 # check for availability of secrets file
10926
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
81 if not os.path.exists(
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
82 os.path.join(EricUtilities.getConfigDir(), CLIENT_SECRET_FILE)
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
83 ):
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
84 self.sendResult.emit(
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
85 False,
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
86 self.tr(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
87 "The client secrets file is not present. Has the Gmail"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
88 " API been enabled?"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
89 ),
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
90 )
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
91 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
92
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
93 credentials = self.__loadCredentials()
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
94 credentials = UserAccessTokenCredentials()
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
95 # If there are no (valid) credentials available, let the user log in.
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
96 if not credentials or not credentials.valid:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
97 if credentials and credentials.expired and credentials.refresh_token:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
98 try:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
99 credentials.refresh(Request())
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
100 except RefreshError:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
101 credentials = None
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
102 if not credentials or not credentials.valid:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
103 flow = InstalledAppFlow.from_client_secrets_file(
10926
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
104 os.path.join(EricUtilities.getConfigDir(), CLIENT_SECRET_FILE),
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
105 SCOPES,
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
106 )
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
107 credentials = flow.run_local_server(port=0)
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
108 # Save the credentials for the next run
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
109 self.__saveCredentials(credentials)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
110
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
111 self.__credentials = credentials
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
112
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
113 def __doSendMessages(self):
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
114 """
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
115 Private method to send all queued messages.
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
116 """
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
117 if not self.__credentials or not self.__credentials.valid:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
118 self.sendResult.emit(False, self.tr("No valid credentials available."))
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
119 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
120
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
121 try:
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
122 results = []
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
123 service = discovery.build(
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
124 "gmail", "v1", credentials=self.__credentials, cache_discovery=False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
125 )
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
126 count = 0
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
127 while self.__messages:
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
128 count += 1
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
129 message = self.__messages.pop(0)
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
130 message1 = self.__prepareMessage(message)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
131 service.users().messages().send(userId="me", body=message1).execute()
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
132 results.append(self.tr("Message #{0} sent.").format(count))
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
134 self.sendResult.emit(True, "\n\n".join(results))
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
135 except errors.HttpError as err:
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
136 self.sendResult.emit(False, str(err))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
137
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
138 def __loadCredentials(self):
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
139 """
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
140 Private method to load credentials from the token file.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
141
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
142 @return created credentials object
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
143 @rtype Credentials
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
144 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
145 homeDir = os.path.expanduser("~")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
146 credentialsDir = os.path.join(homeDir, ".credentials")
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
147 if not os.path.exists(credentialsDir):
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
148 os.makedirs(credentialsDir)
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
149 tokenPath = os.path.join(credentialsDir, TOKEN_FILE)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
150
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
151 if os.path.exists(tokenPath):
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
152 return Credentials.from_authorized_user_file(tokenPath, SCOPES)
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
153 else:
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
154 return None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
155
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
156 def __saveCredentials(self, credentials):
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
157 """
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
158 Private method to save credentials to the token file.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
159
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
160 @param credentials credentials to be saved
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
161 @type Credentials
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
162 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
163 homeDir = os.path.expanduser("~")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
164 credentialsDir = os.path.join(homeDir, ".credentials")
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
165 if not os.path.exists(credentialsDir):
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
166 os.makedirs(credentialsDir)
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
167 tokenPath = os.path.join(credentialsDir, TOKEN_FILE)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
168
6828
bb6667ea9ae7 Changed the Google Mail interface to not use obsoleted packages anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6825
diff changeset
169 with open(tokenPath, "w") as tokenFile:
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
170 tokenFile.write(credentials.to_json())
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 def GoogleMailHelp():
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 Module function to get some help about how to enable the Google Mail
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 OAuth2 service.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
177
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 @return help text
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 @rtype str
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 """
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 return (
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 "<h2>Steps to turn on the Gmail API</h2>"
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 "<ol>"
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 "<li>Use <a href='{0}'>this wizard</a> to create or select a project"
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 " in the Google Developers Console and automatically turn on the API."
9427
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
186 " Click <b>Continue</b>.</li>"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
187 "<li>Select <b>Create Project</b>, fill out the form and select <b>Create</b>."
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
188 " Select <b>Continue</b> and <b>Activate</b>.</li>"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
189 "<li>In the left side pane (menu) select <b>APIs and Services</b></li>"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
190 "<li>Select <b>OAuth consent screen</b>, press <b>Create</b> and"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
191 " fill out the form.</li>"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
192 "<li>In the menu select <b>Credentials</b> and create OAuth credentials.</li>"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
193 "<li>At the end of the process select the <b>Download</b> button below the"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
194 "Client-ID. Save the credentials file to the eric configuration directory"
905e7af29101 Gmail Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
195 " <code>{1}</code> with the name <code>{2}</code>.</li>"
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 "</ol>".format(
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 "https://console.developers.google.com/start/api?id=gmail",
10926
9ef616cd220d Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
198 EricUtilities.getConfigDir(),
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
199 CLIENT_SECRET_FILE,
5769
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 )
944c04cec861 Added the capability to send these emails via Google Mail using OAuth2 authentication.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 )

eric ide

mercurial