Skip to content

Instantly share code, notes, and snippets.

@gerritjandebruin
Created January 15, 2022 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerritjandebruin/6a26193e7cad369e1a6e6d107609628f to your computer and use it in GitHub Desktop.
Save gerritjandebruin/6a26193e7cad369e1a6e6d107609628f to your computer and use it in GitHub Desktop.
Send an e-mail on any DSlab server in LIACS.
import os, smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
def send_mail(
send_from="bruingjde@liacs.leidenuniv.nl",
send_to: list = ["gerritjan.debruin@gmail.com"],
subject="We're done.",
text="We're done.",
files=None,
server="smtp.leidenuniv.nl"
):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment