def test_craft_from_sample(): list_name = "test.list" user_full_address = "tester@localhost" sample = mail.MailResponse(To=list_name + "@localhost", From=user_full_address, Subject="Test message with attachments.", Body="The body as one attachment.") sample.update({"Test": "update"}) sample.attach(filename="tests/lamson_tests/message_tests.py", content_type="text/plain", disposition="attachment") inmsg = mail.MailRequest("fakepeer", None, None, str(sample)) assert "Test" in sample.keys() for part in inmsg.to_message().walk(): assert part.get_payload(), "inmsg busted." outmsg = mail.MailResponse(To=inmsg['from'], From=inmsg['to'], Subject=inmsg['subject']) outmsg.attach_all_parts(inmsg) result = outmsg.to_message() for part in result.walk(): assert part.get_payload(), "outmsg parts don't have payload."
def test_mail_response_html_and_plain_text(): sample = mail.MailResponse(To="receiver@localhost", Subject="Test message", From="sender@localhost", Html="<html><body><p>Hi there.</p></body></html>", Body="Test from test_mail_response_html_and_plain_text.") return sample
def test_mail_response_attachments(): sample = mail.MailResponse(To="receiver@localhost", Subject="Test message", From="sender@localhost", Body="Test from test_mail_response_attachments.") readme_data = open("./README.md").read() assert_raises(AssertionError, sample.attach, filename="./README.md", disposition="inline") sample.attach(filename="./README.md", content_type="text/plain", disposition="inline") assert len(sample.attachments) == 1 assert sample.multipart msg = sample.to_message() assert_equal(len(msg.get_payload()), 2) sample.clear() assert len(sample.attachments) == 0 assert not sample.multipart sample.attach(data=readme_data, filename="./README.md", content_type="text/plain") msg = sample.to_message() assert_equal(len(msg.get_payload()), 2) sample.clear() sample.attach(data=readme_data, content_type="text/plain") msg = sample.to_message() assert_equal(len(msg.get_payload()), 2) return sample
def respond(self, variables, content, **kwd): """ Works like lamson.view.respond letting you craft a lamson.mail.MailResponse immediately from the results of a lamson.html.HtmlMail.render call. Simply pass in the From, To, and Subject parameters you would normally pass in for MailResponse, and it'll craft the HTML mail for you and return it ready to deliver. A slight convenience in this function is that if the Body kw parameter equals the content parameter, then it's assumed you want the raw markdown content to be sent as the text version, and it will produce a nice dual HTML/text email. """ assert content, "You must give a contents template." if kwd.get('Body', None) == content: kwd['Body'] = view.render(variables, content) for key in kwd: kwd[key] = kwd[key] % variables msg = mail.MailResponse(**kwd) msg.Html = self.render(variables, content) return msg
def craft_response(message, From, To, contact_addr=None): response = mail.MailResponse(To=To, From=From, Subject=message['subject']) msg_id = message['message-id'] if contact_addr: response.update({ "Sender": contact_addr, "Reply-To": contact_addr, "Return-Path": contact_addr, "Precedence": "list", }) if 'date' in message: response['Date'] = message['date'] if 'references' in message: response['References'] = message['References'] elif msg_id: response['References'] = msg_id if msg_id: response['message-id'] = msg_id if 'in-reply-to' not in message: response["In-Reply-To"] = message['Message-Id'] if message.all_parts(): response.attach_all_parts(message) else: response.Body = message.body() return response
def test_mail_response_html(): sample = mail.MailResponse( To="receiver@localhost", Subject="Test message", From="sender@localhost", Html="<html><body><p>From test_mail_response_html</p></body></html>") return sample
def test_mail_response_mailing_list_headers(): list_addr = "test.users@localhost" msg = mail.MailResponse(From='somedude@localhost', To=list_addr, Subject='subject', Body="Mailing list reply.") print repr(msg) msg["Sender"] = list_addr msg["Reply-To"] = list_addr msg["List-Id"] = list_addr msg["Return-Path"] = list_addr msg["In-Reply-To"] = 'Message-Id-1231123' msg["References"] = 'Message-Id-838845854' msg["Precedence"] = 'list' data = str(msg) req = mail.MailRequest('localhost', 'somedude@localhost', list_addr, data) headers = [ 'Sender', 'Reply-To', 'List-Id', 'Return-Path', 'In-Reply-To', 'References', 'Precedence' ] for header in headers: assert msg[header] == req[header] # try a delete del msg['Precedence']
def test_HtmlMail_content_type_respected(): generator = html.HtmlMail("style.css", "html_test.html", {}) resp = generator.respond({}, "content.markdown", From="somedude@localhost", To="somedude@localhost", Subject="Test of an HTML mail.", Body="content.markdown") req = mail.MailRequest('fakepeer', None, None, str(resp)) assert_equal(req.base.content_encoding['Content-Type'][0], 'multipart/alternative') resp2 = mail.MailResponse(To=req['to'], From=req['from'], Subject=req['subject']) resp2.attach_all_parts(req) assert_equal(resp2.base.content_encoding['Content-Type'], resp.base.content_encoding['Content-Type']) assert_equal(resp2.base.content_encoding['Content-Type'][0], 'multipart/alternative') req2 = mail.MailRequest('fakepeer', None, None, str(resp2)) assert_equal(resp2.base.content_encoding['Content-Type'][0], 'multipart/alternative') assert_equal(req2.base.content_encoding['Content-Type'][0], 'multipart/alternative')
def send(self, To, From, Subject, Body): """ Does what it says, sends an email. If you need something more complex then look at lamson.mail.MailResponse. """ msg = mail.MailResponse(To=To, From=From, Subject=Subject, Body=Body) self.deliver(msg)
def send_command(port=8825, host='127.0.0.1', username=False, password=False, ssl=False, starttls=False, debug=1, sender=None, to=None, subject=None, body=None, attach=False): """ Sends an email to someone as a test message. See the sendmail command for a sendmail replacement. lamson send -port 8825 -host 127.0.0.1 -debug 1 \\ -sender EMAIL -to EMAIL -subject STR -body STR -attach False' There is also a username, password, and starttls option for those who need it. """ message = mail.MailResponse(From=sender, To=to, Subject=subject, Body=body) if attach: message.attach(attach) if username == False: username = None if password == False: password = None relay = server.Relay(host, port=port, username=username, password=password, ssl=ssl, starttls=starttls, debug=debug) relay.deliver(message)
def deliver(self, To, From, Subject, Body): """Overrides TestConversation.deliver to do it internally.""" sample = mail.MailResponse(From=From, To=To, Subject=Subject, Body=Body) msg = mail.MailRequest('localhost', sample['From'], sample['To'], str(sample)) routing.Router.deliver(msg)
def test_sendmail_command(): sys.stdin.read.function() msg = mail.MailResponse(To="tests@localhost", From="tests@localhost", Subject="Hello", Body="Test body.") sys.stdin.read.return_value = str(msg) commands.sendmail_command(port=8899)
def test_get(): q = test_push() msg = mail.MailResponse(To="test@localhost", From="test@localhost", Subject="Test", Body="Test") key = q.push(str(msg)) assert key, "Didn't get a key for test_get push." msg = q.get(key) assert msg, "Didn't get a message for key %r" % key
def test_copy_parts(): bm = mail.MailRequest(None,None,None, open("tests/bounce.msg").read()) resp = mail.MailResponse(To=bm['to'], From=bm['from'], Subject=bm['subject']) resp.attach_all_parts(bm) resp = resp.to_message() bm = bm.to_message() assert_equal(len([x for x in bm.walk()]), len([x for x in resp.walk()]))
def test_remove(): q = test_push() msg = mail.MailResponse(To="test@localhost", From="test@localhost", Subject="Test", Body="Test") key = q.push(str(msg)) assert key, "Didn't get a key for test_get push." assert q.count() == 2, "Wrong count %d should be 2" % q.count() q.remove(key) assert q.count() == 1, "Wrong count %d should be 1" % q.count()
def test_push(): q = queue.Queue("run/queue", safe=USE_SAFE) q.clear() # the queue doesn't really care if its a request or response, as long # as the object answers to str(msg) msg = mail.MailResponse(To="test@localhost", From="test@localhost", Subject="Test", Body="Test") key = q.push(msg) assert key, "Didn't get a key for test_get push." return q
def respond(variables, Body=None, Html=None, **kwd): """ Does the grunt work of cooking up a MailResponse that's based on a template. The only difference from the lamson.mail.MailResponse class and this (apart from variables passed to a template) are that instead of giving actual Body or Html parameters with contents, you give the name of a template to render. The kwd variables are the remaining keyword arguments to MailResponse of From/To/Subject. For example, to render a template for the body and a .html for the Html attachment, and to indicate the From/To/Subject do this: msg = view.respond(locals(), Body='template.txt', Html='template.html', From='*****@*****.**', To='*****@*****.**', Subject='Test body from "%(dude)s".') In this case you're using locals() to gather the variables needed for the 'template.txt' and 'template.html' templates. Each template is setup to be a text/plain or text/html attachment. The From/To/Subject are setup as needed. Finally, the locals() are also available as simple Python keyword templates in the From/To/Subject so you can pass in variables to modify those when needed (as in the %(dude)s in Subject). """ assert Body or Html, "You need to give either the Body or Html template of the mail." for key in kwd: kwd[key] = kwd[key] % variables msg = mail.MailResponse(**kwd) if Body: msg.Body = render(variables, Body) if Html: msg.Html = render(variables, Html) return msg
def test_mail_response_plain_text(): sample = mail.MailResponse(To="receiver@localhost", Subject="Test message", From="sender@localhost", Body="Test from test_mail_response_plain_text.") return sample
def test_msg_is_deprecated(): warnings.simplefilter("ignore") msg = mail.MailRequest(None, None, None, "") assert_equal(msg.msg, msg.base) resp = mail.MailResponse() assert_equal(resp.msg, resp.base)
from lamson.testing import * from lamson import mail from config import settings from app.model import filter, addressing host = 'myinboxisnota.tv' user = '******' user_anon_addr = addressing.mapping(user, 'user', host) marketroid = '*****@*****.**' mk_anon_addr = addressing.mapping(marketroid, 'marketroid', host) user_id = user_anon_addr.split('@')[0] marketroid_id = mk_anon_addr.split('@')[0] from_marketroid = mail.MailResponse( From=marketroid, To=user_anon_addr, Subject="Buy my crap!", Html="<html></body>You should buy this!</body></html>") from_user = mail.MailResponse(From=user, To=mk_anon_addr, Subject="No thanks.", Body="Sorry but I'd rather not.") def setup(): addressing.store(user_id, user) addressing.store(marketroid_id, marketroid) addressing.store(marketroid, marketroid_id) def teardown():