1

I have written a bash script and want it to send me an email with a message body that has a multi-line message.

subject="subject1"

I have tried multiple messages, here are just a couple that I can remember:

message="temporary message"
message="temporary message
         next line here"
message=printf '%s\n' "Note that $start and $end use server time. \n $timeelapsed"
message=echo -e . . .

Here are just four of many variations of the mail command I have tried in combination with the messages above:

mail -s "$subject" emailadd1@google.com <<< "$message"
mail -s "$subject" emailadd1@google.com <<< $( echo -e "Note that $start and $end use server time. \n the end")
mail -s "$subject" emailadd1@google.com <<< $( echo -e "Note that $start and $end use server time. \n the end")
mail -s "$subject" emailadd1@google.com <<< $( echo -e "Note that $start and $end use server time. \r the end")

None have worked. The result is always a one-line message in the email body.

I have already tried everything in this related post without luck: How to insert new line in the email using linux mail command?

Can someone please write a complete solution for me?

user2205916
  • 3,196
  • 11
  • 54
  • 82
  • 2
    SMTP uses CRLF line endings, not CR alone and not LF alone. In fact, the RFC specifically says you MUST NOT send a lone CR or lone LF. Also, messages should end in two CRLF, not one. Also see [RFC 5321](https://tools.ietf.org/html/rfc5321), Section 2.3.8, *Lines*. – jww Dec 12 '19 at 10:28
  • I'm sure that is helpful to someone, but it is beyond me. What bash code would accomplish what I am seeking to accomplish? – user2205916 Dec 12 '19 at 10:38
  • 2
    I did not find a good duplicate, which is kind of surprising... For the line ending issue, maybe try: `echo -en "Test CRLF endings\r\n"`. Looking at it with `hexdump` shows the expected line endings. `echo -en "Test CRLF endings\r\n" | hexdump -C` shows `54 65 73 74 20 43 52 4c 46 20 65 6e 64 69 6e 67 73 0d 0a`. `echo -en` should work on Linux, OS X and Windows. – jww Dec 12 '19 at 10:45
  • If you have many lines I suggest you write the message in a multi-line quoted string (where you just use a literal linefeed to represent the linefeed) then use `sed` to insert `\r` at the end of each line and an extra line at the end. See [this ideone](https://ideone.com/3ou3y3); in the output `^M` represents the `\r` and `$` the `\n` – Aaron Dec 12 '19 at 10:54
  • Several of your attempts would have worked fine if `$message` had actually contained a multi-line string. – tripleee Dec 12 '19 at 16:23
  • @jww Any competently written SMTP client will transparently convert from the system's native line-ending convention to the format SMTP requires during transport. Conversely, any reasonable MUA will save attached text files using the system's native convention. (There are corner cases where you can force a literal interpretation, like specifying the MIME content type as `application/octet-stream` instead of `text/plain`.) – tripleee Dec 12 '19 at 16:26
  • @tripleee Can you share a complete solution to my question instead of closing it? – user2205916 Dec 12 '19 at 23:25
  • The first command in your attempt should work, if you insist it doesn't, there are probably details you are not revealing. See if you could reduce this to a [mre] with the first of your attempts and a properly initialized multi-line variable (the last two initialization attempts are broken as you can easily verify yourself) and include the generated message and/or pertinent error messages; that should be sufficient to reopen this. – tripleee Dec 13 '19 at 05:46

1 Answers1

1

Tested on MacOS with Bash 3.2

mail -s "$subject" MyTestEmail@gmail.com <<< $(printf "%s\r\n%s\n" "Note that $start and $end use server time"  "the end")

This is a screen shot from gmail of the email received

Screen Shot from Gmail - Email Received

David Walton
  • 321
  • 4
  • 10