Created
December 22, 2019 16:49
-
-
Save joelhoro/be328c777783f73864e1344fcf865040 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 email.message import Message | |
from email.generator import Generator | |
from email import generator | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
from email import encoders | |
import os | |
class MimeMessage(): | |
def __init__(self,fromEmail,to,cc="",subject="",body="",format='text'): | |
self.msg = msg = MIMEMultipart() | |
msg['From'] = fromEmail | |
msg['To'] = to | |
msg['Cc'] = cc | |
msg['Subject'] = subject | |
msg1 = MIMEText(body,format) | |
self.msg.attach(msg1) | |
def add_attachment(self,filename, text): | |
part = MIMEText(text) | |
part.add_header('Content-Disposition', 'attachment', filename=filename) | |
self.msg.attach(part) | |
def add_file(self,filename): | |
part = MIMEBase('application','octet-stream') | |
with open(filename, 'rb') as file: | |
part.set_payload(file.read()) | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition', | |
'attachment; filename="{}"'.format(os.path.basename(filename))) | |
self.msg.attach(part) | |
def to_string(self): | |
return self.msg.as_string() | |
def save(self, filename): | |
file = open(filename,'w') | |
g= Generator(file) | |
g.flatten(self.msg) | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment