InlineImages are sent as attachments to email, not as embedded pictures - html

I am using templated_email package to send emails with embedded images following templated_email docs with the below code block:
with open(path_to_img, 'rb') as image:
image = image.read()
inline_image = InlineImage(filename=image_name, content=image)
But images supposed to be inlined are instead being added as attachments in some devices. I don't want them to be added as attachments. Is there any solution?
I also tried after setting subtype = related as mentioned in one of the solutions but it did not work.

Related

Rendering a pdf file from an html view to display it on a web page as a image preview

I need to create a pdf preview that should be displayed on a web page as an image. The pdf file is just a simple report build on almost plain HTML. Essentially I had a problem with displaying checkboxes, now I replaced them with pics of checkboxes but the issue remains the same.
Here how I create the pdf report from my HTML view with help of groovy and grails:
def html = htmlRenderService.getReport(info)
ByteArrayOutputStream out = new ByteArrayOutputStream()
HtmlImageGenerator htmlImageGenerator = new HtmlImageGenerator()
htmlImageGenerator.loadHtml(html)
BufferedImage bi = htmlImageGenerator.bufferedImage
ImageIO.write(bi, "PNG", out)
byte[] bytes = out.toByteArray()
String base64bytes = encoder.encodeToString(bytes)
String src = "data:image/png;base64," + base64bytes
out.flush()
def getReport(Info info) {
return groovyPageRenderer.render(view: REPORT_VIEW,
model: [info: info])
}
Then I send the src string to my view and render it as:<img src="${src}" alt=""/>
Then my checkbox pic looks like this: <div style="/*style stuff*/ background-image: url(data:image/png;base64,LINK_TO_THE_IMAGE"></div>
In the end, I received a picture of my pdf report rendered pretty well displaying as an image on my page, BUT without checkboxes. Here is the picture of one part of it:
And here is the same part but from the pdf document which I rendered all the same way, but just downloaded directed from my webapp:
Here is an example where I combined both options(input checkbox and image checkbox) and rendered it as an image:
So what could cause this issue? Thank you in advance.
UPDATE: I came across today to this comment under another issue with HtmlImageGenerator:
HtmlImageGenerator seems to use a JEditorPane for rendering the HTML. Swing HTML support does not extend to the ability to render data images. It might be possible by digging into the HTMLEditorKit and changing the image loading element to support data images, but then you'd need to find a way to get HtmlImageGenerator to use the altered editor pane.
Seems that HtmlImageGenerator doesn't work well with images inside HTML files, but it's still unclear why it doesn't render checkbox inputs as well.
Without seeing the code you end up with after page load, check the chrome dev tools panel to see if the image has actually loaded correctly to the page which will tell you it's at least accessible to use. Then check if the url is output correctly to the div as the background-image. If it looks correct and there aren't related errors in the console, it is likely a css setting.
With background images, your container will need to contain content or else you will need to specify:
width
height
a display setting
background-position, and a
background-size
If you can upload more info, I might be able to be more specific.

apps script getBody doesn't give full html contents

I get emails from a system that contains paragraphs and tables.
When I try to get full html format using getBody from email it gives me just CSS contents and not the whole html contents.
However when I copy full email body and send it to myself in a new email then getBody function gives me accurately full body in html format including all tags and contents.
Kindly guide what I am missing here?
var label = GmailApp.getUserLabelByName("INBOX/reports0");
var threads = label.getThreads();
var tempbody = threads[i].getMessages()[0].getBody();
Regards
As the logged output in the console is too large and it's being truncated you're not seeing the whole output in neither of the cases, you're getting the entire HTML you just don't see it there. You can use Stackdriver logging instead to show the whole output.

How to return an image object in django http response?

