How to embed a linked image into a python email - python

There are plenty of answers on how to embed images into emails in python. What I can't figure out is how to embed an image that's clickable and leads you to a site.
Sending Multipart html emails which contain embedded images
I pretty much followed the first comment of this exactly. I just need to figure out how to add a link to that image
this is what I followed
msgRoot = MIMEMultipart('related')
fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', "<image1>")
msgRoot.attach(msgImage)
Obviously this just embeds an image, but I need it to embed a linked image!

The example you linked were properly formatted HTML Emails where add_alternative() was used to supply the HTML portion of the email. You excluded this in what you've written. If you include an actual HTML body for your email, then all you need to do is wrap the image in an anchor(link) element with the url you're trying to link to.
(Adapted from your linked question)
msg.add_alternative("""\
<html>
<body>
<p>Click the Image below to visit our site!</p>
<img src="cid:{image_cid}">
</body>
</html>
""".format(image_cid=image_cid[1:-1]), subtype='html')
Edit
Don't have Python 2 around to test, but the following code from the Accepted Answer on that same thread (which was indicated as being Python 2.x compatible) presumably should work.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlternative.attach(msgText)
Again, the point here being that you embed the image via html, which also allows you apply anchor tags (and basically any other styling you would like).

Related

Adding a variable string to an expression in Airium (Python)

I'm working on a little jig that generates a static gallery page based on a folder full of images. My current hangup is generating the HTML itself-
I used Airium to reverse-translate my existing HTML to Airium's python code, and added the variables I want to modify for each anchor tag in a loop. But I can't for the life of me figure out how to get it to let me add 'thumblink'. I'm not sure why it's treating it so differently from the others, my guess is that Airium expects foo:bar but not foo:bar(xyz) with xyz being the only part I want to pull out and modify.
from airium import Airium
imagelink = "image name here" # after pulling image filename from list
thumblink = "thumb link here" # after resizing image to thumb size
artistname = "artist name here" # after extracting artist name from filename
a = Airium()
with a.a(href='javascript:void(0);', **{'data-image': imagelink}):
with a.div(klass='imagebox', style='background-image:url(images/2015-12-29kippy.png)'):
a.div(klass='artistname', _t= artistname)
html = str(a) # cast to string
print(html) # print to console
where "images/2015-12-29kippy.png" is what I'd replace with string variable "thumblink".
image and artist do translate correctly in the output after testing -
<a href="javascript:void(0);" data-image="image name here">
<div class="imagebox" style="background-image:url(images/2015-12-29kippy.png)">
<div class="artistname">artist name here</div>
</div>
</a>
>>>

Adding an image to an Outlook MailItem via Python

