Creating a unix carriage return with unix LF instead of windows CRLF on blobMedia

What I’m trying to do:
I am trying to send a txt file attachment for further processing. The text file has a key and value using an equals(=) between the key and value. The system expects the newline to be unix based return(LF). Text files are created as windows return(CRLF). I am using the blobMedia function to create the file and send it through using anvil.send.email.
What I’ve tried and what’s not working:
I do not know enough about python for this issue. It probably is simple. I have tried directly creating a media object and also creating a file in the temp and converting to a media object. I have googled the issue with creating binary file data. My email results are always with a carriage return to windows(CRLF) instead of unix(LF). This is my latest code sample, trying to force the creation of a ‘\n’ by first creating a temp file then sending it through as media object. To no avail. I’m not sure what I am doing wrong. Thanks so much in advance.
Code Sample:

# this is a formatted code snippet.
# paste your code between ```
  new_line = '\n'
  txtstring = 'Supplier Name = {}{}PO Number = {}{}Date = {}{}Product_0 = {}'.format(supplier_name, new_line, po_number, new_line, str(date), new_line, category)
  txt_name = po_number + ".txt"

  with open(f'/tmp/{po_number}.txt', 'w+', newline="\n") as f:
    # Write the byte contents of the media object to 'tmp/my-file.txt'
    # (we opened the file in binary mode, so f.write() accepts bytes)
    f.write(txtstring)

  txt_media_object = anvil.media.from_file(f'/tmp/{po_number}.txt', "text/plain")
                   
  anvil.email.send(
    from_address="something@example.com",
    from_name="Weird Report",
    to="someone@example.com",
    subject="Test",
    text="Attached are the weird reports",
    attachments=[pdf, txt_media_object]
  )

Welcome to the Forum!

Just off the top of my head, what happens when you open the file in binary mode, e.g., 'w+b' instead of the default text mode?

Edit: see open()

As mentioned in the Overview, Python distinguishes between binary and text I/O.

In C, the distinction between text and binary modes is that

  • binary mode transfers data unfiltered, byte for byte
  • text mode translates between in-memory newlines (\n, the ASCII linefeed character) and external representations (e.g, \r\n, the ASCII pair carriage-return + linefeed).

Hi @p.colbert thanks for your reply, much appreciated.

I narrowed it down to the send mail function in anvil anvil.email.send(). I created the mediaBlob object and sent it as downloadable. It was indeed a unix character return. I then set the email settings to personal smtp settings to check if maybe was something to do with the server setup on Anvil’s side. This still was in windows(CRLF). I am guessing - not trialed, that it has something to do with the way it attaches media objects with the send mail function. I am now going to attempt to write my own file attachment function and see what the result is. Not sure how I’ll attempt this but willing to play around.

Thanks for your edit :grinning: I tried both with binary and text mode. Initially I thought it definately had something to do with that. So ran a couple of tests around this idea locally and through Anvil.

I would do it the easy way, without even specifying the newline argument in the open:

txtstring = f'Supplier Name = {supplier_name}\r\nPO Number = {po_number}\r\nDate = {date}\r\nProduct_0 = {category}'

That’s pretty much what he’s getting at the destination – and what he’s trying to prevent.

1 Like

Oops… I need better glasses!

1 Like

:eyeglasses:’.replace(“\r\n”, “\n”)

:sweat_smile:

1 Like

No worries. Happens to me every day. Misremember “a+b” as “b+a”, and it doesn’t make any difference. It’s the “a-b” we have to watch out for.

Hi @stefano.menci Thanks anyways for your assistance. :rofl: :rofl: :rofl:. I have decided to conclude that it might be easier to try request for the prod system to accept both. I attempted with your idea, just changed to \n instead but it is still coming up as \r\n. I attempted to write my own email service with smptlb but decided after awhile it was not worth the effort and performance dip, unless the prod environment comes back and are unable to accept both. My rationale is 99.999% of Anvil users would never need to ever worry about this. Thanks @stefano.menci @p.colbert so much for all the assistance. Much appreciated.

Instead of sending the file as an email attachment, can you just send a link to the attachment (for example, to an API function that pulls the file from a database table where it’s been saved)? I always prefer to do that anyways wherever possible, so that the file doesn’t just exist in someone’s email account where it can get deleted.

1 Like

Hi @nickantonaccio. Thanks so much for your feedback. Yeah that is a good point and well worth considering. Deletions will always be a issue. Ill probably need to reassess how I am performing this function. The upside is that it also possibly solves the issue in question. Ill have a look at your suggestion and verify the feasibility. Thanks again.