convert a string (more likely bytes) or a message into a mime message.
(raw_data, content_type=NOT_MULTIPART_TYPE)
| 363 | |
| 364 | # Converts a raw string into a mime message |
| 365 | def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE): |
| 366 | """convert a string (more likely bytes) or a message into |
| 367 | a mime message.""" |
| 368 | if not raw_data: |
| 369 | raw_data = b"" |
| 370 | |
| 371 | def create_binmsg(data, content_type): |
| 372 | maintype, subtype = content_type.split("/", 1) |
| 373 | msg = MIMEBase(maintype, subtype) |
| 374 | msg.set_payload(data) |
| 375 | return msg |
| 376 | |
| 377 | if isinstance(raw_data, str): |
| 378 | bdata = raw_data.encode("utf-8") |
| 379 | else: |
| 380 | bdata = raw_data |
| 381 | bdata = util.decomp_gzip(bdata, decode=False) |
| 382 | if b"mime-version:" in bdata[0:4096].lower(): |
| 383 | msg = util.message_from_string(bdata.decode("utf-8")) |
| 384 | else: |
| 385 | msg = create_binmsg(bdata, content_type) |
| 386 | |
| 387 | return msg |
no test coverage detected