Send email with bash script

I would like to use this in a script, but it needs Ctrl D to send it.

  ssmtp [email protected] < Test_Email.txt

Is there a way to send ctrl D to it and have it execute?

Ctrl-D is an EOF character.
It tells where is the End Of File

In the scripts it's used like this:

cat << EOF
Here
is some
text.
EOF

From the manual man ssmtp it looks like -t option is what you are looking for.
From here sSMTP - ArchWiki it looks like message has a header and content.

CONTENT="I would like to use this in a script, but it needs Ctrl D to send it."

ssmtp -t << EOF
To: [email protected]
From: [email protected]
Subject: fixit asking for help

$CONTENT

Cheers,
fixit
EOF

This works, but I get an error message.

  /home/andy/bin/Send_Email.sh: line 18: warning: here-document at line 8 delimited
  by end-of-file (wanted `EOF')

#!/bin/bash
# Ubuntu_Mate 18.04 LTS

CONTENT="I would like to use this in a script, but it needs Ctrl D to send it."

ssmtp -t << EOF
To: [email protected]
From: [email protected]
Subject: fixit asking for help

    $CONTENT

    Cheers,
    fixit
    EOF

EOF must be at the begining of the line. Remove the leading spaces in front of EOF

[code]#!/bin/bash

Ubuntu_Mate 18.04 LTS

CONTENT=“I would like to use this in a script, but it needs Ctrl D to send it.”

ssmtp -t << EOF
To: [email protected]
From: [email protected]
Subject: fixit asking for help

$CONTENT

Cheers,
fixit
EOF[/code]

No errors, but only recipient and subject came thru?

from here sSMTP - ArchWiki
Note: When using mail interactively, after typing the Subject: subject and other headers, hit enter twice, and then type the body. Hit Ctrl+d on a blank line to end your message and automatically send it out.

Maybe there need to be one or two empty lines between the header and the content (message body).

1 Like

Thanks.

After entering 2 blank lines under Subject, it now works.

I usually install mutt and use that:

mutt -s “Subject” -a /path/to/attachment – [email protected] < /path/to/body.txt

Just to give an alternative

1 Like