My goal is to add an image (from local .png file) to an Outlook MailItem using Python.
I have read answers to similar questions on SO (mostly using VBA), but the issue I have is that the mail looks fine (I can see the image) before I send it (and when I look in my Sent Items folder), but when it is received at the other end, the image is broken.
If I manually insert the image in a message (using Insert / Picture / From this Device from the Outlook menu) the email arrives fine, with the image displayed.
Here is the basic code I am using (based mostly on VBA examples):
import win32com.client as wc
def main():
outlook = wc.Dispatch("Outlook.Application")
#Create a new mail item
msg = outlook.CreateItem(0)
#Set some properties
msg.BodyFormat = 2
msg.Subject = 'Here is my chart'
msg.Recipients.Add('xxx#yyy.com')
#Add an attachment, with position 0
imageFile = 'C:\\Temp\\myplot.png'
ats = msg.Attachments
att = ats.Add(imageFile,1,0)
#Set the HTMLBody
msg.HTMLBody = '<img src="cid:{0:}" width=200 height=200>'.format(att.FileName)
#Send
msg.Send()
main()
This is what I see in my Sent Items folder:
The HTML in my sent item (using View Source) is this:
<img src="cid:myplot.png" width=200 height=200>
But this is what arrives:
The HTML in the body at the destination is this:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<img src="cid:AD1B7014D243624CA83F873A11053F18#GBRP123.PROD.OUTLOOK.COM" width="200" height="200">
</body>
</html>
When I manually insert the image, the received HTML is much longer (so I won't include it all here), but the 'img' tag is here (and this matches what is in the Sent Items folder):
<img width="614" height="461" style="width:6.3958in;height:4.802in" id="Picture_x0020_1" src="cid:image001.png#01D736CD.72206440" alt="Chart, line chart Description automatically generated">
I would appreciate any insights on this! (Sadly Outlook won't let me record a macro to see what the manual process is doing). I have seen one example (from C#) where the SetProperty() method is used on the MailItem to add other items that are not in the Outlook object model (schema, context id), together with a newly created Guid, but is this really necessary?
Using Microsoft 365, Outlook version 2105, Beta Channel
Create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty.
Your HTML body (MailItem.HTMLBody property) would then need to reference that image attachment through the cid:
img src="cid:xyz"
where xyz is the value of the PR_ATTACH_CONTENT_ID property.
Look at an existing message with OutlookSpy (I am its author) - click IMessage button.
attachment = mailitem.Attachments.Add("c:\temp\MyPicture.jpg");
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1");
mailitem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>";

Using xmltodict to access a line inside a tag in Python

I have an xml file that looks like:
<!-- For the full list of available Crowd HTML Elements and their input/output documentation,
please refer to https://docs.aws.amazon.com/sagemaker/latest/dg/sms-ui-template-reference.html -->
<!-- You must include crowd-form so that your task submits answers to MTurk -->
<crowd-form answer-format="flatten-objects">
<!-- The crowd-classifier element will create a tool for the Worker to
select the correct answer to your question.
Your image file URLs will be substituted for the "image_url" variable below
when you publish a batch with a CSV input file containing multiple image file URLs.
To preview the element with an example image, try setting the src attribute to
"https://s3.amazonaws.com/cv-demo-images/two-birds.jpg" -->
<crowd-image-classifier\n
src= "https://someone#example.com/abcd.jpg"\n
categories="[\'Yes\', \'No\']"\n
header="abcd"\n
name="image-contains">\n\n
<!-- Use the short-instructions section for quick instructions that the Worker\n
will see while working on the task. Including some basic examples of\n
good and bad answers here can help get good results. You can include\n
any HTML here. -->\n
<short-instructions>\n\n
</crowd-image-classifier>
</crowd-form>
<!-- YOUR HTML ENDS -->
I want to extract the line:
src = https://someone#example.com/abcd.jpg
and assign it to a variable in python.
Bit New to xml parsing:
I tried like:
hit_doc = xmltodict.parse(get_hit['HIT']['Question'])
image_url = hit_doc['HTMLQuestion']['HTMLContent']['crowd-form']['crowd-image-classifier']
Error:
image_url = hit_doc['HTMLQuestion']['HTMLContent']['crowd-form']['crowd-image-classifier']
TypeError: string indices must be integers
If I don't access the ['crowd-image-classifier'] in code and limit myself to
hit_doc = xmltodict.parse(get_hit['HIT']['Question'])
image_url = hit_doc['HTMLQuestion']['HTMLContent']
Then I'm getting complete xml file.
How to access that img src?
You can use BeautifulSoup. See a the working code below.
from bs4 import BeautifulSoup
html = '''<!-- For the full list of available Crowd HTML Elements and their input/output documentation,
please refer to https://docs.aws.amazon.com/sagemaker/latest/dg/sms-ui-template-reference.html -->
<!-- You must include crowd-form so that your task submits answers to MTurk -->
<crowd-form answer-format="flatten-objects">
<!-- The crowd-classifier element will create a tool for the Worker to
select the correct answer to your question.
Your image file URLs will be substituted for the "image_url" variable below
when you publish a batch with a CSV input file containing multiple image file URLs.
To preview the element with an example image, try setting the src attribute to
"https://s3.amazonaws.com/cv-demo-images/two-birds.jpg" -->
<crowd-image-classifier\n
src= "https://someone#example.com/abcd.jpg"\n
categories="[\'Yes\', \'No\']"\n
header="abcd"\n
name="image-contains">\n\n
<!-- Use the short-instructions section for quick instructions that the Worker\n
will see while working on the task. Including some basic examples of\n
good and bad answers here can help get good results. You can include\n
any HTML here. -->\n
<short-instructions>\n\n
</crowd-image-classifier>
</crowd-form>
<!-- YOUR HTML ENDS -->'''
soup = BeautifulSoup(html, 'html.parser')
element = soup.find('crowd-image-classifier')
print(element['src'])
output
https://someone#example.com/abcd.jpg
I switched to using xml element tree
Syntax I got is somewhat similar to:
import xml.etree.ElementTree as ET
root = ET.fromstring(hit_doc)
for child in root:
if child[0].text == 'crowd-image-classifier':
image_data = child[1].text

Using AngularJS to show images from API (flask app)

I'm trying to show some badge images I made for a RANK APP I've been working for. It's 10 images that should be shown specific for each driver.
I'm not an expert on coding, but I keep searching and studying ways to solve the problem I've been through.
I firstly tried to send base64 images from the API to the browser, using this code:
<!-- language: python -->
for img in imglist: #loop for creating a list of base64 images from a list of image dir.
imgcode = base64.encodestring(open(imgdir + img,"rb").read())
imgcodelist.append(imgcode)
for driver in sortdriverList: #loop for taking drivers points and turn it into RANK img
if (driver['Races'] < 21):
driver['Rank'] = str(imgcodelist[9])
[...]
The second loop is longer than that, stil what I've shown to you above makes any driver that wasn't participating in more than 21 races, be part of a 'NON CLASSIFIED' badge.
I used AngularJS to try to show the base64 image using the code below.
'<html>'
<td><img src="data:image/png;base64,{{ '{{driver.Rank}}'}}"></td>
[driver.Rank] should be the base64 code string. When I run the app, the image is not shown, instead I see the very code of the image inside the table =/
Then I tried to turn [driver.Rank] into a dir string for "img src=", using the codes below.
<!-- language: python -->
imglist = ["notclassified.png", etc...]
imgdir = "static/images/"
for item in sortdriverList:
if (item['Races'] < 21):
item['Points'] = imgdir + imglist[9]
and in my HTML I changed the img src to:
'<html>'
<img src= {{ '{{driver.Rank}}' }}>
and now it shows the directory of the images.
I've been searching for CSS ways to make it possible.
I coudn't find a solution yet.
It's hard to tell what's going on since only segments are pasted, but I'm guessing it has to do with how you are escaping the code. Maybe you could paste the generated code in chrome.
Sometimes seeing a working example helps.
angular.module('App', [])
.controller('DriverCtrl', DriverCtrl);
function DriverCtrl($scope) {
// base64 encode 1x1 black pixel
this.Rank = 'R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';
}
<div ng-app="App">
<div ng-controller="DriverCtrl as driver">
<div>Rank: {{driver.Rank}}</div>
<span>Image:</span>
<img ng-src="data:image/png;base64,{{driver.Rank}}">
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Exporting data as an XML file in google appengine

I'm trying to export data to an XML file in the Google appengine, I'm using Python/Django. The file is expected to contain upto 100K records converted to XML. Is there an equivalent in App Engine of:
f = file('blah', 'w+')
f.write('whatever')
f.close()
?
Thanks
Edit
What I'm trying to achieve is exporting some information to an XML document so it can be exported to google places (don't know exactly how this will work, but I've been told that google will fecth this xml file from time to time).
You could also generate XML with Django templates. There's no special reason that a template has to contain HMTL. I use this approach for generating the Atom feed for my blog. The template looks like this. I pass it the collection of posts that go into the feed, and each Post entity has a to_atom method that generate its Atom representation.
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xml:lang="en"
xml:base="http://www.example.org">
<id>urn:uuid:4FC292A4-C69C-4126-A9E5-4C65B6566E05</id>
<title>Adam Crossland's Blog</title>
<subtitle>opinions and rants on software and...things</subtitle>
<updated>{{ updated }}</updated>
<author>
<name>Adam Crossland</name>
<email>adam#adamcrossland.net</email>
</author>
<link href="http://blog.adamcrossland.net/" />
<link rel="self" href="http://blog.adamcrossland.net/home/feed" />
{% for each_post in posts %}{{ each_post.to_atom|safe }}
{% endfor %}
</feed>
Every datastore model class has an instance method to_xml() that will generate an XML representation of that datastore type.
Run your query to get the records you want
Set the content type of the response as appropriate - if you want to prompt the user to save the file locally, add a content-disposition header as well
generate whatever XML preamble you need to come before your record data
iterate through the query results, calling to_xml() on each and adding that output to your reponse
do whatever closing of the XML preamble you need to do.
What the author is talking about is probably Sitemaps.
Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.
And about what I think you need is to write the XML to request object like so:
doc.writexml(self.response.out)
In my case I do this based on mime types sent from the client:
_MIME_TYPES = {
# xml mime type needs lower priority, that's needed for WebKit based browsers,
# which add application/xml equally to text/html in accept header
'xml': ('application/xml;q=0.9', 'text/xml;q=0.9', 'application/x-xml;q=0.9',),
'html': ('text/html',),
'json': ('application/json',),
}
mime = self.request.accept.best_match(reduce(lambda x, y: x + y, _MIME_TYPES.values()))
if mime:
for shortmime, mimes in _MIME_TYPES.items():
if mime in mimes:
renderer = shortmime
break
# call specific render function
renderer = 'render' + renderer
logging.info('Using %s for serving response' % renderer)
try:
getattr(self.__class__, renderer)(self)
except AttributeError, e:
logging.error("Missing renderer %s" % renderer)

Categories

Resources