Skip to content

Instantly share code, notes, and snippets.

@wfng92
Last active July 14, 2023 16:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wfng92/a36729f5d8a43652abe46b9ab944d64a to your computer and use it in GitHub Desktop.
Save wfng92/a36729f5d8a43652abe46b9ab944d64a to your computer and use it in GitHub Desktop.
from flask import Flask, request, jsonify
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
app = Flask(__name__)
def send_test_mail(body):
sender_email = "sender@email.com"
receiver_email = "receiver@email.com"
msg = MIMEMultipart()
msg['Subject'] = '[Email Test]'
msg['From'] = sender_email
msg['To'] = receiver_email
msgText = MIMEText('<b>%s</b>' % (body), 'html')
msg.attach(msgText)
filename = "example.txt"
msg.attach(MIMEText(open(filename).read()))
with open('example.jpg', 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-Disposition', 'attachment', filename="example.jpg")
msg.attach(img)
pdf = MIMEApplication(open("example.pdf", 'rb').read())
pdf.add_header('Content-Disposition', 'attachment', filename= "example.pdf")
msg.attach(pdf)
try:
with smtplib.SMTP('smtp.office365.com', 587) as smtpObj:
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login("sender@email.com", "password")
smtpObj.sendmail(sender_email, receiver_email, msg.as_string())
except Exception as e:
print(e)
@app.route('/')
def hello_world():
return "Hello world!"
if __name__ == "__main__":
send_test_mail("Welcome to Medium!")
app.run('0.0.0.0',port=5000)
@Douglasonen
Copy link

this was very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment