本文整理匯總了Python中email.mime.image.MIMEImage方法的典型用法代碼示例。如果您正苦於以下問題:Python image.MIMEImage方法的具體用法?Python image.MIMEImage怎麽用?Python image.MIMEImage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類email.mime.image
的用法示例。
在下文中一共展示了image.MIMEImage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_send_smtp_inline_images
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test_send_smtp_inline_images(self, mock_send_mime):
image = read_fixture("sample.png")
utils.send_email_smtp(
"to", "subject", "content", app.config, images=dict(blah=image)
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
logger.debug(call_args)
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["to"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert len(msg.get_payload()) == 2
mimeapp = MIMEImage(image)
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
示例2: attach_images
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def attach_images(*fns):
email = MIMEMultipart()
for fn in fns:
if not img_patt.search(fn.split('.')[-1]):
# Following is kinda like throwing an exception, but better.
print("%s doesn't seem to be an image file. Skipping." % fn)
continue
if url_patt.match(fn):
data = requests.get(fn).content
else:
with open(fn, 'rb') as f:
data = f.read()
img = MIMEImage(data, name=fn)
img.add_header('Content-Disposition', 'attachment; filename="%s"' % fn)
email.attach(img)
return email
示例3: test_add_header
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test_add_header(self):
eq = self.assertEqual
self._au.add_header('Content-Disposition', 'attachment',
filename='audiotest.au')
eq(self._au['content-disposition'],
'attachment; filename="audiotest.au"')
eq(self._au.get_params(header='content-disposition'),
[('attachment', ''), ('filename', 'audiotest.au')])
eq(self._au.get_param('filename', header='content-disposition'),
'audiotest.au')
missing = []
eq(self._au.get_param('attachment', header='content-disposition'), '')
self.assertIs(self._au.get_param('foo', failobj=missing,
header='content-disposition'),
missing)
# Try some missing stuff
self.assertIs(self._au.get_param('foobar', missing), missing)
self.assertIs(self._au.get_param('attachment', missing,
header='foobar'), missing)
# Test the basic MIMEImage class
示例4: test__all__
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test__all__(self):
module = __import__('email')
# Can't use sorted() here due to Python 2.3 compatibility
all = module.__all__[:]
all.sort()
self.assertEqual(all, [
# Old names
'Charset', 'Encoders', 'Errors', 'Generator',
'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
'MIMENonMultipart', 'MIMEText', 'Message',
'Parser', 'Utils', 'base64MIME',
# new names
'base64mime', 'charset', 'encoders', 'errors', 'generator',
'header', 'iterators', 'message', 'message_from_file',
'message_from_string', 'mime', 'parser',
'quopriMIME', 'quoprimime', 'utils',
])
示例5: test_add_header
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test_add_header(self):
eq = self.assertEqual
unless = self.assertTrue
self._au.add_header('Content-Disposition', 'attachment',
filename='audiotest.au')
eq(self._au['content-disposition'],
'attachment; filename="audiotest.au"')
eq(self._au.get_params(header='content-disposition'),
[('attachment', ''), ('filename', 'audiotest.au')])
eq(self._au.get_param('filename', header='content-disposition'),
'audiotest.au')
missing = []
eq(self._au.get_param('attachment', header='content-disposition'), '')
unless(self._au.get_param('foo', failobj=missing,
header='content-disposition') is missing)
# Try some missing stuff
unless(self._au.get_param('foobar', missing) is missing)
unless(self._au.get_param('attachment', missing,
header='foobar') is missing)
# Test the basic MIMEImage class
示例6: test_add_header
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test_add_header(self):
eq = self.assertEqual
self._au.add_header('Content-Disposition', 'attachment',
filename='audiotest.au')
eq(self._au['content-disposition'],
'attachment; filename="audiotest.au"')
eq(self._au.get_params(header='content-disposition'),
[('attachment', ''), ('filename', 'audiotest.au')])
eq(self._au.get_param('filename', header='content-disposition'),
'audiotest.au')
missing = []
eq(self._au.get_param('attachment', header='content-disposition'), '')
self.assertIs(self._au.get_param('foo', failobj=missing,
header='content-disposition'), missing)
# Try some missing stuff
self.assertIs(self._au.get_param('foobar', missing), missing)
self.assertIs(self._au.get_param('attachment', missing,
header='foobar'), missing)
# Test the basic MIMEImage class
示例7: generate_list_entry
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def generate_list_entry(mime_images, alert, screenshots, filenames, url, count):
result = '<hr><h3>' + alert + '</h3><h4>Alert generated on chart: <a href="' + url + '">' + url + '<a></h4>'
if len(screenshots) > 0:
for chart in screenshots:
result += '<p><a href="' + chart + '"><img src="' + screenshots[chart] + '"/></a><br/><a href="'+screenshots[chart]+'">' + screenshots[chart] + '</a></p>'
elif len(filenames) > 0:
for chart in filenames:
try:
screenshot_id = str(count + 1)
fp = open(filenames[chart], 'rb')
mime_image = MIMEImage(fp.read())
fp.close()
mime_image.add_header('Content-ID', '<screenshot' + screenshot_id + '>')
mime_images.append(mime_image)
result += '<p><a href="' + chart + '"><img src="cid:screenshot' + screenshot_id + '"/></a><br/>' + filenames[chart] + '</p>'
except Exception as send_mail_error:
log.exception(send_mail_error)
result += '<p><a href="' + url + '">Error embedding screenshot: ' + filenames[chart] + '</a><br/>' + filenames[chart] + '</p>'
return result
示例8: get_attachments
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def get_attachments(self):
attachments = super().get_attachments()
filename = (
finders.find('images/email_logo.png')
or finders.find('images/email_logo.svg')
)
if filename:
if filename.endswith('.png'):
imagetype = 'png'
else:
imagetype = 'svg+xml'
with open(filename, 'rb') as f:
logo = MIMEImage(f.read(), imagetype)
logo.add_header('Content-ID', '<{}>'.format('logo'))
return attachments + [logo]
return attachments
示例9: _build_html_msg
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def _build_html_msg(text, html, images):
"""Build Multipart message with in-line images and rich HTML (UTF-8)."""
_LOGGER.debug("Building HTML rich email")
msg = MIMEMultipart('related')
alternative = MIMEMultipart('alternative')
alternative.attach(MIMEText(text, _charset='utf-8'))
alternative.attach(MIMEText(html, ATTR_HTML, _charset='utf-8'))
msg.attach(alternative)
for atch_num, atch_name in enumerate(images):
name = os.path.basename(atch_name)
try:
with open(atch_name, 'rb') as attachment_file:
attachment = MIMEImage(attachment_file.read(), filename=name)
msg.attach(attachment)
attachment.add_header('Content-ID', '<{}>'.format(name))
except FileNotFoundError:
_LOGGER.warning("Attachment %s [#%s] not found. Skipping",
atch_name, atch_num)
return msg
示例10: test_add_header
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test_add_header(self):
eq = self.assertEqual
unless = self.failUnless
self._au.add_header('Content-Disposition', 'attachment',
filename='audiotest.au')
eq(self._au['content-disposition'],
'attachment; filename="audiotest.au"')
eq(self._au.get_params(header='content-disposition'),
[('attachment', ''), ('filename', 'audiotest.au')])
eq(self._au.get_param('filename', header='content-disposition'),
'audiotest.au')
missing = []
eq(self._au.get_param('attachment', header='content-disposition'), '')
unless(self._au.get_param('foo', failobj=missing,
header='content-disposition') is missing)
# Try some missing stuff
unless(self._au.get_param('foobar', missing) is missing)
unless(self._au.get_param('attachment', missing,
header='foobar') is missing)
# Test the basic MIMEImage class
示例11: add_attachment
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def add_attachment(self,filepath,filename=None):
if filename == None:
filename=os.path.basename(filepath)
with open(filepath,'rb') as f:
file=f.read()
ctype, encoding = mimetypes.guess_type(filepath)
if ctype is None or encoding is not None:ctype = "application/octet-stream"
maintype, subtype = ctype.split('/', 1)
if maintype == "text":
with open(filepath) as f:file=f.read()
attachment = MIMEText(file, _subtype=subtype)
elif maintype == "image":
with open(filepath,'rb') as f:file=f.read()
attachment = MIMEImage(file, _subtype=subtype)
elif maintype == "audio":
with open(filepath,'rb') as f:file=f.read()
attachment = MIMEAudio(file, _subtype=subtype)
else:
with open(filepath,'rb') as f:file=f.read()
attachment = MIMEBase(maintype,subtype)
attachment.set_payload(file)
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
attachment.add_header('Content-ID',str(self.attachment_num))
self.attachment_num+=1
self.attachment_list.append(attachment)
示例12: get_attachments
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def get_attachments(self):
# this returns two attachments, one a text file, the other an inline attachment that can be referred to in a
# template using cid: notation
fp = open('tests/python.jpeg', 'rb')
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format('python.jpg'))
raw_data = 'Some Report Data'
return [
('Report.txt', raw_data, 'text/plain'),
img,
]
示例13: setUp
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def setUp(self):
fp = openfile('PyBanner048.gif')
try:
self._imgdata = fp.read()
finally:
fp.close()
self._im = MIMEImage(self._imgdata)
示例14: test_checkSetMinor
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def test_checkSetMinor(self):
im = MIMEImage(self._imgdata, 'fish')
self.assertEqual(im.get_content_type(), 'image/fish')
示例15: send_email
# 需要導入模塊: from email.mime import image [as 別名]
# 或者: from email.mime.image import MIMEImage [as 別名]
def send_email(strTo):
strFrom = config.fromaddr
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Thanks for your ticket'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
#Add text message to the MIME object
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText('Hi there, <br><br>Thanks for your query with us today.'
' You can look at our <a href="https://google.com">FAQs</a>'
' and we shall get back to you soon.<br><br>'
'Thanks,<br>Support Team<br><br><img src="cid:image1">', 'html')
msgAlternative.attach(msgText)
#Add logo image to the auto response
fp = open('google.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
#Start SMTO Server and respond to the customer
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(config.fromaddr, config.password)
server.sendmail(config.fromaddr, config.toaddr, msgRoot.as_string())
server.quit()
#Infinite loop to read the inbox for new emails
#every 60 seconds (1 minute)