Skip to content

Instantly share code, notes, and snippets.

@eli2and40
Last active August 3, 2020 14:31
Show Gist options
  • Save eli2and40/9b490ebf6548cc4094ecf46a3858f753 to your computer and use it in GitHub Desktop.
Save eli2and40/9b490ebf6548cc4094ecf46a3858f753 to your computer and use it in GitHub Desktop.
"""
Let yourself know your simple http server is running on a specific port when you broke your computer screen
but you've still gotta get jiggy with your file system so you're using someone else's machine while they watch
tv on the telly you'd be watching otherwise
"""
import os, smtplib
from email.message import EmailMessage
def quickMail(msg,dst=os.environ.get('personalEmail'),subj=None,head=None,extra=None,att=None):
"""
Sends a short email to the given dst using gmail's smtp configuration with default sender/receiver info taken from user environment variables
Dependencies: os.environ.get [func], smtplib [mod], email.message.EmailMessage [obj]
Arguments: message, **destination
Output: None
"""
message = EmailMessage()
message['Subject'] = f'{subj if subj!=None else head if head!=None else ""}'
message['From'] = os.environ.get('promoEmail')
message['To'] = dst
message.set_content(msg)
if any([subj!=None,head!=None,extra!=None]):
message.add_alternative(f"""\
<!DOCTYPE html>
<html style='font-family:trebuchet ms;'>
<body>
<h1 style="color:SlateGray;">{head if head!=None else ''}</h1>
<p>{msg}</p>
<P>{extra if extra!=None else ''}
</body>
</html>
""", subtype='html')
if att != None:
for file in att.split('*'):
with open(file, "rb") as f:
fileData = f.read()
fileName = f.name.split(os.sep)[-1]
fileType = (os.path.splitext(f.name)[1]).replace(".","")
message.add_attachment(fileData, maintype="image", subtype=fileType, filename=fileName)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(os.environ.get('promoEmail'), os.environ.get('promoEmailPass'))
smtp.send_message(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment