You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,6 +130,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
130
130
-[How to Make a URL Shortener in Python](https://www.thepythoncode.com/article/make-url-shortener-in-python). ([code](general/url-shortener))
131
131
-[How to Get Google Page Ranking in Python](https://www.thepythoncode.com/article/get-google-page-ranking-by-keyword-in-python). ([code](general/getting-google-page-ranking))
132
132
-[How to Make a Telegram Bot in Python](https://www.thepythoncode.com/article/make-a-telegram-bot-in-python). ([code](general/telegram-bot))
133
+
-[How to Use Gmail API in Python](https://www.thepythoncode.com/article/use-gmail-api-in-python). ([code](general/gmail-api))
# [How to Use Gmail API in Python](https://www.thepythoncode.com/article/use-gmail-api-in-python)
2
+
To use the scripts here:
3
+
- Create your credentials file in Google API dashboard and putting it in the current directory, follow [this tutorial](https://www.thepythoncode.com/article/use-gmail-api-in-python) for detailed information.
4
+
-`pip3 install -r requirements.txt`
5
+
- Change `our_email` variable in `common.py` to your gmail address.
6
+
- To send emails, use the `send_emails.py` script:
7
+
```
8
+
python send_emails.py --help
9
+
```
10
+
**Output:**
11
+
```
12
+
usage: send_emails.py [-h] [-f FILES [FILES ...]] destination subject body
13
+
14
+
Email Sender using Gmail API
15
+
16
+
positional arguments:
17
+
destination The destination email address
18
+
subject The subject of the email
19
+
body The body of the email
20
+
21
+
optional arguments:
22
+
-h, --help show this help message and exit
23
+
-f FILES [FILES ...], --files FILES [FILES ...]
24
+
email attachments
25
+
```
26
+
For example, sending to example@domain.com:
27
+
```
28
+
python send_emails.py example@domain.com "This is a subject" "Body of the email" --files file1.pdf file2.txt file3.img
29
+
```
30
+
- To read emails, use the `read_emails.py` script. Downloading & parsing emails for Python related emails:
31
+
```
32
+
python read_emails.py "python"
33
+
```
34
+
This will output basic information on all matched emails and creates a folder for each email along with attachments and HTML version of the emails.
35
+
- To mark emails as **read** or **unread**, consider using `mark_emails.py`:
36
+
```
37
+
python mark_emails.py --help
38
+
```
39
+
**Output**:
40
+
```
41
+
usage: mark_emails.py [-h] [-r] [-u] query
42
+
43
+
Marks a set of emails as read or unread
44
+
45
+
positional arguments:
46
+
query a search query that selects emails to mark
47
+
48
+
optional arguments:
49
+
-h, --help show this help message and exit
50
+
-r, --read Whether to mark the message as read
51
+
-u, --unread Whether to mark the message as unread
52
+
```
53
+
Marking emails from **Google Alerts** as **Read**:
54
+
```
55
+
python mark_emails.py "Google Alerts" --read
56
+
```
57
+
Marking emails sent from example@domain.com as **Unread**:
58
+
```
59
+
python mark_emails.py "example@domain.com" -u
60
+
```
61
+
- To delete emails, consider using `delete_emails.py` script, e.g: for deleting emails about Bitcoin:
62
+
```
63
+
python delete_emails.py "bitcoin"
64
+
```
65
+
- If you want the full code, consider using `tutorial.ipynb` file.
66
+
- Or if you want a all-in-one script, `gmail_api.py` is here as well!
0 commit comments