crontab:
30 06 * * Mon /home/ted/send_email.x > send_email.log
send_email.x
#!/usr/local/bin/python3.7
import smtplib, ssl
import datetime
import secrets
from base64 import urlsafe_b64encode as b64e, urlsafe_b64decode as b64d
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
backend = default_backend()
iterations = 100_000
def _derive_key(password: bytes, salt: bytes, iterations: int = iterations) -> bytes:
"""Derive a secret key from a given password and salt"""
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(), length=32, salt=salt,
iterations=iterations, backend=backend)
return b64e(kdf.derive(password))
def password_decrypt(token: bytes, password: str) -> bytes:
decoded = b64d(token)
salt, iter, token = decoded[:16], decoded[16:20], b64e(decoded[20:])
iterations = int.from_bytes(iter, 'big')
key = _derive_key(password.encode(), salt, iterations)
return Fernet(key).decrypt(token)
password = 'xxxxxxx' # put your password here
token = b'QDUf_61z0TKsxZ-Jk94j2AABhqCAAAAAAGCjO4L3iQeT7aqOMnasrd5y7eQyXmUIe6mOhxO9BcTdLve-RKwpa71KcCD2rfCN7IYFxO9JJ1_DH3TwExHwZDOXla1u'
gmail_password=password_decrypt(token, password).decode()
d = datetime.datetime.now()
if d.isoweekday() != 1 :
print("You can only send this Email on Monday!")
exit()
smtp_server = "smtp.gmail.com"
port = 587 # For SSL
sender_email = "ted.liu.2030@gmail.com"
#receiver_email = ['tony.liu.033@gmail.com','ted.liu.2030@gmail.com']
receiver_email = ['helen@newdatatech.com','itgotousa@gmail.com','ted.liu.2030@gmail.com']
# Create a secure SSL context
context = ssl.create_default_context()
d = datetime.datetime.today().weekday()
last_monday=datetime.datetime.today() - datetime.timedelta(days=(7-d))
yesterday=datetime.datetime.today() - datetime.timedelta(days=(1))
from_date = last_monday.strftime("%Y/%m/%d")
yesterday_date = yesterday.strftime("%Y/%m/%d")
workday = from_date + " -- " + yesterday_date
subject = "Subject: Workday for the week of " + from_date + " -- " + yesterday_date
message1= """\
Hi Helen,
Please count my work day for the week of """
message2="""\
as 5 days, 5 X 8 = 40 hrs
Thanks,
Ted"""
message=subject + message1 + workday + message2;
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo()
server.starttls(context=context) # Secure the connection
server.ehlo()
server.login(sender_email, gmail_password)
server.sendmail(sender_email, receiver_email, message)
#server.sendmail(sender_email, receiver_email, message)
#server.close()
except Exception as e:
# Print any error messages to stdout
now = datetime.datetime.now()
print(now, e)
finally:
server.quit()
now = datetime.datetime.now()
print(now, 'Email sent ...')
Use this function to encrypt your password:
def password_encrypt(message: bytes, password: str, iterations: int = iterations) -> bytes:
salt = secrets.token_bytes(16)
key = _derive_key(password.encode(), salt, iterations)
return b64e(
b'%b%b%b' % (
salt,
iterations.to_bytes(4, 'big'),
b64d(Fernet(key).encrypt(message)),
)
)
Install Python 3.7:
Step 1 – Requirements
This Python installation required the GCC compiler on your system. Login to your server using ssh or shell access. Now, use the following command to install prerequisites for Python before installing it.
yum install -y gcc openssl-devel bzip2-devel libffi-devel zlib-devel
Step 2 – Download Python 3.7
Download Python using the following command from the Python official site. You can also download the latest version in place of specified below.
cd /usr/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
Now extract the downloaded package.
tar xzf Python-3.7.9.tgz
Step 3 – Install Python 3.7
Use below set of commands to compile Python source code on your system using altinstall.
cd Python-3.7.9
./configure --enable-optimizations
make altinstall
make altinstall is used to prevent replacing the default python binary file /usr/bin/python.
Now remove downloaded source archive file from your system
rm /usr/src/Python-3.7.9.tgz
Step 4 – Check Python Version
Check the latest version installed of python. Use command python3.7 instead of just python.
python3.7 -V
Python 3.7.9