I am sending emails containing images. I am using html templates for these emails. I want the images to be generated on the fly. Hence, the 'src' in the image tag is a url that makes a REST api call to my app. The images are dynamically created, a publicly accessible URL is created. I want this image to be displayed in the email. But I am not able to figure out how to return this image.
You probably want some code like this
the_image= Image.new(#whatever you want in your image)
response = HttpResponse(content_type="image/jpeg")
the_image.save(response, "JPEG")
return response

What is the syntax for using cid:attachmentID in e-mail for image file used in css

I am working on a script that e-mails some formatted html and images to recipients. Using MIME::Lite, I figured out a way to send the css file and the image file it uses as attachments. The image comes out at the end of the mail message - as an attachment. The following line appears to work:
<link href="cid:style.css" rel="stylesheet" type="text/css" />
My question is what should be the syntax for the following lines (in the file style.css)? Following does not work.
body {
background-image:url("cid:bgLine.png");
background-repeat:repeat-x;
}
Furthermore, how can I stop the mail client from showing the image by itself? Script I am using follows:
my $msg = MIME::Lite->new( From =>"from\#company\.com",
To => "to\#company\.com",,
Subject =>"Action Required",
Disposition =>'inline',
Type =>'multipart/related');
$msg->attach(Type => 'text/html', Data => qq{#htFileContents});
$msg->attach(Type => 'text/html', Id => $cssFileName, Data => qq{#cssFileContents});
$msg->attach(Type => 'image/png', Id => $imageFile, Path => $imageFile);
$msg->send("sendmail","/usr/sbin/sendmail -t");
Having the mail client access an URL for the css or the image file from an http server is not an option. The e-mail needs to be self-contained. TIA for an example showing the syntax.
This won't really work. Even if by some miracle you were able to get your attachments to see and talk to each other, Outlook won't support background images, Gmail will strip out your linked css file and any embedded css, and hotmail will not support css backgrounds.
Sounds like you need to host an HTML email somewhere that accepts dynamic parts, and trigger the send from within your application, passing it the dynamic parts to fill in before sending. Check out a tool like Lyris Listmanager or something.
There is no connection between the title and the posted question above.
Anyway, in order to send emails, showing safely images, that are attached in the email, it just not enough to refer the images with some cid: URLs.
Attaching the images to the email has the following specifics:
Replace the external URLs to images with the URLs of an attachments in the email.
The email to generate is not just multipart/mixed what is construed by default of JavaMail, but MIME multipart/related type, in order to allow the email client to use the images.
The attachments for the images must be have
Content-Disposition: inline; filename=... header, in order they not to be shown as attached files, but to allow the email client to use them.
ContentID:unique-name#domain header, where the unique-name and domain have to be replaced with actual values and keep < and >!
Example: ContentId: logo.png#paysafe.com
The references in the HTML part of the email message (the message body) happens through cid:content-id URLs, where content-id is the the value of the ContentId header of the attachment holding the image
Example:
References:
https://javaee.github.io/javamail/docs/api/ for the mail protocol-specific properties.
https://www.rfc-editor.org/rfc/rfc2392 - defines the CID: URL schema and the use of addr-spec
https://www.rfc-editor.org/rfc/rfc822 - defines addr-spec in the form: local-part "#" domain
https://www.rfc-editor.org/rfc/rfc1806 - defines Content-Disposition header
https://www.rfc-editor.org/rfc/rfc2387 - The MIME Multipart/Related Content-type - vital for image inlining

Json API plugin- Get thumbnail image from Attachments Array

I am pulling in data from Wordpress using Json API plugin. I am successfully getting the array of attachments using:
var myImage=myJSON[j].attachments;
But that gives me the whole array. How do I get just the thumbnail image from inside that array?
If you want the post thumbnail url for the post "featured image" you can just do:
var myImage=myJSON[j].thumbnail_images.thumbnail.url;
If you want to get the thumbnail of any attachments URL do this:
var myImage=myJSON[j].attachments[0].images.thumbnail.url;
just replace the "[0]" with the index of the image you want to get. console.log() and check your JSON response to find the index of the desired image.
With the current version of this plugin, this works as kmazla described only if you upload the image while editing the post/or creating the post for the first time. If you attach an existing image from the media database, it will still show up as an empty array in the query. If you decide to upload more photos (since the first one you uploaded was as mistake for example) they get appended to the attachments array even though you only select one of the images as your "featured image". It is quite confusing.
The workaround?
Make a fresh new post and make sure you attach the right images in the right sequence. Older posts should be deleted and republished for the attachments array to be lined up properly.