def test_encode_7bit(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_7or8bit) eq(msg.get_payload(), 'hello world\n') eq(msg['content-transfer-encoding'], '7bit') msg = MIMEText('hello \x7f world', _encoder=Encoders.encode_7or8bit) eq(msg.get_payload(), 'hello \x7f world\n') eq(msg['content-transfer-encoding'], '7bit')
def send_email(from_addr, to_addrs, subject, message_body): """ Simple email sending wrapper, use this so we can capture messages for unit testing purposes. Args: - from_addr: address you're sending the email from - to_addrs: list of recipient email addresses - subject: subject of the email - message_body: email body text """ if common.TESTS_ENABLED or mg_globals.app_config['email_debug_mode']: mhost = FakeMhost() elif not mg_globals.app_config['email_debug_mode']: if mg_globals.app_config['email_smtp_use_ssl']: smtp_init = smtplib.SMTP_SSL else: smtp_init = smtplib.SMTP mhost = smtp_init( mg_globals.app_config['email_smtp_host'], mg_globals.app_config['email_smtp_port']) # SMTP.__init__ Issues SMTP.connect implicitly if host if not mg_globals.app_config['email_smtp_host']: # e.g. host = '' mhost.connect() # We SMTP.connect explicitly try: mhost.starttls() except smtplib.SMTPException: # Only raise an exception if we're forced to if mg_globals.app_config['email_smtp_force_starttls']: six.reraise(*sys.exc_info()) if ((not common.TESTS_ENABLED) and (mg_globals.app_config['email_smtp_user'] or mg_globals.app_config['email_smtp_pass'])): mhost.login( mg_globals.app_config['email_smtp_user'], mg_globals.app_config['email_smtp_pass']) message = MIMEText(message_body.encode('utf-8'), 'plain', 'utf-8') message['Subject'] = subject message['From'] = from_addr message['To'] = ', '.join(to_addrs) if common.TESTS_ENABLED: EMAIL_TEST_INBOX.append(message) elif mg_globals.app_config['email_debug_mode']: print u"===== Email =====" print u"From address: %s" % message['From'] print u"To addresses: %s" % message['To'] print u"Subject: %s" % message['Subject'] print u"-- Body: --" print message.get_payload(decode=True) return mhost.sendmail(from_addr, to_addrs, message.as_string())
def send_email(from_addr, to_addrs, subject, message_body): """ Simple email sending wrapper, use this so we can capture messages for unit testing purposes. Args: - from_addr: address you're sending the email from - to_addrs: list of recipient email addresses - subject: subject of the email - message_body: email body text """ if common.TESTS_ENABLED or mg_globals.app_config['email_debug_mode']: mhost = FakeMhost() elif not mg_globals.app_config['email_debug_mode']: if mg_globals.app_config['email_smtp_use_ssl']: smtp_init = smtplib.SMTP_SSL else: smtp_init = smtplib.SMTP mhost = smtp_init(mg_globals.app_config['email_smtp_host'], mg_globals.app_config['email_smtp_port']) # SMTP.__init__ Issues SMTP.connect implicitly if host if not mg_globals.app_config['email_smtp_host']: # e.g. host = '' mhost.connect() # We SMTP.connect explicitly try: mhost.starttls() except smtplib.SMTPException: # Only raise an exception if we're forced to if mg_globals.app_config['email_smtp_force_starttls']: six.reraise(*sys.exc_info()) if ((not common.TESTS_ENABLED) and (mg_globals.app_config['email_smtp_user'] or mg_globals.app_config['email_smtp_pass'])): mhost.login(mg_globals.app_config['email_smtp_user'], mg_globals.app_config['email_smtp_pass']) message = MIMEText(message_body.encode('utf-8'), 'plain', 'utf-8') message['Subject'] = subject message['From'] = from_addr message['To'] = ', '.join(to_addrs) if common.TESTS_ENABLED: EMAIL_TEST_INBOX.append(message) elif mg_globals.app_config['email_debug_mode']: print u"===== Email =====" print u"From address: %s" % message['From'] print u"To addresses: %s" % message['To'] print u"Subject: %s" % message['Subject'] print u"-- Body: --" print message.get_payload(decode=True) return mhost.sendmail(from_addr, to_addrs, message.as_string())
def send_email(from_addr, to_addrs, subject, message_body): """ Simple email sending wrapper, use this so we can capture messages for unit testing purposes. Args: - from_addr: address you're sending the email from - to_addrs: list of recipient email addresses - subject: subject of the email - message_body: email body text """ if common.TESTS_ENABLED or mg_globals.app_config["email_debug_mode"]: mhost = FakeMhost() elif not mg_globals.app_config["email_debug_mode"]: mhost = smtplib.SMTP(mg_globals.app_config["email_smtp_host"], mg_globals.app_config["email_smtp_port"]) # SMTP.__init__ Issues SMTP.connect implicitly if host if not mg_globals.app_config["email_smtp_host"]: # e.g. host = '' mhost.connect() # We SMTP.connect explicitly if (not common.TESTS_ENABLED) and ( mg_globals.app_config["email_smtp_user"] or mg_globals.app_config["email_smtp_pass"] ): mhost.login(mg_globals.app_config["email_smtp_user"], mg_globals.app_config["email_smtp_pass"]) message = MIMEText(message_body.encode("utf-8"), "plain", "utf-8") message["Subject"] = subject message["From"] = from_addr message["To"] = ", ".join(to_addrs) if common.TESTS_ENABLED: EMAIL_TEST_INBOX.append(message) elif mg_globals.app_config["email_debug_mode"]: print u"===== Email =====" print u"From address: %s" % message["From"] print u"To addresses: %s" % message["To"] print u"Subject: %s" % message["Subject"] print u"-- Body: --" print message.get_payload(decode=True) return mhost.sendmail(from_addr, to_addrs, message.as_string())
def send_email(from_addr, to_addrs, subject, message_body): """ Simple email sending wrapper, use this so we can capture messages for unit testing purposes. Args: - from_addr: address you're sending the email from - to_addrs: list of recipient email addresses - subject: subject of the email - message_body: email body text """ # TODO: make a mock mhost if testing is enabled if TESTS_ENABLED or mg_globals.email_debug_mode: mhost = FakeMhost() elif not mg_globals.email_debug_mode: mhost = smtplib.SMTP() mhost.connect() message = MIMEText(message_body.encode('utf-8'), 'plain', 'utf-8') message['Subject'] = subject message['From'] = from_addr message['To'] = ', '.join(to_addrs) if TESTS_ENABLED: EMAIL_TEST_INBOX.append(message) if getattr(mg_globals, 'email_debug_mode', False): print u"===== Email =====" print u"From address: %s" % message['From'] print u"To addresses: %s" % message['To'] print u"Subject: %s" % message['Subject'] print u"-- Body: --" print message.get_payload(decode=True) return mhost.sendmail(from_addr, to_addrs, message.as_string())
class TestMIMEText(unittest.TestCase): def setUp(self): self._msg = MIMEText('hello there') def test_types(self): eq = self.assertEqual unless = self.failUnless eq(self._msg.get_type(), 'text/plain') eq(self._msg.get_param('charset'), 'us-ascii') missing = [] unless(self._msg.get_param('foobar', missing) is missing) unless(self._msg.get_param('charset', missing, header='foobar') is missing) def test_payload(self): self.assertEqual(self._msg.get_payload(), 'hello there\n') self.failUnless(not self._msg.is_multipart())
class TestMIMEText(unittest.TestCase): def setUp(self): self._msg = MIMEText('hello there') def test_types(self): eq = self.assertEqual unless = self.failUnless eq(self._msg.get_type(), 'text/plain') eq(self._msg.get_param('charset'), 'us-ascii') missing = [] unless(self._msg.get_param('foobar', missing) is missing) unless( self._msg.get_param('charset', missing, header='foobar') is missing ) def test_payload(self): self.assertEqual(self._msg.get_payload(), 'hello there\n') self.failUnless(not self._msg.is_multipart())
def update_launch_template(self): basesize = str(self.disk_size) + 'G' startup_script = Template( self.startup_script_template).substitute(basesize=basesize) payload = MIMEText(startup_script, 'cloud-boothook') mime = MIMEMultipart() mime.attach(payload) user_data = MIMEText(mime.as_string(), 'plain') encoders.encode_base64(user_data) user_data_base64 = user_data.get_payload() path = os.path.join(os.path.dirname(__file__), 'AWS/launch-template-data.json') with open(path) as f: data = json.load(f) data['LaunchTemplateData']['BlockDeviceMappings'][0]['Ebs'][ 'VolumeSize'] = self.disk_size data['LaunchTemplateData']['UserData'] = user_data_base64 subprocess.call([ 'aws', 'ec2', '--region', self.conf[utils.PLATFORM]['regions'], 'create-launch-template', '--cli-input-json', json.dumps(data) ])
# Copyright (C) 2001 Python Software Foundation
def test_encode_quoted_printable(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_quopri) eq(msg.get_payload(), 'hello=20world\n') eq(msg['content-transfer-encoding'], 'quoted-printable')
def test_encode_base64(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_base64) eq(msg.get_payload(), 'aGVsbG8gd29ybGQK\n') eq(msg['content-transfer-encoding'], 'base64')
def test_encode_noop(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_noop) eq(msg.get_payload(), 'hello world\n') eq(msg['content-transfer-encoding'], None)