def encode_email_part(content, content_type): try: # simplest email - plain ascii encoded_content = content.encode('ascii') encoding = 'ascii' except Exception: # utf8 will get base64 encoded so we only do it if ascii fails encoded_content = content.encode('utf-8') encoding = 'utf-8' for line in encoded_content.splitlines(): if len(line) > MAX_MAIL_LINE_OCTETS: # switch to Quoted-Printable encoding to avoid too-long lines # we could always Quoted-Printabl, but it makes the output a little messier and less human-readable # the particular order of all these operations seems to be very important for everything to end up right msg = MIMEText(None, content_type) msg.replace_header('content-transfer-encoding', 'quoted-printable') cs = email.charset.Charset('utf-8') cs.header_encoding = email.charset.QP cs.body_encoding = email.charset.QP payload = cs.body_encode(content.encode('utf-8')) msg.set_payload(payload, 'utf-8') return msg else: return MIMEText(encoded_content, content_type, encoding)
def AttacheFichiersJoints(self, email=None): for fichier in self.fichiers: """Guess the content type based on the file's extension. Encoding will be ignored, altough we should check for simple things like gzip'd or compressed files.""" ctype, encoding = mimetypes.guess_type(fichier) if ctype is None or encoding is not None: # No guess could be made, or the file is encoded (compresses), so # use a generic bag-of-bits type. ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) if maintype == 'text': fp = open(fichier) # Note : we should handle calculating the charset part = MIMEText(fp.read(), _subtype=subtype) fp.close() elif maintype == 'image': fp = open(fichier, 'rb') part = MIMEImage(fp.read(), _subtype=subtype) fp.close() else: fp = open(fichier, 'rb') part = MIMEBase(maintype, subtype) part.set_payload(fp.read()) fp.close() # Encode the payload using Base64 encoders.encode_base64(part) # Set the filename parameter nomFichier = os.path.basename(fichier) if type(nomFichier) == six.text_type: nomFichier = FonctionsPerso.Supprime_accent(nomFichier) # changement cosmetique pour ajouter les guillements autour du filename part.add_header('Content-Disposition', "attachment; filename=\"%s\"" % nomFichier) email.attach(part)
def AttacheFichiersJoints(self, email=None): for fichier in self.fichiers: """Guess the content type based on the file's extension. Encoding will be ignored, altough we should check for simple things like gzip'd or compressed files.""" ctype, encoding = mimetypes.guess_type(fichier) if ctype is None or encoding is not None: # No guess could be made, or the file is encoded (compresses), so # use a generic bag-of-bits type. ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) if maintype == 'text': fp = open(fichier) # Note : we should handle calculating the charset part = MIMEText(fp.read(), _subtype=subtype) fp.close() elif maintype == 'image': fp = open(fichier, 'rb') part = MIMEImage(fp.read(), _subtype=subtype) fp.close() else: fp = open(fichier, 'rb') part = MIMEBase(maintype, subtype) part.set_payload(fp.read()) fp.close() # Encode the payload using Base64 encoders.encode_base64(part) # Set the filename parameter nomFichier = os.path.basename(fichier) if type(nomFichier) == six.text_type: nomFichier = FonctionsPerso.Supprime_accent(nomFichier) # changement cosmetique pour ajouter les guillements autour du filename part.add_header('Content-Disposition', "attachment; filename=\"%s\"" % nomFichier) email.attach(part)
def send(self,sendto='', subject='', attach='', body=' '): print('Sending email') msg = MIMEMultipart() msg["From"] = self.mailfrom msg["To"] = sendto msg["Subject"] = subject msg['Date'] = formatdate(localtime=True) #add messagw self._print('Attaching msg: %s' %body) message = MIMEText('text', "plain") message.set_payload(body+'\n') msg.attach(message) # attach a file if attach: self._print('Attachment found: %s'% attach) part = MIMEBase('application', "octet-stream") part.set_payload( open(attach,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % attach) msg.attach(part) self._print('Creating SMTP') server = smtplib.SMTP(self.host,int(self.port)) if self.tls == 'True' or self.tls == 'true': server.starttls() self._print('tls started.') if self.auth == 'True' or self.auth == 'true': try: self._print('Logging into to SMTP: %s %s'%(self.user,self.passwd)) server.login(self.user, self.passwd) # optional except Exception as e: print('Failed Login %s'%e) sys.exit(0) else: try: self._print('Connecting to SMTP') server.connect() except Exception as e: print('Failed to connect %s'%e) sys.exit(0) try: self._print('Sending mail to: %s' %sendto) server.sendmail(self.mailfrom, sendto, msg.as_string()) print('mail sent.') server.close() except Exception as e: errorMsg = "Unable to send email. Error: %s" % str(e)
def send(self, sendto='', subject='', attach='', body=' '): print('Sending email') msg = MIMEMultipart() msg["From"] = self.mailfrom msg["To"] = sendto msg["Subject"] = subject msg['Date'] = formatdate(localtime=True) #add messagw self._print('Attaching msg: %s' % body) message = MIMEText('text', "plain") message.set_payload(body + '\n') msg.attach(message) # attach a file if attach: self._print('Attachment found: %s' % attach) part = MIMEBase('application', "octet-stream") part.set_payload(open(attach, "rb").read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % attach) msg.attach(part) self._print('Creating SMTP') server = smtplib.SMTP(self.host, int(self.port)) if self.tls == 'True' or self.tls == 'true': server.starttls() self._print('tls started.') if self.auth == 'True' or self.auth == 'true': try: self._print('Logging into to SMTP: %s %s' % (self.user, self.passwd)) server.login(self.user, self.passwd) # optional except Exception as e: print('Failed Login %s' % e) sys.exit(0) else: try: self._print('Connecting to SMTP') server.connect() except Exception as e: print('Failed to connect %s' % e) sys.exit(0) try: self._print('Sending mail to: %s' % sendto) server.sendmail(self.mailfrom, sendto, msg.as_string()) print('mail sent.') server.close() except Exception as e: errorMsg = "Unable to send email. Error: %s" % str(e)