def attachment(content_type, body, filename=None, disposition=None, charset=None): """Smarter method to build attachments that detects the proper content type and form of the message based on content type string, body and filename of the attachment """ # fix and sanitize content type string and get main and sub parts: main, sub = fix_content_type( content_type, default=('application', 'octet-stream')) # adjust content type based on body or filename if it's not too accurate content_type = adjust_content_type( ContentType(main, sub), body, filename) if content_type.main == 'message': try: message = message_container(from_string(body)) message.headers['Content-Disposition'] = WithParams(disposition) return message except DecodingError: content_type = ContentType('application', 'octet-stream') return binary( content_type.main, content_type.sub, body, filename, disposition, charset, True)
def adjust_content_type(content_type, body=None, filename=None): """Adjust content type based on filename or body contents """ if filename and str(content_type) == 'application/octet-stream': # check if our internal guess returns anything guessed = _guess_type(filename) if guessed: return guessed # our internal attempt didn't return anything, use mimetypes guessed = mimetypes.guess_type(filename)[0] if guessed: main, sub = fix_content_type( guessed, default=('application', 'octet-stream')) content_type = ContentType(main, sub) if content_type.main == 'image' and body: sub = imghdr.what(None, body) if sub: content_type = ContentType('image', sub) elif content_type.main == 'audio' and body: sub = audio._whatsnd(body) if sub: content_type = ContentType('audio', sub) return content_type
def attachment(content_type, body, filename=None, disposition=None, charset=None): """Smarter method to build attachments that detects the proper content type and form of the message based on content type string, body and filename of the attachment """ # fix and sanitize content type string and get main and sub parts: main, sub = fix_content_type( content_type, default=('application', 'octet-stream')) # adjust content type based on body or filename if it's not too accurate content_type = adjust_content_type( ContentType(main, sub), body, filename) if content_type.main == 'message': try: message = message_container(from_string(body)) message.headers['Content-Disposition'] = WithParams(disposition) return message except DecodingError: content_type = ContentType('application', 'octet-stream') return binary( content_type.main, content_type.sub, body, filename, disposition, charset)
def adjust_content_type(content_type, body=None, filename=None): """Adjust content type based on filename or body contents """ if filename and str(content_type) == 'application/octet-stream': # check if our internal guess returns anything guessed = _guess_type(filename) if guessed: return guessed # our internal attempt didn't return anything, use mimetypes guessed = mimetypes.guess_type(filename)[0] if guessed: main, sub = fix_content_type( guessed, default=('application', 'octet-stream')) content_type = ContentType(main, sub) if content_type.main == 'image' and body: image_preamble = body[:32] if six.PY3 and isinstance(body, six.text_type): image_preamble = image_preamble.encode('utf-8', 'ignore') sub = imghdr.what(None, image_preamble) if sub: content_type = ContentType('image', sub) elif content_type.main == 'audio' and body: sub = _email.detect_audio_type(body) if sub: content_type = ContentType('audio', sub) return content_type
def parse_header_value(name, val): if not is_pure_ascii(val): if parametrized.is_parametrized(name, val): # PHOENIX HACK!!! # FIXME: making the function return a completely different type on # parameterized headers is wrong. val = val.decode('utf-8', errors='replace') val = val.split(";", 1) if len(val) == 2: val, params = val rparams = {} for p in params.split(";"): k,v = p.split("=", 1) rparams[k.strip()] = v.split('"')[1] return val, rparams return val[0], {} return to_unicode(val) else: if parametrized.is_parametrized(name, val): val, params = parametrized.decode(val) if name == 'Content-Type': main, sub = parametrized.fix_content_type(val) return ContentType(main, sub, params) else: return WithParams(val, params) else: return val
def parse_header_value(name, val): if parametrized.is_parametrized(name, val): val, params = parametrized.decode(val) if name == 'Content-Type': main, sub = parametrized.fix_content_type(val) return ContentType(main, sub, params) else: return WithParams(val, params) else: return val if is_pure_ascii(val) else to_unicode(val)
def parse_header_value(name, val): if not is_pure_ascii(val): val = to_unicode(val) if parametrized.is_parametrized(name, val): val, params = parametrized.decode(val) if val is not None and not is_pure_ascii(val): raise DecodingError('Non-ascii content header value') if name == 'Content-Type': main, sub = parametrized.fix_content_type(val) return ContentType(main, sub, params) return WithParams(val, params) return val
def parse_header_value(name, val): if not is_pure_ascii(val): if parametrized.is_parametrized(name, val): raise DecodingError("Unsupported value in content- header") return to_unicode(val) else: if parametrized.is_parametrized(name, val): val, params = parametrized.decode(val) if name == 'Content-Type': main, sub = parametrized.fix_content_type(val) return ContentType(main, sub, params) else: return WithParams(val, params) else: return val
def adjust_content_type(content_type, body=None, filename=None): """Adjust content type based on filename or body contents """ if filename and str(content_type) == "application/octet-stream": guessed = mimetypes.guess_type(filename)[0] if guessed: main, sub = fix_content_type(guessed, default=("application", "octet-stream")) content_type = ContentType(main, sub) if content_type.main == "image" and body: sub = imghdr.what(None, body) if sub: content_type = ContentType("image", sub) elif content_type.main == "audio" and body: sub = audio._whatsnd(body) if sub: content_type = ContentType("audio", sub) return content_type
def adjust_content_type(content_type, body=None, filename=None): """Adjust content type based on filename or body contents """ if filename and str(content_type) == 'application/octet-stream': guessed = mimetypes.guess_type(filename)[0] if guessed: main, sub = fix_content_type(guessed, default=('application', 'octet-stream')) content_type = ContentType(main, sub) if content_type.main == 'image' and body: sub = imghdr.what(None, body) if sub: content_type = ContentType('image', sub) elif content_type.main == 'audio' and body: sub = audio._whatsnd(body) if sub: content_type = ContentType('audio', sub) return content_type