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.
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.
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)
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)
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 () ``
.
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
Recommended Posts