Umail micropython and attachments missing content #9933
-
Hi, Using https://github.com/shawwwn/uMail to send emails with attachments. Having issues with emailing csv/text type attachments that contain a : within the file. Using a gmail client to both send and receive the email. example code: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hmmm, here are the first two lines out of my daily .csv file:
davef |
Beta Was this translation helpful? Give feedback.
-
My function maybe has a few hints in it. def send_csv(date):
file_size = uos.stat('datalog.csv')[6]
chunk_size = 1024
counter = 0
content = 'there is no content' # if reading ramdisk fails this keeps things alive
smtp = umail.SMTP('smtp.gmail.com', 587)
utime.sleep(1)
while True:
counter +=1
try:
# test1 = smtp.login('[email protected]', 'xyz')
test1 = smtp.login('[email protected]', 'abcd')
result = str(test1).find('Accepted')
if (result != -1):
print('login OK')
counter = 0
break
except Exception:
# try to capture the failure
try:
with open('errors.txt', 'a') as outfile:
outfile.write('smtp.login did NOT work')
except OSError:
pass
if (counter == 5):
counter = 0
# generate a wdt
utime.sleep(80)
counter = 0 # reset
wdt_feed()
utime.sleep(1)
smtp.to('[email protected]')
utime.sleep(1)
smtp.write('From: [email protected]\n')
smtp.write('To: [email protected]\n')
smtp.write('Subject: Today\'s ' + config.device + 'datalog\n')
smtp.write('MIME-Version: 1.0\n')
smtp.write('Content-type: multipart/mixed; boundary=12345678900987654321\n')
smtp.write('--12345678900987654321\n')
smtp.write('Content-Type: text/plain; charset=utf-8\n')
smtp.write('see attachment\n')
smtp.write('--12345678900987654321\n')
# build today's filename into Content-Type and Content-Disposition
csv_file = config.device + date + '.csv\n'
content_type = 'Content-Type: text/csv; name=' + csv_file
content_disposition = 'Content-Disposition: attachment; filename=' + csv_file
smtp.write(content_type)
smtp.write(content_disposition)
wdt_feed()
# send data in 1024 chunks
try:
with open('datalog.csv', 'rb') as file:
for chunk in range(0, file_size, chunk_size):
this_chunk_size = min(chunk_size, file_size-chunk)
# handles the last dangling chunk that would be smaller than the full 1024 bytes
smtp.write(file.read(this_chunk_size))
except OSError as error:
try:
with open('errors.txt', 'a') as outfile:
outfile.write('error = ' + str(error) + '\n')
outfile.write('did not handle datalog.csv properly' + '\n')
except OSError:
pass
smtp.send() # does the proper ending
smtp.quit()
print('log has been sent') |
Beta Was this translation helpful? Give feedback.
-
The RFCs (see e.g. wikipedia on SMTP) states that all lines need to end with CRLF ("\r\n" in python). I think the real error here is that you don't separate the mail headers from the content.
My guess: everything in front of the colon |
Beta Was this translation helpful? Give feedback.
The RFCs (see e.g. wikipedia on SMTP) states that all lines need to end with CRLF ("\r\n" in python).
This might be handled by the umail package, but who knows without looking ?
I think the real error here is that you don't separate the mail headers from the content.
In SMTP needs to be an empty line after you send the content-type, e.g.
My guess: everything in front of the colon
:
gets interpreted as some (unknown) header when you don't tell the package that the mail headers are complete.