Using the code below I am trying to send an attachment to my Kindle from an Appengine App.
from google.appengine.api import mail
import cStringIO
out=cStringIO.StringIO()
out.write('Hello, World!')
reading = mail.EmailMessage(
sender='[sender email]',
subject='convert',
to=[list or recipient emails],
attachments=[('test.txt', out.getvalue())])
reading.send()
However, Amazon replies with
Your email to Kindle(s) did not include any attachments
Next I tried adding my email to the list of recipients and I get the email with the attachment just fine.
How come Amazon doesn't?
You're trying to send a .TXT file, but that's not a supported file type.
From Amazon's documentation:
Kindle Personal Documents Service supports multiple file types, including:
Microsoft Word (.DOC, .DOCX)
HTML (.HTML, .HTM)
RTF (.RTF)
JPEG (.JPEG, .JPG)
Kindle Format (.MOBI, .AZW)
GIF (.GIF)
PNG (.PNG)
BMP (.BMP)
PDF (.PDF)
Related
I am trying to export an Email message obtained with aiosmtpd to an *.eml file that I can later open with any *.eml viewer or email client like Thunderbird or Mail.
async def handle_DATA(self, server, session, envelope) -> str:
msg: EmailMessage = message_from_bytes(envelope.content, policy=policy.SMTPUTF8)
with open("test.eml", 'wb') as outfile:
outfile.write(msg.as_bytes())
I've also tried to save the file with Generator with both unixfrom=False and unixfrom=False and same thing.
with open("test.eml", 'w') as file:
emlGenerator = generator.Generator(file)
emlGenerator.flatten(msg, unixfrom=False)
The file gets created correctly but not all the eml files can be read correctly by Mail or Thunderbird.
Emails received from gmail.com are created correctly but emails received from protonmail.com are not. Eml files created from Protonmail emails can be opened but only from and to parameters of the email can be seen. I can't see the content neither the attachments in it.
I guess it does not have the format that eml parsers are expecting to see.
I've tried with different policies (like policy.SMTP, policy.default, ...).
What is the proper way to create eml files with python?
NOTE: The content of the EML file for Protonmail contains the following This is an OpenPGP/MIME signed message (RFC 4880 and 3156). Could this be related with the lack of correct parsing for Protonmail emails?
I had the same issue with the Email API. Although it can not always be opened directly as an .eml file, it is correctly opened if the message is sent to an SMTP server. I understand the Email API generates a string that represents the data sent to the SMTP server. That data is to be interpreted by an SMTP server, not a client. The SMTP server will interpret this data and store it as if finds more convenient (i.e. storing it in a db).
A client like Thunderbird will retrieve the message using POP3 or IMAP, but that doesn't mean that it is retrieving the same message that was sent to the SMTP server, since the server might store it in a different format.
The Instagram page does not have an option to upload videos when you open the page from a computer, I usually use Python Selenium to find a position on the screen and send text or upload/download a file, however, I cannot use Selenium on Instagram because the Instagram version for Computer does not show any button to upload a video.
I did a testing using INSTABOT to upload a photo, the testing was successful, but when I try to upload a video, INSTABOT cannot upload it.
I would like to know if someone has any script to upload videos on Instagram using python.
Thanks
Use this library:
https://github.com/instagrambot/instapy-cli/tree/master/examples
Here is an example of what you asked:
from instapy_cli import client
username = 'USERNAME'
password = 'PASSWORD'
video = 'docs/video-sample-upload.mp4'
text = 'This will be the caption of your video.' + '\r\n' + 'You can also use hashtags! #hash #tag #now'
with client(username, password) as cli:
cli.upload(video, text)
I am trying to send a logo with the email and have it appear in the HTML part of the email. I am building my email like this:
mail_subject = _("Subject of email %s" %
self.get_company_display())
from_email = "test#test.com"
message = EmailMultiAlternatives(mail_subject, mail_txt, from_email,
['destination#email.com'])
message.attach_alternative(mail_html, 'text/html')
message.attach('logo.png', static('myapp/images/logo.png'))
message.send()
And in my mail template I have:
<img src="cid:logo.png">
I receive the email but the image doesn't appear in the email. In fact, the email does not appear to have the image as an attachment.
Working on Python 3.4, Django 1.8.4 and sending the mails through Postfix installed on the same machine Django is running.
The EmailMessage.attach method expects to be passed the content of the file not its path, what you are actually doing is attaching the string returned by static('myapp/images/logo.png') to the message.
Use EmailMessage.attach_file instead (EmailMessage reference).
The whole purpose of yagmail (I'm the developer) is to make it really easy to send emails, especially with HTML or attachment needs.
Please try the following code:
import yagmail
yag = yagmail.SMTP(from_add, password) # add host="" and port=
contents = ['See my attachment below', '/home/static/images/logo.png']
yag.send(contents = contents)
Notice the magic here: contents is a list, where an item equal to a file path will automatically be loaded, mimetype guessed, and attached.
There's a lot more magic involved, such as easy to embed images, passwordless scripts, usernameless scripts, easy aliases, smart defaults (notice I omitted the to and subject arguments?) and much more. I advise/encourage you to read its github page :-). Feel free to raise issues or add feature requests!
You can get yagmail by using pip to install it:
pip install yagmail # Python 2
pip3 install yagmail # Python 3
I found this thread: GAE Python - How to attach the results of csv.writer to an email?
which has been helpful in getting me to be able to attach csv's to an email from Google App Engine. My code is as follows:
self.response.headers[str('Content-Type')] = str('text/csv')
self.response.headers[str('Content-Disposition')] = str('attachment; filename="data.csv"')
writer = csv.writer(self.response.out)
[write csv here]
message=mail.EmailMessage(sender='test#example.com',subject='Subject', attachments=[("data.csv",self.response.body)])
message.to=[to email here]
message.html='Message Body'
message.send()
The issue is when this code runs, not only is an email sent but a file is also downloaded. Is there a way to prevent the file from being downloaded?
It is downloading because you are writing it to the response output. You should be writing the attachment content to a temporary file/memory and attach that.
Also there is no need to set those headers.
The accepted answer to the SO question you have referenced is incorrect, the other answer is best https://stackoverflow.com/a/15453323/2018227
So I want to send an email from Django/Python hosted with Heroku with an attachment (PDF) located in an S3 bucket. So I can send an email with a file attachment that is locally stored but I can't find a way to attach a remotely hosted file to an email.
As an additional point, I'm trying to use Mandrill but this isn't the main requirement.
I'm simply trying to send an email from python/django with an attachment without having to save the attachment locally first (as this isn't recommended/possible/ideal with my Heroku setup)
This is simple enough, assuming your file is stored as a normal file field (with a different storage selected), then you can do the following:
message = EmailMessage(subject, body, from_email, bcc=recipient_list)
message.attach(FILENAME, mymodel.myfilefield.read())
This will just download the file from S3 temporarily into the memory and send the email with the attachment without the necessity to use the local filesystem.