Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from email.message import EmailMessage
- import smtplib
- from pathlib import Path
- def send_simple_message(email, subject, html, from_subject="Github", bcc="", attachment=None):
- from_address = 'no-reply@someone.com'
- auth_user = 'SDSDFSDFSDFSDFSDF'
- auth_password = 'SDGDFGyyrtyrtyrtytrtyrtyrtyrt'
- smtp_host = 'email-smtp.eu-west-1.amazonaws.com'
- smtp_port = 587
- def attach_file(email, filepath, extention, filename):
- types = {
- 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'pdf': 'application/pdf',
- # Put any mimetypes there
- }
- maintype, subtype = types[extention].split('/', 1)
- with open(filepath, 'rb') as content_file:
- content = content_file.read()
- email.add_attachment(content, maintype=maintype, subtype=subtype, filename=filename)
- return email
- def create_email_message(from_address, to_address, subject, body, bcc):
- msg = EmailMessage()
- msg['From'] = "%s <%s>" % (from_subject, from_address)
- msg['To'] = to_address
- msg['Subject'] = subject
- msg['Bcc'] = bcc
- msg.set_content(body, subtype='html')
- return msg
- msg = create_email_message(from_address, email, subject, html, bcc)
- if attachment:
- msg.make_mixed()
- filename = Path(attachment).name # file name ("report.pdf")
- subtype = str(Path(filename).suffix).replace('.', '') # "pdf"|"docx"|...file extention
- msg = attach_file(msg, attachment, subtype, filename)
- with smtplib.SMTP(smtp_host, port=smtp_port) as smtp_server:
- smtp_server.ehlo()
- smtp_server.starttls()
- smtp_server.login(auth_user, auth_password)
- smtp_server.send_message(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement