I'm currently using the yagmail module to send e-mails with Python, and I'm having difficulty embedding locally stored images into an e-mail. Is this possible?
Here's a code example:
contents = ["<img src='/path/to/local/image'>"]
yag = yagmail.SMTP('myemail#gmail.com', 'password')
yag.send('myotheremail#gmail.com', 'E-mail Title', contents)
Using the above code example, if I input an external path (e.g, imgur image or google image), it works perfectly, put I cannot seem to get a local path recognized.
The solution doesn't have to be using yagmail, it just seems to be the easiest e-mail module I've used so far.
Thanks for any help!
yagmail creator here:
Try this:
contents = [yagmail.inline("/path/to/local/image")]
You could put the image on a cloud CDN like google drive has this kind of function and send the email as HTML including the picture.
Related
I'd like to make a function which converts Google Drive videos into VLC streamable links (e.g. vlc://https://WEBSITE.com/FILE_ID.mkv.
I've tried methods which were shared on stack overflow, such as modifying the Google Drive link to:
https://drive.google.com/uc?export=download&id=FILE_ID
All the methods I've tried seem to not work anymore. Any ideas?
I've figured out the answer.
Google Drives' API has a download feature, you just need to make a request to https://www.googleapis.com/drive/v3/files/FILE_ID?alt=media&key=API_KEY
Now this doesn't generate a direct file path ending with .mp4 or .mkv but VLC and PotPlayer are able to recognize this link like this:
potplayer://https://www.googleapis.com/drive/v3/files/FILE_ID?alt=media&key=API_KEY
vlc://https://www.googleapis.com/drive/v3/files/FILE_ID?alt=media&key=API_KEY
Edit: this doesn't work in development, Google prevents bots from making requests like that. To work around this you need to set a header in your request. e.g.
url = "https://www.googleapis.com/drive/v3/files/FILE_ID?alt=media&key=API_KEY"
r = requests.get(url, headers={"Authorization":"Bearer " + accessToken})
You get the accessToken from the Google Drive API
Just make the file public and copy your ID.
You can find it here: /file/d/YOUR ID/view?usp=sharing.
Copy your ID and paste it in this:
drive.google.com/uc?export=view&id=YOUR ID
I have difficulties in embedding images in a multipart email.
I am trying to send out a html file with plenty of embedded images as mail. However, the images do not appear, and are currently just sent as attachments.
I assume I don't manage to link the html code.
Here is part of the HTML code for the first image
</v:shapetype><v:shape id="Picture_x0020_5" o:spid="_x0000_i1029" type="#_x0000_t75"
alt="cid:image001.png#01D58F16.6A9DB2F0" style='width:441.45pt;height:183.85pt;
visibility:visible;mso-wrap-style:square'>
<v:imagedata src="somefolder-data/image001.png"
o:title="image001.png#01D58F16"/>
The images are not placed in the working directory itself.
I believe the issue lies in defining the image's ID as referenced above in the HTML, which looks different from online examples. I have tried a few versions, but haven't had success.
I assumed the part following cid would be relevant (so image001.png), but it might be "image001.png#01D58F16.6A9DB2F0".
Can someone help with to create the right connection here?
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-Disposition', 'inline', filename='image001.png')
# Attach part into message container.
msg.attach(msgImage)
Thanks in advance
I'm trying to make some code on python to edit someone's profile pic, but all I've got so far is this:
image = ctx.message.author.avatar_url
background = Image.open(image)
Apparently that just gets the URL itself, but i need the image itself to edit a picture with PIL. Any insight on how to get it?
with requests.get(ctx.message.author.avatar_url) as r:
img_data = r.content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)
So I played about with this link a bit:
https://cdn.discordapp.com/avatars/190434822328418305/6a56d4edf2a82409ffc8253f3afda455.png
And I was able to save my own avatar image (the one I use for my accounts everywhere). I was then able to open the file regularly with the photo viewer app within Pycharm.
After, it would simply become a case of opening the new jpeg file with PIL or pillow instead of trying to open anything from a website, if that makes sense.
You should consider that this will save a file onto your Discord bot server, so this is extremely crude, a malformed or maliciously formed jpeg file could lead to some sort of remote vulnerability.
Furthermore to your comment, if you want the size of the image you download to be bigger, for example, please see the amended link below to solve your problem there:
https://cdn.discordapp.com/avatars/190434822328418305/6a56d4edf2a82409ffc8253f3afda455.png?size=<Number from list [16,32,64,128,256,512,1024,2048]>
Hope this helps :)
Ok so I've got a google script that I'm working on to help out with some stuff and I need to be able to create a txt file and download it to my local computer. I've figured out how to create the file as a blob and create a file in google drive.
The problem is that I for one can't figure out how to delete the old file and create the new file and I also can't seem to figure out how to download it locally so I can work my python magic and create a nice looking report and print it out. I've gone over the documentation and looked at similar questions but I can't figure out how to actually download a file.
An example program would be great answer for me something that uses a dummy file and downloads it, really that would be awesome.
My thoughts are that I could go back to the old file that I am trying to download and just edit it so that way I don't have to actually delete the file which would make it have the same ID's, meta data, and URL.
Can I use the google app script to download it directly to my computer and for a little added info I run a Linux machine so some things are a little more labor intensive for me while some other stuff is nice and easy. I feel like there is an app that can run on my computer locally that stores my google drive files locally and I could possibly just grab it from there.
Lastly a link to documentation for running the scripts natively would be helpful as well.
This is how to download a file in google drive using Python.
# sample
import googleDriveAccess
ci = googleDriveAccess.readClientId(basedir)
ds = googleDriveAccess.second_authorize(basedir, ci, script=True)
id, fileobj = googleDriveAccess.script_download(ds, folder, SCRIPT_ID)
source code of googleDriveAccess:
https://github.com/HatsuneMiku/googleDriveAccess
Apps Script Crash Course: Import/Export Apps Script Code
https://www.youtube.com/watch?v=lEVMu9KE6jk
Google Drive SDK: Searching for files
https://www.youtube.com/watch?v=DOSvQmQK_HA
use
urllib.retrieve(httpaddressofthefile, filenametosaveitto)
or
import urllib2
response = urllib2.urlopen('http://www.wherever.com/')
html = response.read()
f=open(filename, 'w')
f.write(html)
f.close()
Okay what you are looking for is pydrive
Its a google drive API made just for uploading/downloading google drive stuff.
also #example request:
here:
#-------------------------------------------------------------------------------
# Author: Amazingred
# Created: 0017032814
#-------------------------------------------------------------------------------
import urllib, urllib2, os, sys
#The pydrive package download
page='https://pypi.python.org/packages/source/P/PyDrive/PyDrive-1.0.0.tar.gz'
request=urllib.urlretrieve(page, 'pydrive.tar.gz')
if os.path.exists(os.path.join(os.path.dirname(__file__),"pydrive.tar.gz")):
print "YUP THE FILE EXISTS"
else:
print "nope"
I am writing a script which will run on my server. Its purpose is to download the document. If any person hit the particular url he/she should be able to download the document. I am using urllib.urlretrieve but it download document on the server side not on the client. How to download in python at client side?
If the script runs on your server, its purpose is to serve a document, not to download it (the latter would be the urllib solution).
Depending on your needs you can:
Set up static file serving with e.g. Apache
Make the script execute on a certain URL (e.g. with mod_wsgi), then the script should set the Content-Type (provides document type such as "text/plain") and Content-Disposition (provides download filename) headers and send the document data
As your question is not more specific, this answer can't be either.
Set the appropriate Content-type header, then send the file contents.
If the document is on your server and your intention is that the user should be able to download this file, couldn't you just serve the url to that resource as a hyperlink in your HTML code. Sorry if I have been obtuse but this seems the most logical step given your explanation.
You might want to take a look at the SocketServer module.