本文整理汇总了Python中email.mime.application.MIMEApplication方法的典型用法代码示例。如果您正苦于以下问题:Python application.MIMEApplication方法的具体用法?Python application.MIMEApplication怎么用?Python application.MIMEApplication使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.mime.application
的用法示例。
在下文中一共展示了application.MIMEApplication方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_mail
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def send_mail(self, files):
msg = MIMEMultipart()
msg['From'] = self.user_email
msg['To'] = self.kindle_email
msg['Subject'] = 'Convert' if self.convert else 'Sent to Kindle'
msg.preamble = 'This email has been automatically sent by fb2mobi tool'
for file_path in files:
fname = os.path.basename(file_path)
msg.attach(MIMEApplication(open(file_path, 'rb').read(), Content_Disposition='attachment; filename="%s"' % fname, Name=fname))
mail_server = smtplib.SMTP(host=self.smtp_server, port=self.smtp_port)
mail_server.starttls()
mail_server.login(self.smtp_login, self.smtp_password)
mail_server.sendmail(self.user_email, self.kindle_email, msg.as_string())
mail_server.quit()
示例2: prepare_mail_attachments
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def prepare_mail_attachments(self):
# first find the latest alarm folder
subdirs = []
for directory in os.listdir(self.data_dir):
full_path = os.path.join(self.data_dir, directory)
if os.path.isdir(full_path):
subdirs.append(full_path)
# TODO: check if subdirs is empty
latest_subdir = max(subdirs, key=os.path.getmtime)
logging.debug("Mailer: Will look into %s for data" % latest_subdir)
#then iterate through it and attach all the files to the mail
for file in os.listdir(latest_subdir):
# check if it really is a file
if os.path.isfile("%s/%s" % (latest_subdir, file)):
f = open("%s/%s" % (latest_subdir, file), "rb")
att = MIMEApplication(f.read())
att.add_header('Content-Disposition','attachment; filename="%s"' % file)
f.close()
self.message.attach(att)
logging.debug("Mailer: Attached file '%s' to message" % file)
else:
logging.debug("Mailer: %s is not a file" % file)
# TODO: maybe log something if there are no files?
示例3: testExtractAttachedKey
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def testExtractAttachedKey(self):
KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
message = MIMEMultipart()
message.add_header("from", ADDRESS_2)
key = MIMEApplication("", "pgp-keys")
key.set_payload(KEY)
message.attach(key)
self.fetcher._keymanager.put_raw_key = Mock(
return_value=defer.succeed(None))
def put_raw_key_called(_):
self.fetcher._keymanager.put_raw_key.assert_called_once_with(
KEY, address=ADDRESS_2)
d = self._do_fetch(message.as_string())
d.addCallback(put_raw_key_called)
return d
示例4: testExtractInvalidAttachedKey
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def testExtractInvalidAttachedKey(self):
KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
message = MIMEMultipart()
message.add_header("from", ADDRESS_2)
key = MIMEApplication("", "pgp-keys")
key.set_payload(KEY)
message.attach(key)
self.fetcher._keymanager.put_raw_key = Mock(
return_value=defer.fail(KeyAddressMismatch()))
def put_raw_key_called(_):
self.fetcher._keymanager.put_raw_key.assert_called_once_with(
KEY, address=ADDRESS_2)
d = self._do_fetch(message.as_string())
d.addCallback(put_raw_key_called)
d.addErrback(log.err)
return d
示例5: testExtractAttachedKeyAndNotOpenPGPHeader
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def testExtractAttachedKeyAndNotOpenPGPHeader(self):
KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
KEYURL = "https://leap.se/key.txt"
OpenPGP = "id=12345678; url=\"%s\"; preference=signencrypt" % (KEYURL,)
message = MIMEMultipart()
message.add_header("from", ADDRESS_2)
message.add_header("OpenPGP", OpenPGP)
key = MIMEApplication("", "pgp-keys")
key.set_payload(KEY)
message.attach(key)
self.fetcher._keymanager.put_raw_key = Mock(
return_value=defer.succeed(None))
self.fetcher._keymanager.fetch_key = Mock()
def put_raw_key_called(_):
self.fetcher._keymanager.put_raw_key.assert_called_once_with(
KEY, address=ADDRESS_2)
self.assertFalse(self.fetcher._keymanager.fetch_key.called)
d = self._do_fetch(message.as_string())
d.addCallback(put_raw_key_called)
return d
示例6: test_add_header
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_add_header(self):
eq = self.assertEqual
self._im.add_header('Content-Disposition', 'attachment',
filename='dingusfish.gif')
eq(self._im['content-disposition'],
'attachment; filename="dingusfish.gif"')
eq(self._im.get_params(header='content-disposition'),
[('attachment', ''), ('filename', 'dingusfish.gif')])
eq(self._im.get_param('filename', header='content-disposition'),
'dingusfish.gif')
missing = []
eq(self._im.get_param('attachment', header='content-disposition'), '')
self.assertIs(self._im.get_param('foo', failobj=missing,
header='content-disposition'),
missing)
# Try some missing stuff
self.assertIs(self._im.get_param('foobar', missing), missing)
self.assertIs(self._im.get_param('attachment', missing,
header='foobar'), missing)
# Test the basic MIMEApplication class
示例7: test_binary_body_with_encode_7or8bit
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_binary_body_with_encode_7or8bit(self):
# Issue 17171.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit)
# Treated as a string, this will be invalid code points.
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg.get_payload(decode=True), bytesdata)
self.assertEqual(msg['Content-Transfer-Encoding'], '8bit')
s = StringIO()
g = Generator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_string(wireform)
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit')
示例8: test_binary_body_with_encode_noop
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_binary_body_with_encode_noop(self):
# Issue 16564: This does not produce an RFC valid message, since to be
# valid it should have a CTE of binary. But the below works, and is
# documented as working this way.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop)
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg.get_payload(decode=True), bytesdata)
s = StringIO()
g = Generator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_string(wireform)
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
# Test the basic MIMEText class
示例9: test_add_header
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_add_header(self):
eq = self.assertEqual
unless = self.assertTrue
self._im.add_header('Content-Disposition', 'attachment',
filename='dingusfish.gif')
eq(self._im['content-disposition'],
'attachment; filename="dingusfish.gif"')
eq(self._im.get_params(header='content-disposition'),
[('attachment', ''), ('filename', 'dingusfish.gif')])
eq(self._im.get_param('filename', header='content-disposition'),
'dingusfish.gif')
missing = []
eq(self._im.get_param('attachment', header='content-disposition'), '')
unless(self._im.get_param('foo', failobj=missing,
header='content-disposition') is missing)
# Try some missing stuff
unless(self._im.get_param('foobar', missing) is missing)
unless(self._im.get_param('attachment', missing,
header='foobar') is missing)
# Test the basic MIMEApplication class
示例10: _add_attachment
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def _add_attachment(msg, name, content):
"""Add attachments to the msg
Args:
msg (MIMEMultipart): email to attach too
name (str): name for the file to be attached
content (str): content of the file to be attached (should be string)
Returns:
msg (MIMEMultipart): Email with the relevant attachments
"""
LOGGER.debug("Attaching %s to msg", name)
att = MIMEApplication(content)
att.add_header("Content-Disposition", "attachment", filename=name)
msg.attach(att)
return msg
示例11: test_add_single_attachment
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_add_single_attachment(self):
"""SESOutput - Test single attachment"""
rule_name = 'test_single_attachment'
alert = get_random_alert(10, rule_name, omit_rule_desc=True)
output = MagicMock(spec=SESOutput)
alert_publication = compose_alert(alert, output, self.DESCRIPTOR)
msg = SESOutput._build_email(alert, alert_publication, self.CREDS)
# Verify attachment
payloads = msg.get_payload()
for payload in payloads:
if isinstance(payload, MIMEApplication):
assert_equal(payload.get_filename(), 'record.json')
break
else:
# Raise an error if no payload of type MIMEApplication is found
raise AssertionError
示例12: test_add_multiple_attachments
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_add_multiple_attachments(self):
"""SESOutput - Multiple attachments"""
rule_name = 'test_multiple_attachments'
alert = get_random_alert(10, rule_name, omit_rule_desc=True)
output = MagicMock(spec=SESOutput)
alert_publication = compose_alert(alert, output, self.DESCRIPTOR)
# remove the default record
alert_publication['@aws-ses.attach_record'] = False
attachments = {
'file_one.json': '{"test": true, "foo": "bar"}',
'file_two.json': '{"test": true, "bar": "foo"}'
}
alert_publication['@aws-ses.attachments'] = attachments
msg = SESOutput._build_email(alert, alert_publication, self.CREDS)
# Tests
payloads = msg.get_payload()
for payload in payloads:
if isinstance(payload, MIMEApplication):
assert_true(payload.get_filename() in attachments.keys())
示例13: test_send_smtp
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_send_smtp(self, mock_send_mime):
attachment = tempfile.NamedTemporaryFile()
attachment.write(b'attachment')
attachment.seek(0)
utils.email.send_email_smtp('to', 'subject', 'content', files=[attachment.name])
self.assertTrue(mock_send_mime.called)
call_args = mock_send_mime.call_args[0]
self.assertEqual(conf.get('smtp', 'SMTP_MAIL_FROM'), call_args[0])
self.assertEqual(['to'], call_args[1])
msg = call_args[2]
self.assertEqual('subject', msg['Subject'])
self.assertEqual(conf.get('smtp', 'SMTP_MAIL_FROM'), msg['From'])
self.assertEqual(2, len(msg.get_payload()))
filename = 'attachment; filename="' + os.path.basename(attachment.name) + '"'
self.assertEqual(filename, msg.get_payload()[-1].get('Content-Disposition'))
mimeapp = MIMEApplication('attachment')
self.assertEqual(mimeapp.get_payload(), msg.get_payload()[-1].get_payload())
示例14: test_send_bcc_smtp
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def test_send_bcc_smtp(self, mock_send_mime):
attachment = tempfile.NamedTemporaryFile()
attachment.write(b'attachment')
attachment.seek(0)
utils.email.send_email_smtp('to', 'subject', 'content', files=[attachment.name], cc='cc', bcc='bcc')
self.assertTrue(mock_send_mime.called)
call_args = mock_send_mime.call_args[0]
self.assertEqual(conf.get('smtp', 'SMTP_MAIL_FROM'), call_args[0])
self.assertEqual(['to', 'cc', 'bcc'], call_args[1])
msg = call_args[2]
self.assertEqual('subject', msg['Subject'])
self.assertEqual(conf.get('smtp', 'SMTP_MAIL_FROM'), msg['From'])
self.assertEqual(2, len(msg.get_payload()))
self.assertEqual('attachment; filename="' + os.path.basename(attachment.name) + '"',
msg.get_payload()[-1].get('Content-Disposition'))
mimeapp = MIMEApplication('attachment')
self.assertEqual(mimeapp.get_payload(), msg.get_payload()[-1].get_payload())
示例15: notify
# 需要导入模块: from email.mime import application [as 别名]
# 或者: from email.mime.application import MIMEApplication [as 别名]
def notify(prj_number, prj_path, journal_excerpt, subject="", attachment="", port=25):
mail_server, mail_sender, mail_recipients = fetch_config(prj_number)
if not subject:
subject = "rvt model corrupt!!"
if mail_server and mail_sender and mail_recipients:
mail_text = f"warning - rvt model {prj_number} at path {prj_path} : {subject}! \nsee: {journal_excerpt}"
msg = MIMEMultipart()
msg.attach(MIMEText(mail_text))
msg['From'] = mail_sender
msg['To'] = mail_recipients
msg['Subject'] = f"{prj_number} - {subject}"
if attachment:
with open(attachment, "rb") as csv_file:
part = MIMEApplication(
csv_file.read(),
Name=op.basename(attachment),
)
part['Content-Disposition'] = f'attachment; filename="{op.basename(attachment)}"'
msg.attach(part)
with smtplib.SMTP(f"{mail_server}:{port}") as smtp_con:
smtp_con.send_message(msg)