def getFormatedArgs(self, args=None): """ This lookd inside the args dictionnary and then convert any unicode string to string """ new_args = {} for keyword in args.keys(): data = args[keyword] if isinstance(keyword, unicode): keyword = keyword.encode(self.getEncoding()) if isinstance(data, (tuple, list)): new_data = [] for item in data: if isinstance(item, unicode): item = item.encode(self.getEncoding()) new_data.append(item) data = new_data if isinstance(data, unicode): data = data.encode(self.getEncoding()) if keyword == 'binary_data': #LOG('ERP5Conduit.getFormatedArgs', DEBUG, 'binary_data keyword: %s' % str(keyword)) msg = MIMEBase('application','octet-stream') encoders.encode_base64(msg) msg.set_payload(data) data = msg.get_payload(decode=True) new_args[keyword] = data return new_args
def sendmail(mailto, subject, message, subtype='html', charset='utf-8', smtpconfig=None, attachments={}, use_starttls=False, **headers): ''' Send an email to the given address. Additional SMTP headers may be specified as keyword arguments. ''' if not smtpconfig: # we support both smtp and mail for legacy reasons # smtp is the correct usage. smtpconfig = config.get('smtp') or config.get('mail') # mailto arg is explicit to ensure that it's always set, but it's processed # mostly the same way as all other headers headers['To'] = _string_or_list(mailto) msg = MIMEMultipart('alternative') msg['Subject'] = subject for key, value in six.iteritems(headers): for val in _string_or_list(value): msg.add_header(key, val) text = MIMEText(message, subtype, charset) msg.attach(text) # Add attachments for file_name, file_payload in attachments.items(): part = MIMEBase('application', 'octet-stream') part.set_payload(file_payload.encode(charset)) if part.get_payload() is not None: encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % file_name) msg.attach(part) if not 'From' in msg: msg['From'] = smtpconfig.get('from') mailfrom = msg['From'] assert isinstance(mailfrom, six.string_types) recipients = [] for toheader in ('To', 'CC', 'BCC'): recipients += msg.get_all(toheader, []) if 'BCC' in msg: del msg['BCC'] smtp = smtplib.SMTP(smtpconfig.get('host'), smtpconfig.get('port')) if smtpconfig.get('username', None) is not None and smtpconfig.get( 'password', None) is not None: if use_starttls: smtp.elho() smtp.starttls() smtp.elho() smtp.login(smtpconfig.get('username'), smtpconfig.get('password')) smtp.sendmail(mailfrom, recipients, msg.as_string()) smtp.quit() log.info('Sent email to %s (Subject: %s)', recipients, subject)