def test_attach_text(): mail = encoding.MailBase() mail.attach_text("This is some text.", 'text/plain') msg = encoding.to_message(mail) assert msg.get_payload(0).get_payload() == "This is some text." assert encoding.to_string(mail) mail.attach_text("<html><body><p>Hi there.</p></body></html>", "text/html") msg = encoding.to_message(mail) assert len(msg.get_payload()) == 2 assert encoding.to_string(mail)
def test_to_message_from_message_with_spam(): mb = mailbox.mbox("tests/spam") fails = 0 total = 0 for msg in mb: try: m = encoding.from_message(msg) out = encoding.to_message(m) assert repr(out) m2 = encoding.from_message(out) for k in m: if '@' in m[k]: assert_equal(parseaddr(m[k]), parseaddr(m2[k])) else: assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (k, m[k], m2[k]) assert not m[k].startswith(u"=?") assert not m2[k].startswith(u"=?") assert m.body == m2.body, "Bodies don't match" assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.") for i, part in enumerate(m.parts): assert part.body == m2.parts[i].body, "Part %d isn't the same: %r \nvs\n. %r" % (i, part.body, m2.parts[i].body) total += 1 except encoding.EncodingError, exc: fails += 1
def test_MailBase(): the_subject = u"p\xf6stal" m = encoding.MailBase() m["To"] = "testing@localhost" m["Subject"] = the_subject m["Content-Type"] = "text/plain; charset=iso-8859-1" m["MIME-Version"] = "1.0" assert m["To"] == "testing@localhost" assert m["TO"] == m["To"] assert m["to"] == m["To"] assert m["Subject"] == the_subject assert m["subject"] == m["Subject"] assert m["sUbjeCt"] == m["Subject"] msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) for k in m.keys(): assert k in m del m[k] assert k not in m
def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' assert m['To'] == "testing@localhost" assert m['TO'] == m['To'] assert m['to'] == m['To'] assert m['Subject'] == the_subject assert m['subject'] == m['Subject'] assert m['sUbjeCt'] == m['Subject'] msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) for k in m.keys(): assert k in m del m[k] assert k not in m
def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject assert m['To'] == "testing@localhost" assert m['TO'] == m['To'] assert m['to'] == m['To'] assert m['Subject'] == the_subject assert m['subject'] == m['Subject'] assert m['sUbjeCt'] == m['Subject'] msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) for k in m.keys(): assert k in m del m[k] assert not k in m
def test_attach_file(): mail = encoding.MailBase() png = open("tests/salmon.png").read() mail.attach_file("salmon.png", png, "image/png", "attachment") msg = encoding.to_message(mail) payload = msg.get_payload(0) assert payload.get_payload(decode=True) == png assert payload.get_filename() == "salmon.png", payload.get_filename()
def test_MIMEPart(): text1 = encoding.MIMEPart("text/plain") text1.set_payload("The first payload.") text2 = encoding.MIMEPart("text/plain") text2.set_payload("The second payload.") image_data = open("tests/salmon.png").read() img1 = encoding.MIMEPart("image/png") img1.set_payload(image_data) img1.set_param('attachment','', header='Content-Disposition') img1.set_param('filename','salmon.png', header='Content-Disposition') encoders.encode_base64(img1) multi = encoding.MIMEPart("multipart/mixed") for x in [text1, text2, img1]: multi.attach(x) mail = encoding.from_message(multi) assert mail.parts[0].body == "The first payload." assert mail.parts[1].body == "The second payload." assert mail.parts[2].body == image_data encoding.to_message(mail)
def test_MIMEPart(): text1 = encoding.MIMEPart("text/plain") text1.set_payload("The first payload.") text2 = encoding.MIMEPart("text/plain") text2.set_payload("The second payload.") image_data = open("tests/salmon.png").read() img1 = encoding.MIMEPart("image/png") img1.set_payload(image_data) img1.set_param('attachment', '', header='Content-Disposition') img1.set_param('filename', 'salmon.png', header='Content-Disposition') encoders.encode_base64(img1) multi = encoding.MIMEPart("multipart/mixed") for x in [text1, text2, img1]: multi.attach(x) mail = encoding.from_message(multi) assert mail.parts[0].body == "The first payload." assert mail.parts[1].body == "The second payload." assert mail.parts[2].body == image_data encoding.to_message(mail)
def test_to_message_from_message_with_spam(): mb = mailbox.mbox("tests/spam") for msg in mb: m = encoding.from_message(msg) out = encoding.to_message(m) assert repr(out) m2 = encoding.from_message(out) for k in m: if '@' in m[k]: assert_equal(parseaddr(m[k]), parseaddr(m2[k])) elif k.lower() in [key.lower() for key in encoding.CONTENT_ENCODING_KEYS]: pass # skip! else: assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (k, m[k], m2[k]) assert not m[k].startswith(u"=?") assert not m2[k].startswith(u"=?") # salmon adds stuff to content headers that's missing from example messages for k in encoding.CONTENT_ENCODING_KEYS: if k == "Content-Transfer-Encoding" \ and m.content_encoding[k][0] is None \ and m2.content_encoding[k][0] == "7bit": continue # salmon fixing bad email elif k == "Mime-Version" \ and m.content_encoding[k][0] is None \ and m2.content_encoding[k][0] == "1.0": continue # again, salmon fixing bad email assert m.content_encoding[k][0] == m2.content_encoding[k][0], \ "%s: %r != %r" % (k, m.content_encoding[k], m2.content_encoding[k]) for p in m.content_encoding[k][1]: if p in m2.content_encoding[k][1]: assert m.content_encoding[k][1][p].lower() == m2.content_encoding[k][1][p].lower(), \ "%s: %r != %r" % (p, m.content_encoding[k][1][p], m2.content_encoding[k][1][p]) assert m.body == m2.body, "Bodies don't match" assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.") for i, part in enumerate(m.parts): assert part.body == m2.parts[i].body, \ "Part %d isn't the same: %r \nvs\n. %r" % (i, part.body, m2.parts[i].body)
def test_content_encoding_headers_are_maintained(): inmail = encoding.from_file(open("tests/signed.msg")) ctype, ctype_params = inmail.content_encoding['Content-Type'] assert_equal(ctype, 'multipart/signed') # these have to be maintained for key in ['protocol', 'micalg']: assert key in ctype_params # these get removed for key in encoding.CONTENT_ENCODING_REMOVED_PARAMS: assert key not in ctype_params outmsg = encoding.to_message(inmail) ctype, ctype_params = encoding.parse_parameter_header(outmsg, 'Content-Type') for key in ['protocol', 'micalg']: assert key in ctype_params, key
def test_to_message_from_message_headers(self): for msg in self.mailbox: m = encoding.from_message(msg) out = encoding.to_message(m) assert repr(out) m2 = encoding.from_message(out) for k in m: if '@' in m[k]: self.assertEqual(parseaddr(m[k]), parseaddr(m2[k])) elif k in encoding.CONTENT_ENCODING_KEYS: pass # skip! else: assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % ( k, m[k], m2[k]) assert not m[k].startswith("=?") assert not m2[k].startswith("=?")
def test_to_message_from_message_with_spam(): raise SkipTest # skip this mb = mailbox.mbox("tests/spam") fails = 0 total = 0 for msg in mb: try: m = encoding.from_message(msg) out = encoding.to_message(m) assert repr(out) m2 = encoding.from_message(out) for k in m: if '@' in m[k]: assert_equal(parseaddr(m[k]), parseaddr(m2[k])) else: assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % ( k, m[k], m2[k]) assert not m[k].startswith(u"=?") assert not m2[k].startswith(u"=?") assert m.body == m2.body, "Bodies don't match" assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.") for i, part in enumerate(m.parts): assert part.body == m2.parts[ i].body, "Part %d isn't the same: %r \nvs\n. %r" % ( i, part.body, m2.parts[i].body) total += 1 except encoding.EncodingError: fails += 1 assert fails / total < 0.01, "There were %d failures out of %d total." % ( fails, total)
def to_message(self): """ Figures out all the required steps to finally craft the message you need and return it. The resulting message is also available as a self.base attribute. What is returned is a Python email API message you can use with those APIs. The self.base attribute is the raw salmon.encoding.MailBase. """ del self.base.parts[:] if self.Body and self.Html: self.multipart = True self.base.content_encoding['Content-Type'] = ( 'multipart/alternative', {}) if self.multipart: self.base.body = None if self.Body: self.base.attach_text(self.Body, 'text/plain') if self.Html: self.base.attach_text(self.Html, 'text/html') for args in self.attachments: self._encode_attachment(**args) elif self.Body: self.base.body = self.Body self.base.content_encoding['Content-Type'] = ('text/plain', {}) elif self.Html: self.base.body = self.Html self.base.content_encoding['Content-Type'] = ('text/html', {}) return encoding.to_message(self.base)
def test_to_message_from_message_with_spam(): raise SkipTest # skip this mb = mailbox.mbox("tests/spam") fails = 0 total = 0 for msg in mb: try: m = encoding.from_message(msg) out = encoding.to_message(m) assert repr(out) m2 = encoding.from_message(out) for k in m: if "@" in m[k]: assert_equal(parseaddr(m[k]), parseaddr(m2[k])) else: assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (k, m[k], m2[k]) assert not m[k].startswith(u"=?") assert not m2[k].startswith(u"=?") assert m.body == m2.body, "Bodies don't match" assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.") for i, part in enumerate(m.parts): assert part.body == m2.parts[i].body, "Part %d isn't the same: %r \nvs\n. %r" % ( i, part.body, m2.parts[i].body, ) total += 1 except encoding.EncodingError: fails += 1 assert fails / total < 0.01, "There were %d failures out of %d total." % (fails, total)
def to_message(self): """ Figures out all the required steps to finally craft the message you need and return it. The resulting message is also available as a self.base attribute. What is returned is a Python email API message you can use with those APIs. The self.base attribute is the raw salmon.encoding.MailBase. """ del self.base.parts[:] if self.Body and self.Html: self.multipart = True self.base.content_encoding['Content-Type'] = ('multipart/alternative', {}) if self.multipart: self.base.body = None if self.Body: self.base.attach_text(self.Body, 'text/plain') if self.Html: self.base.attach_text(self.Html, 'text/html') for args in self.attachments: self._encode_attachment(**args) elif self.Body: self.base.body = self.Body self.base.content_encoding['Content-Type'] = ('text/plain', {}) elif self.Html: self.base.body = self.Html self.base.content_encoding['Content-Type'] = ('text/html', {}) return encoding.to_message(self.base)
def test_to_message_encoding_error(mp_init): mp_init.side_effect = raises_TypeError test = encoding.from_file(open("tests/borked.msg")) msg = encoding.to_message(test)
def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' assert_equal(m['To'], "testing@localhost") assert_equal(m['TO'], m['To']) assert_equal(m['to'], m['To']) assert_equal(m['Subject'], the_subject) assert_equal(m['subject'], m['Subject']) assert_equal(m['sUbjeCt'], m['Subject']) msg = encoding.to_message(m) m2 = encoding.from_message(msg) assert_equal(len(m), len(m2)) assert_equal(m.items(), m2.items()) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) assert("To" in m) assert("Bob" not in m) assert_equal(m["To"], "testing@localhost") m["To"] = "somebody@localhost" assert_equal(m["To"], "somebody@localhost") assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To']) assert_equal(m.items(), [ ('Subject', the_subject), ('Mime-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('To', 'somebody@localhost')]) assert_equal(m.get_all("Subject"), [the_subject]) assert_equal(m.get_all("Bob"), []) # appending headers m.append_header("subject", "something else") assert_equal(m["Subject"], the_subject) assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To', "Subject"]) assert_equal(m.items(), [ ('Subject', the_subject), ('Mime-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('To', 'somebody@localhost'), ('Subject', "something else")]) assert_equal(m.get_all("Subject"), [the_subject, "something else"]) # reset subject header m["Subject"] = the_subject for key, item in zip(m.keys(), m.items()): assert_equal(key, item[0]) assert key in m del m[key] assert key not in m assert key not in [i[0] for i in m.items()]
def to_message(self): """ Converts this to a Python email message you can use to interact with the python mail APIs. """ return encoding.to_message(self.base)
def test_to_message_encoding_error(mp_init): mp_init.side_effect = raises_TypeError test = encoding.from_file(open("tests/borked.msg", "rb")) encoding.to_message(test)
def to_message(self): """ Converts this to a Python email message you can use to interact with the python mail APIs. """ return encoding.to_message(self.Email)
def test_to_message_encoding_error(self, mp_init): mp_init.side_effect = TypeError with open("tests/data/borked.msg", "rb") as file_obj: test = encoding.from_file(file_obj) with self.assertRaises(encoding.EncodingError): encoding.to_message(test)
def test_MailBase(self): the_subject = 'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' self.assertEqual(m['To'], "testing@localhost") self.assertEqual(m['TO'], m['To']) self.assertEqual(m['to'], m['To']) self.assertEqual(m['Subject'], the_subject) self.assertEqual(m['subject'], m['Subject']) self.assertEqual(m['sUbjeCt'], m['Subject']) msg = encoding.to_message(m) m2 = encoding.from_message(msg) self.assertEqual(len(m), len(m2)) self.assertEqual(m.items(), m2.items()) for k in m: assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k]) assert ("To" in m) assert ("Bob" not in m) self.assertEqual(m["To"], "testing@localhost") m["To"] = "somebody@localhost" self.assertEqual(m["To"], "somebody@localhost") self.assertEqual(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To']) self.assertEqual(m.items(), [('Subject', the_subject), ('Mime-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('To', 'somebody@localhost')]) self.assertEqual(m.get_all("Subject"), [the_subject]) self.assertEqual(m.get_all("Bob"), []) # appending headers m.append_header("subject", "something else") self.assertEqual(m["Subject"], the_subject) self.assertEqual( m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To', "Subject"]) self.assertEqual(m.items(), [('Subject', the_subject), ('Mime-Version', '1.0'), ('Content-Type', 'text/plain; charset="iso-8859-1"'), ('To', 'somebody@localhost'), ('Subject', "something else")]) self.assertEqual(m.get_all("Subject"), [the_subject, "something else"]) # reset subject header m["Subject"] = the_subject for key, item in zip(m.keys(), m.items()): self.assertEqual(key, item[0]) assert key in m del m[key] assert key not in m assert key not in [i[0] for i in m.items()]