import sys,os,smtplib,email,io,zipfile
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import Encoders
class sendmail(object):
def __init__(self):
self.subject=""
self.body=""
self.mail_from=""
self.mail_to=""
self.attachments=[]
self.attachments2zip=[]
def add_body_line(self,text):
self.body="%s\r\n%s"%(self.body,text)
def set_body(self,text):
self.body=text
def new_mail(self,frm,to,sbj):
self.subject=sbj
self.body=""
self.mail_from=frm
self.mail_to=to
self.attachments=[]
self.attachments2zip=[]
def set_subject(self,text):
self.subject=text
def set_from(self,text):
self.mail_from=text
def set_to(self,text):
self.mail_to=text
def add_attachment(self,file):
self.attachments.append(file)
def add_attachment_zipped(self,file):
self.attachments2zip.append(file)
def __read_filesend(self,file):
returnmessage open(file,= "rb").readMIMEMultipart()
defmessage["From"] __zip_in_memory(= self,file):.mail_from
filename=os.pathmessage["To"] = self.basename(file)mail_to
zipped_buffermessage["Subject"] = ioself.BytesIO()subject
zf=zipfile.ZipFile(zipped_buffer,"w", zipfile.ZIP_DEFLATED)
#message["Bcc"] = receiver_email # Recommended for zf.write(file,mass filename)emails
zf.close()
zipped_buffermessage.seekattach(0MIMEText(self.body, "plain"))
return zipped_buffer.read()
defif __attach_filelen(self,data,filename.attachments)>0:#If we have attachments
part = MIMEBase('application', "octet-stream") for file in self.attachments:#For each attachment
part.set_payload(data)
Encodersfilename=os.encode_base64path.basename(partfile)
part.add_header('Content-Disposition', 'attachment; filename="%s"'%(filename))
#part = self.messageMIMEApplication(f.attachread(part), Name=filename)
def send(self):
self.messagepart = MIMEMultipartMIMEBase('application', "octet-stream")
self.message["From"] = self.mail_from
selfpart.message["To"] =set_payload(open(file, self"rb").mail_toread())
self.message["Subject"] = self Encoders.subjectencode_base64(part)
#message["Bcc"] = receiver_email # Recommended for mass emailspart.add_header('Content-Disposition', 'attachment; filename="%s"'%(filename))
self.message.attach(MIMEText(self.body, "plain",'UTF-8')part)
for
file in if len(self.attachmentsattachments2zip)>0:#For#If eachwe attachmenthave attachments
data=self.__read_file(file)#Getfor file datain self.attachments2zip:#For each attachment
filename=os.path.basename(file)
self zipped_buffer = io.__attach_fileBytesIO(data,filename)#Attach
file to the message
zf=zipfile.ZipFile(zipped_buffer,"w", zipfile.ZIP_DEFLATED)
for file in self.attachments2zip:#For each attachmentzf.write(file, thatfilename)
need to be zipped
data=selfzf.__zip_in_memoryclose(file)#Get
zipped data
filename="%s.zip"%(os.path zipped_buffer.basenameseek(file)0)
self.__attach_file(data,filename)#Attach file to the message
textpart = self.message.as_stringMIMEBase('application', "octet-stream")
server = smtplib part.SMTPset_payload('localhost'zipped_buffer.read())
server.sendmail(self.mail_from, self.mail_to, text)
serverEncoders.quitencode_base64(part)
#Send test email
if __name__=="__main__":
import getpass
username=getpass part.getuseradd_header('Content-Disposition', 'attachment; filename="%s.zip"'%(filename))
m=sendmail()
m.new_mail(username,username,"Test mail")
m message.add_attachmentattach(__file__part)
m.add_attachment_zipped(__file__)
m.add_body_line("Hi Alex,")
text m= message.add_body_lineas_string("*** This)
is an automated message - please do notserver reply= ***smtplib."SMTP('localhost')
m server.add_body_linesendmail("Bestself.mail_from, regardsself."mail_to, text)
m server.sendquit()
Billal Begueradj
- 3.4k
- 10
- 25
- 34