Last active
July 14, 2023 16:06
-
-
Save wfng92/a36729f5d8a43652abe46b9ab944d64a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this was very helpful