Create Gmail in Python without API

Overview

Gmail mail composition screen display in Python

The way to do this is to write the text in a text file and The method is to get the text file and create the URL. Recently, the daily report has already done this way. In this article, I will write according to the previous flow, assuming a daily report.

URL creation

The URL and parameters were written in the previous article, but they are as follows.

https://mail.google.com/mail/?view=cm Concatenate the parameters to this URL.


 The parameters are as follows (almost the same)

 |Parameters|meaning|
 | :----: | :----: |
 | to= | To |
 | cc= | Cc |
 | bcc= | Bcc |
 | su= |subject|
 | body= |Text|


#### **`URL creation`**
```python

from datetime import datetime
import urllib.parse

def getUrl(body: str) -> str:
    url = "https://mail.google.com/mail/?view=cm"
    url += "&[email protected]"
    url += "&[email protected]"
    url += "&[email protected]"
    today = datetime.now()
    url +=  f"&su=Daily report{today.month}/{today.strftime('%d')}Daily report Taro"
    url += f"&body={strenc(body)}"

    return url

def strenc(txt: str) -> str :
    lst = list("#'|^<>{};?@&$" + '"')
    for v in lst:
        txt = txt.replace(v, urllib.parse.quote(v))
    txt = urllib.parse.quote(txt)
    return txt

Here and there is almost the same as last time.

Open URL

This is the same as last time, and we will execute the command. Since it is Python, it is easy to write unlike VBA.

Open URL with start command from Python


import subprocess

def openUrl(url: str):
    subprocess.call(f'cmd /c start "" "{url}"', shell=True)

Compose an email

Now that we have the function, we will get the contents of the text file and compose the email. Suppose you have a file on your desktop called Mail.txt.

import os

desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop/')
fileName = "Email.txt"

def main():
    with open(f"{desktop}{fileName}", mode="r", encoding="UTF-8") as f:
        url = getUrl(f.read())
        openUrl(url)

Batch creation

When I run it, the browser opens and Gmail opens. It is troublesome to execute Python at the command prompt one by one, so I will write a batch. Python files don't need to be on your desktop, so As an example, put `` `/ work / python / DailyReport / DailyReport.py``` in the user folder.

@echo off
cd ../work/python/DailyReport
python DailyReport.py

Save this batch to your desktop. With this, if you write the body in `mail.txt``` and execute the batch, I think that it was opened in the browser and the mail composition screen was displayed. (I will omit the execution result this time. Since the contents are the same ...) Regarding outlook, getUrl" in the previous [bonus](https://qiita.com/neruru_y/items/a5d0a3f7ef30f5a36962#%E3%81%8A%E3%81%BE%E3%81%91) I think you can do it by imitating () `` .

Whole source code

DailyReport.py


from datetime import datetime
import os
import urllib.parse
import subprocess

desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop/')
fileName = "Email.txt"

def main():
    with open(f"{desktop}{fileName}", mode="r", encoding="UTF-8") as f:
        url = getUrl(f.read())
        openUrl(url)

def getUrl(body: str) -> str:
    url = "https://mail.google.com/mail/?view=cm"
    url += "&[email protected]"
    url += "&[email protected]"
    url += "&[email protected]"
    today = datetime.now()
    url +=  f"&su=Daily report{today.month}/{today.strftime('%d')}Daily report Taro"
    url += f"&body={strenc(body)}"

    return url

def strenc(txt: str) -> str :
    lst = list("#'|^<>{};?@&$" + '"')
    for v in lst:
        txt = txt.replace(v, urllib.parse.quote(v))
    txt = urllib.parse.quote(txt)
    return txt

def openUrl(url: str):
    subprocess.call(f'cmd /c start "" "{url}"', shell=True)

if __name__ == "__main__":
    main()

mail.bat


@echo off
cd ../work/python/DailyReport
python DailyReport.py

Reference site

Recommended Posts

Create Gmail in Python without API
[Python] Create API to send Gmail
Create SpatiaLite in Python
Evernote API in Python
Draft Gmail in Python
C API in Python 3
Send Gmail in Python
Hit Mastodon's API in Python
Create a dictionary in Python
Create gif video in Python
Blender Python API in Houdini (Python 3)
[LINE Messaging API] Create a rich menu in Python
Getting the arXiv API in Python
I tried to create API list.csv in Python from swagger.yaml
Create Awaitable with Python / C API
Create a local scope in Python without polluting the namespace
Create a DI Container in Python
[Python3] Google translate google translate without using api
Create a binary file in Python
Hit the web API in Python
Create Python project documentation in Sphinx
Detect keystrokes in Python (without Enter)
Create a Kubernetes Operator in Python
Quickly implement REST API in Python
Access the Twitter API in Python
Create a random string in Python
Create and read messagepacks in Python
[Python / Django] Create a web API that responds in JSON format
Mouse operation using Windows API in Python
Create your own Linux commands in Python
Try using the Wunderlist API in Python
Automatically create Python API documentation with Sphinx
Try using the Kraken API in Python
Create ScriptableObject in Python when building ADX2
[LLDB] Create your own command in Python
Create a simple GUI app in Python
Get mail using Gmail API in Java
Create Python module [CarSensor API support module csapi]
Create a JSON object mapper in Python
Tweet using the Twitter API in Python
Create Qt designer parts in Python (PyQt)
Get Google Fit API data in Python
Get Youtube data in Python using Youtube Data API
Quickly try Microsoft's Face API in Python
[GPS] Create a kml file in Python
[Python] Quickly create an API with Flask
Try hitting the YouTube API in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python