Sending a mail from a linux shell script
If the server is well configured, eg it has an up and running MTA, you can just use the mail command.
For instance, to send the content of a file, you can do this:
$ cat /path/to/file | mail -s "your subject" your@email.com
man mail
for more details.
Shell script to send email
Yes it works fine and is commonly used:
$ echo "hello world" | mail -s "a subject" someone@somewhere.com
Sending e-mail from bash script
using mailutils
I think the problem is that if you specify -A
, stdin is ignored: https://savannah.gnu.org/bugs/?54992
You can include the body text as an additional attachment:
echo "This is the body of the e-mail" |\
mail address@example.com \
-s "This is the subject" \
--skip-empty-attachments \
--content-type text/plain -A - \
-A /file/path/file.txt
using mutt
Although I don't think mutt is really intended for scripting, it looks like this should work:
echo "this is the body" |\
mutt \
-s "this is the subject" \
-a /file/path/file.txt -- \
address@example.com
Sending HTML mail using a shell script
First you need to compose the message. The bare minimum is composed of these two headers:
MIME-Version: 1.0
Content-Type: text/html
... and the appropriate message body:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
Once you have it, you can pass the appropriate information to the mail command:
body = '...'
echo $body | mail \
-a "From: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com
This is an oversimplified example, since you also need to take care of charsets, encodings, maximum line length... But this is basically the idea.
Alternatively, you can write your script in Perl or PHP rather than plain shell.
Update
A shell script is basically a text file with Unix line endings that starts with a line called shebang that tells the shell what interpreter it must pass the file to, follow some commands in the language the interpreter understands and has execution permission (in Unix that's a file attribute). E.g., let's say you save the following as hello-world
:
#!/bin/sh
echo Hello, world!
Then you assign execution permission:
chmod +x hello-world
And you can finally run it:
./hello-world
Whatever, this is kind of unrelated to the original question. You should get familiar with basic shell scripting before doing advanced tasks with it. Here you are a couple of links about bash, a popular shell:
http://www.gnu.org/software/bash/manual/html_node/index.html
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
Sending email using unix shell scripting
You forgot the quotes:
echo $body | mail $receiver -s "$subj"
Note that you must use double quotes (otherwise, the variable won't be expanded).
Now the question is: Why double quotes around $subj
and not $body
or $receiver
. The answer is that echo
doesn't care about the number of arguments. So if $body
expands to several words, echo
will just print all of them with a single space in between. Here, the quotes would only matter if you wanted to preserve double spaces.
As for $receiver
, this works because it expands only to a single word (no spaces). It would break for mail addresses like John Doe <doe@none.com>
.
How do I send a file as an email attachment using Linux command line?
None of the mutt ones worked for me. It was thinking the email address was part of the attachment. Had to do:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.example
How can I send a mail in linux shell script, using mail, but also awk?
Try the following script file :
#!/bin/bash
your_grade_files="/path_to_file.txt"
while read p; do
FirstName=$(echo $p | awk -F' ' '{print $1}')
LastName=$(echo $p | awk -F' ' '{print $2}')
UserID=$(echo $p | awk -F' ' '{print $3}')
Grade=$(echo $p | awk -F' ' '{print $4}')
if [ "$Grade" -gt 5 ]
then
mail -s "Enter your subject here" $UserID <<< "Dear $FirstName $LastName! On this subject, your grade is [ $Grade ] "
else
mail -s "Enter your subject here" $UserID <<< "Dear $FirstName $LastName! On this subject, your grade was less than 5!"
fi
done < $your_grade_files
Related Topics
How to Extract Only the Raw Contents of an Elf Section
Making Cmake Print Commands Before Executing
"Max Open Files" for Working Process
Linux: Where Are Environment Variables Stored
Should I Use Libc++ or Libstdc++
Bash: Infinite Sleep (Infinite Blocking)
Grep Inside All Files Created Within Date Range
Linux Bash, Camel Case String to Separate by Dash
How to Get Position of Cursor in Terminal
How to Get the Start Time of a Long-Running Linux Process
How to Automatically Add User Account and Password with a Bash Script
How to Monitor the Thread Count of a Process on Linux
Can't Detach Child Process When Main Process Is Started from Systemd
How Clear and Invalidate Arm V7 Processor Cache from User Mode on Linux 2.6.35