I have a requirement like, i need to export the sql query result set to a .csv file and send it as an attachment. but here i need to include the body with html formatting and wanted to include sample rows of the .csv file in body as well in html format.
csv file contains :
Id ID_CNT
a 3
b 6
and i want to display the mail body with sample rows.
I have tried below two ways
mutt -e "set content_type=text/html" -a query_gen23.csv -s "abc" abc#gmail.com
export CONTENT="test.html"
export SUBJECT="$subject"
#(
# echo "To: "
# echo "Subject: $SUBJECT"
# echo "MIME-Version: 1.0"
# echo "Content-Type: text/html"
# echo "Content-Disposition: inline"
# cat $CONTENT
#) | /usr/sbin/sendmail $MAILTO
mutt -s "$subject" -a "$subject".csv -a test.html "$MAILTO" </dev/null
Thanks in Advance!
It's a long time since I had used mutt, but if I remember right, it expects the body of the mail from standard input. In your script, you input-redirect from /dev/null !
I would divide the task into two steps: One creates the body (HTML code with the CSV sample lines), and the second step pipes this file into mutt. I think this is much easier than trying to do everything at once.
Following works fine for me :
#!/usr/bin/ksh
export MAILTO="spam#ebay.com"
export SUBJECT="Mail Subject"
export BODY="/tmp/email_body.html"
export ATTACH="/tmp/attachment.txt"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode -m $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO
Related
Not able to get mail by below command:
cmd = ` ( echo -e "${message}" ) | mail -r "from me-<\`whoami\`#\`hostname\`>" \
-a "From: me#example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "${subject}" ${address}`
where message=`<html>
<head><title></title></head>
<body>\nHi, \\n
<p>
<a href='test'>Approve</a>\\n</p></body></html>`,
Please suggest changes in this. Thanks!!!
I am new to Bash scripting and am trying to modify some code that I picked up from the net. Basically I'm trying to attach multiple files to the same email.
#!/bin/bash
function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
declare -a attachments
attachments=("A_PTDIFF.CVS" "A_PTMISS.CVS" "A_PTNPOS.CVS" "A_PTRCON.CVS"
)
# Build headers
{
printf '%s\n'"From: jrpng
To: tom#gmail.com
Subject: Test new Bash script
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"=== Boundary ===\"
--${=== Boundary ===}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
The reports are attached to this email as requested
"
for file in "${attachments[#]}"; do
[ ! -f "$file" ] && echo "Attachment $file not found, omitting file"
>&2 && continue
mimetype=$(get_mimetype "$file")
printf '%s\n' "--${=== Boundary ===}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
"
base64 "$file"
echo
done
# print last boundary with closing --
printf '%s\n' "--${=== Boundary ===}--"
} | /usr/lib/sendmail -t -oi
Okay when run via bash sendmail54.sh I get the following
sendmail54.sh: line 22: From: jrpng
To: tom#gmail.com
Subject: Test new Bash script
`Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="=== Boundary ==="
--${=== Boundary ===}
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
The reports are attached to this email as requested
: bad substitution
No recipient addresses found in header
No idea what the error message is telling me?
If there is a better approach to what I am trying to achieve I'm more than happy to try that, as stated new to bash scripting so am fumbling around in the dark trying to get this working.
You have a number of basic syntax errors and misunderstandings.
Apparently the original code had a variable boundary="randomstring" and then interpolated that at the right places with --${boundary}. But you attempt to use the boundary string itself inside ${...} which is just nonsense -- what goes between the braces is the name of a variable.
Also, your mail message needs some empty lines in order to be valid. The general structure is
From: whoever
To: wherever
Subject: whatever
Mime-version: 1.0
Content-type: multipart something something; boundary="randomstring"
<- empty line between message headers and first body part
--randomstring
Content-type: of first attachment
x-headers: and etc
<- empty line before here; now the actual attachment data
--randomstring
Content-type: of second attachment
x-headers: and etc
payload of second attachment goes here, again after an empty line
--randomstring--
Piecing together an email message by hand requires you to understand MIME which most people simply don't want to. If you have a sensible mail program like mutt installed, you don't need to understand the details of how exactly to format an SMTP MIME message correctly.
Anyway, here is an attempt at fixing your current code, with comments, and proper indentation.
function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
declare -a attachments
attachments=("A_PTDIFF.CVS" "A_PTMISS.CVS" "A_PTNPOS.CVS" "A_PTRCON.CVS")
{
boundary="--random$$string--"
# You were missing a space after '%s\n'
# Also, I'm breaking this into multiple arguments for legibility
printf '%s\n' \
"From: jrpng" \
"To: tom#gmail.com" \
"Subject: Test new Bash script" \
"Mime-Version: 1.0" \
"Content-Type: multipart/mixed; boundary=\"$boundary\"" \
"X-Comment: # notice the empty line after this one" \
"" \
"--${boundary}" \
"Content-Type: text/plain; charset=\"US-ASCII\"" \
"Content-Transfer-Encoding: 7bit" \
"Content-Disposition: inline" \
"X-Comment: # these headers are all superfluous really" \
"X-Comment: # (default is text/plain us-ascii 7bit inline)" \
"X-Comment: # but the next empty line is important" \
"" \
"The reports are attached to this email as requested" \
""
for file in "${attachments[#]}"; do
[ ! -f "$file" ] &&
echo "Attachment $file not found, omitting file" >&2 &&
continue
mimetype=$(get_mimetype "$file")
printf '%s\n' \
"--${boundary}" \
"Content-Type: $mimetype" \
"Content-Transfer-Encoding: base64" \
"Content-Disposition: attachment; filename=\"$file\"" \
""
base64 "$file"
echo
done
printf '%s\n' "--${boundary}--"
} | /usr/lib/sendmail -t -oi
This question already has answers here:
How do I send a file as an email attachment using Linux command line?
(25 answers)
Closed 4 years ago.
I want to send an email using sendmail command in bash. The email should get it's body by reading Input_file_HTML and it should send same input file as an attachment too. To do so I have tried the following.
sendmail_touser() {
cat - ${Input_file_HTML} << EOF | /usr/sbin/sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: $1
Content-Type: text/html; charset=us-ascii
cat ${Input_file_HTML}
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Content-Disposition: attachment; filename: ${Input_file_HTML}
EOF
}
The above command is giving an email with only the attachment of Input_file_HTML and it is not writing it in the body of email. Could you please help/guide me on same? I am using outlook as the email client. I have even removed the cat command in above command, but it also is not working.
Use mutt instead?
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
To install mutt on Debian systems:
sudo apt-get install -y mutt
EDIT Try this if you can only use sendmail:
sendmail_attachment() {
FROM="$1"
TO="$2"
SUBJECT="$3"
FILEPATH="$4"
CONTENTTYPE="$5"
(
echo "From: $FROM"
echo "To: $TO"
echo "MIME-Version: 1.0"
echo "Subject: $SUBJECT"
echo 'Content-Type: multipart/mixed; boundary="GvXjxJ+pjyke8COw"'
echo ""
echo "--GvXjxJ+pjyke8COw"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
echo "<p>Message contents</p>"
echo ""
echo "--GvXjxJ+pjyke8COw"
echo "Content-Type: $CONTENTTYPE"
echo "Content-Disposition: attachment; filename=$(basename $FILEPATH)"
echo ""
cat $FILEPATH
echo ""
) | /usr/sbin/sendmail -t
}
Use like this:
sendmail_attachment "to#example.com" "from#example.com" "Email subject" "/home/user/file.txt" "text/plain"
I wrote script to send mail automatically using an SMTP connection but when I execute the script, sometimes it works and sometimes it is not sending mail. Behavior is quite ambiguous.
Environment : Linux Server Fedora 14
Mailing Client : Lotus Notes 8.5.2
Please find script below.
# Function for sending email
sendemail(){
date=`date '+%d-%m-%Y'`
dbDir=/var/lib/MYSQLBACKUP/$date
dbname='DBNAME'
log_file="${dbDir}/${dbname}_${date}.log"
attached_file="${dbname}_${date}.log"
echo $log_file
echo $attached_file
encoded_log_file=`cat $log_file | openssl base64`
#echo $encoded_log_file
( echo open 172.40.201.31 25
sleep 8
echo helo 172.40.201.31
echo mail from:Pratik.Vyas#gmail.com
echo rcpt to:Pratik.Vyas#gmail.com
echo data
echo to:Pratik.Vyas#gmail.com
echo from:Pratik.Vyas#gmail.com
echo "subject: SPARC CQ DB Backup Report : $date :"
echo "MIME-Version: 1.0"
#echo "Content-Type: text/plain; charset=UTF-8"
#echo "Please view attached file"
echo "Content-Type: text/x-log;name="$attached_file""
echo "Content-Disposition:attachment;filename="$attached_file""
echo "Content-Transfer-Encoding: base64"
echo $encoded_log_file
echo $1
sleep 15
echo .
echo ^]
echo quit ) | telnet
echo "status:$?"
echo "Hello done"
}
sendemail
Here's a rewrite using /usr/lib/sendmail. This is not necessarily the correct location for your system, but you should be able to adapt it.
# Function for sending email
sendemail(){
date=$(date '+%d-%m-%Y') # prefer $(...) over `...`
dbDir=/var/lib/MYSQLBACKUP/$date
dbname='DBNAME'
log_file="${dbDir}/${dbname}_${date}.log"
attached_file="${dbname}_${date}.log"
echo $log_file
echo $attached_file
encoded_log_file=$(openssl base64 < "$log_file") # notice UUCA fix + quoting
#echo $encoded_log_file
# You should configure sendmail to use 172.40.201.31 as your smarthost
/usr/lib/sendmail -oi -t <<________HERE
to: Pratik.Vyas#gmail.com
from: Pratik.Vyas#gmail.com
subject: SPARC CQ DB Backup Report : $date :
MIME-Version: 1.0
Content-Type: text/x-log; name="$attached_file"
Content-Disposition: attachment; filename="$attached_file"
Content-Transfer-Encoding: base64
X-Ample: notice empty line between headers and body! # <-- look
$encoded_log_file
$1
________HERE
echo "status:$?"
echo "Hello done"
}
sendemail
I would recommend using a library or command line program (like mail or mailx) to send mail instead of trying to implement SMTP in a shell script.
This question already has answers here:
Sending a mail from a linux shell script
(11 answers)
Closed 5 years ago.
I am on linux machine and I monitor a process usage. Most of the time I will be away from my system and I have access to internet on my device. So I planned to write a shell-script that can mail me the output of the process.
Is it possible?
If so how to make a shell-script send me mail?
Please provide a snippet to get started.
Yes it works fine and is commonly used:
$ echo "hello world" | mail -s "a subject" someone#somewhere.com
Basically there's a program to accomplish that, called "mail". The subject of the email can be specified with a -s and a list of address with -t. You can write the text on your own with the echo command:
echo "This will go into the body of the mail." | mail -s "Hello world" you#youremail.com
or get it from other files too:
mail -s "Hello world" you#youremailid.com < /home/calvin/application.log
mail doesn't support the sending of attachments, but Mutt does:
echo "Sending an attachment." | mutt -a file.zip -s "attachment" target#email.com
Note that Mutt's much more complete than mail.
You can find better explanation here
PS: thanks to #slhck who pointed out that my previous answer was awful. ;)
sendmail works for me on the mac (10.6.8)
echo "Hello" | sendmail -f my#email.com my#email.com
mail -s "Your Subject" your#email.com < /file/with/mail/content
(/file/with/mail/content should be a plaintext file, not a file attachment or an image, etc)
#!/bin/sh
#set -x
LANG=fr_FR
# ARG
FROM="foo#bar.com"
TO="foo#bar.com"
SUBJECT="test é"
MSG="BODY éé"
FILES="fic1.pdf fic2.pdf"
# http://fr.wikipedia.org/wiki/Multipurpose_Internet_Mail_Extensions
SUB_CHARSET=$(echo ${SUBJECT} | file -bi - | cut -d"=" -f2)
SUB_B64=$(echo ${SUBJECT} | uuencode --base64 - | tail -n+2 | head -n+1)
NB_FILES=$(echo ${FILES} | wc -w)
NB=0
cat <<EOF | /usr/sbin/sendmail -t
From: ${FROM}
To: ${TO}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=frontier
Subject: =?${SUB_CHARSET}?B?${SUB_B64}?=
--frontier
Content-Type: $(echo ${MSG} | file -bi -)
Content-Transfer-Encoding: 7bit
${MSG}
$(test $NB_FILES -eq 0 && echo "--frontier--" || echo "--frontier")
$(for file in ${FILES} ; do
let NB=${NB}+1
FILE_NAME="$(basename $file)"
echo "Content-Type: $(file -bi $file); name=\"${FILE_NAME}\""
echo "Content-Transfer-Encoding: base64"
echo "Content-Disposition: attachment; filename=\"${FILE_NAME}\""
#echo ""
uuencode --base64 ${file} ${FILE_NAME}
test ${NB} -eq ${NB_FILES} && echo "--frontier--" || echo
"--frontier"
done)
EOF
Well, the easiest solution would of course be to pipe the output into mail:
vs#lambda:~$ cat test.sh
sleep 3 && echo test | mail -s test your#address
vs#lambda:~$ nohup sh test.sh
nohup: ignoring input and appending output to `nohup.out'
I guess sh test.sh & will do just as fine normally.
top -b -n 1 | mail -s "any subject" your_email#domain.com