def __addRecipients(self, recipients): returnRecipients = [] if recipients: for recipient in recipients: email = '' if 'email' in recipient: email = recipient['email'] if 'name' in recipient: name = recipient['name'] else: name = email address = Address(name, email).__str__() if 'smtpAddressForceFormat' in self.configuration and self.configuration[ 'smtpAddressForceFormat'] is True: address = address.replace('<"', '<').replace('">', '>').strip() returnRecipients.append(address) return ', '.join(returnRecipients)
def __emailData(self, data): emailSubject = data["subject"] emailBody = data["body"] # prepare message msg = MIMEMultipart('alternative') if "to" in data: msg['To'] = self.__addRecipients(data["to"]) if "cc" in data: msg['Cc'] = self.__addRecipients(data["cc"]) if "bcc" in data: msg['Bcc'] = self.__addRecipients(data["bcc"]) smtpFromName = self.configuration["smtpFromName"] smtpFromEmail = self.configuration["smtpFromEmail"] if 'from' in data: if 'email' in data['from']: smtpFromEmail = data['from']['email'] if 'name' in data['from']: smtpFromName = data['from']['name'] else: smtpFromName = smtpFromEmail smtpFrom = Address(smtpFromName, smtpFromEmail).__str__().strip() if 'smtpAddressForceFormat' in self.configuration and self.configuration[ 'smtpAddressForceFormat'] is True: smtpFrom = smtpFrom.replace('<"', '<').replace('">', '>').strip() if smtpFrom.endswith('<>'): smtpFrom = smtpFrom.rstrip('<>').strip() msg['From'] = smtpFrom msg['Subject'] = emailSubject smtpCustomHeaders = {} if 'smtpCustomHeaders' in self.configuration and self.configuration[ 'smtpCustomHeaders'] and isinstance( self.configuration['smtpCustomHeaders'], dict): for headerKey in self.configuration['smtpCustomHeaders']: if headerKey and headerKey.strip() and headerKey.strip( ) in self.configuration['smtpCustomHeaders']: headerValue = self.configuration['smtpCustomHeaders'][ headerKey] if headerValue and headerValue.strip(): smtpCustomHeaders[headerKey] = headerValue if 'replyTo' in data: smtpCustomHeaders['Reply-To'] = data['replyTo'] if 'returnPath' in data: smtpCustomHeaders['Return-Path'] = data['returnPath'] elif 'replyTo' in data: smtpCustomHeaders['Return-Path'] = data['replyTo'] if 'userAgent' in data and data['userAgent'] and data[ 'userAgent'].strip(): smtpCustomHeaders['User-Agent'] = data['userAgent'] elif 'smtpUserAgent' in self.configuration and self.configuration[ 'smtpUserAgent'] and self.configuration['smtpUserAgent'].strip( ): smtpCustomHeaders['User-Agent'] = self.configuration[ 'smtpUserAgent'] if 'customHeaders' in data and data['customHeaders'] and isinstance( data['customHeaders'], dict): for headerKey in data['customHeaders']: if headerKey and headerKey.strip() and headerKey.strip( ) in data['customHeaders']: headerValue = data['customHeaders'][headerKey] if headerValue and headerValue.strip(): smtpCustomHeaders[headerKey] = headerValue for smtpCustomHeaderKey, smtpCustomHeaderValue in smtpCustomHeaders.items( ): msg.add_header(smtpCustomHeaderKey, smtpCustomHeaderValue) if 'plain' in emailBody or 'html' in emailBody: if 'plain' in emailBody: plain = MIMEText(emailBody['plain'], 'plain') msg.attach(plain) if 'html' in emailBody: html = MIMEText(emailBody['html'], 'html') msg.attach(html) elif 'boundary' in emailBody and 'payload' in emailBody: msg.set_boundary(emailBody['boundary']) msg.set_payload(emailBody['payload']) else: plain = MIMEText(emailBody, 'plain') msg.attach(plain) return self.__sendSmtp(msg)