def _get_date(self): date = self.hdoc.content.get('date', None) try: if not date: received = self.hdoc.content.get('received', None) if received: date = received.split(";")[-1].strip() else: # we can't get a date for this mail, so lets just use now logger.warning('Encountered a mail with missing date and received header fields. ID %s' % self.fdoc.content.get('uid', None)) date = date.iso_now() return dateparser.parse(date).isoformat() except (ValueError, TypeError): date = date.iso_now() return dateparser.parse(date).isoformat()
def headers(self): _headers = { 'To': [], 'Cc': [], 'Bcc': [] } hdoc_headers = self.hdoc.content['headers'] for header in ['To', 'Cc', 'Bcc']: header_value = self._decode_header(hdoc_headers.get(header)) if not header_value: continue _headers[header] = header_value if type(header_value) is list else header_value.split(',') _headers[header] = [head.strip() for head in compact(_headers[header])] for header in ['From', 'Subject']: _headers[header] = self._decode_header(hdoc_headers.get(header)) try: _headers['Date'] = self._get_date() except Exception: _headers['Date'] = date.iso_now() if self.parts and len(self.parts['alternatives']) > 1: _headers['content_type'] = 'multipart/alternative; boundary="%s"' % self.boundary elif self.hdoc.content['headers'].get('Content-Type'): _headers['content_type'] = hdoc_headers.get('Content-Type') if hdoc_headers.get('Reply-To'): _headers['Reply-To'] = hdoc_headers.get('Reply-To') return _headers
def headers(self): _headers = {"To": [], "Cc": [], "Bcc": []} hdoc_headers = self.hdoc.content["headers"] for header in ["To", "Cc", "Bcc"]: header_value = self._decode_header(hdoc_headers.get(header)) if not header_value: continue _headers[header] = header_value if type(header_value) is list else header_value.split(",") _headers[header] = [head.strip() for head in compact(_headers[header])] for header in ["From", "Subject"]: _headers[header] = self._decode_header(hdoc_headers.get(header)) try: _headers["Date"] = self._get_date() except Exception: _headers["Date"] = date.iso_now() if self.parts and len(self.parts["alternatives"]) > 1: _headers["content_type"] = 'multipart/alternative; boundary="%s"' % self.boundary elif self.hdoc.content["headers"].get("Content-Type"): _headers["content_type"] = hdoc_headers.get("Content-Type") if hdoc_headers.get("Reply-To"): _headers["Reply-To"] = hdoc_headers.get("Reply-To") return _headers
def from_python_mail(mail): input_mail = InputMail() input_mail.headers = {key.capitalize(): value for key, value in mail.items()} input_mail.headers['Date'] = date.iso_now() input_mail.headers['Subject'] = mail['Subject'] input_mail.headers['To'] = InputMail.FROM_EMAIL_ADDRESS input_mail._mime = MIMEMultipart() for payload in mail.get_payload(): input_mail._mime.attach(payload) if payload.get_content_type() == 'text/plain': input_mail.body = payload.as_string() return input_mail
def build_welcome_mail(): current_path = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(current_path, '..', 'assets', 'welcome.mail')) as mail_template_file: mail_template = message_from_file(mail_template_file) welcome_mail = InputMail() welcome_mail.headers['To'] = InputMail.FROM_EMAIL_ADDRESS welcome_mail.headers['Subject'] = mail_template['Subject'] welcome_mail.headers['Date'] = iso_now() welcome_mail._mime = MIMEMultipart() for payload in mail_template.get_payload(): welcome_mail._mime.attach(payload) if payload.get_content_type() == 'text/plain': welcome_mail.body = payload.as_string() return welcome_mail
def from_dict(mail_dict): input_mail = InputMail() input_mail.headers = {key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} # XXX this is overriding the property in PixelatedMail input_mail.headers['Date'] = date.iso_now() # XXX this is overriding the property in PixelatedMail input_mail.body = mail_dict.get('body', '') # XXX this is overriding the property in the PixelatedMail input_mail.tags = set(mail_dict.get('tags', [])) input_mail._status = set(mail_dict.get('status', [])) return input_mail
def from_dict(mail_dict): input_mail = InputMail() input_mail.headers = {key.capitalize(): value for key, value in mail_dict.get("header", {}).items()} # XXX this is overriding the property in PixelatedMail input_mail.headers["Date"] = date.iso_now() # XXX this is overriding the property in PixelatedMail input_mail.body = mail_dict.get("body", "") # XXX this is overriding the property in the PixelatedMail input_mail.tags = set(mail_dict.get("tags", [])) input_mail._status = set(mail_dict.get("status", [])) return input_mail