send email with attachment using Unix - unix

I am using the following script to send an email from Unix sever. I have a requirement to attach a file which is at the same location where script is available - /home/app111/attachment.csv.
Could you please help me how to send the file in attachment?
`CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
awk ' BEGIN {
print "To: XXXX#gmail.com"
print "From: YYYY#gmail.com"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: PO file '$CUR_DATE'"
print "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
print "<br>Thanks,</br></font></body></html>"
} ' | sendmail -t`

1
Using mailx -a if mailx -a feature is supported
2
Using uuencode filenm filenm | mailx s#abc.com
3
mutt -a filenm a#abc.com

If you really want to use sendmail (and not mail or mutt), you'll have to encode your attachment in base64 and concatenate it to your message, along with boundaries and the whole nine yards. There is a great article describing exactly what you want to do here with a code example:
http://backreference.org/2013/05/22/send-email-with-attachments-from-script-or-command-line/
If you're on a flavor of Unix or Linux that has mutt or mail, I would definitely recommend one of the two instead of sendmail as it will be a lot easier (and these solutions are also described in the posted article). Here is an example of how you could do it with mail:
CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
to="XXXX#gmail.com"
from="YYYY#gmail.com"
content_type="text/html"
file_to_attach="/home/app111/attachment.csv"
subject="PO file '$CUR_DATE'"
read -r -d '' body << 'EOF'
<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>
<br>Thanks,</br></font></body></html>
EOF
mail -A "$file_to_attach" --content-type "$content_type" -s "$subject" -r "$from" "$to" <<< "$body"

Try this:
MAILFROM="YYYY#gmail.com"
MAILTO="XXXX#gmail.com"
SUBJECT="PO file '$CUR_DATE'"
MAILPART_BODY=q1w2e3r4t5 ## Generates Unique ID
MAILPART=q1qw2ew3r4t35 ## Generates Unique ID
ATTACH="/home/app111/attachment.csv"
(
echo "From: $MAILFROM"
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
echo ""
echo "--$MAILPART"
echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
echo ""
echo "--$MAILPART_BODY"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "You need to enable HTML option for email"
echo "--$MAILPART_BODY"
echo "Content-Type: text/html; charset=ISO-8859-1"
echo "Content-Disposition: inline"
echo "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
echo "<br>Thanks,</br></font></body></html>"
echo "--$MAILPART_BODY--"
echo "--$MAILPART"
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
echo ""
uuencode $ATTACH $(basename $ATTACH)
echo "--$MAILPART--"
) | sendmail -t

Related

sendmail with zip is corrupting first file in the zip

I am trying to send a mail with zip file attached from an unix box. I am limited to use sendmail utility. i ziped the files using the command
zip test.zip 1.html 2.html 3.html
and when trying to send mail with below commands. One of the three files (first file) is not opening properly. but the rest 2.html and 3.html is working fine.
I am getting the error as "Unavailable Data: 1.html"
(
echo "From: from#from.com"
echo "To: to#to.com"
echo "Subject: subject"
echo "Mime-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="X12345"'
echo '--X12345'
echo "Content-Type: application/zip;"
echo "Content-Transfer-Encoding: base64"
echo "Content-Disposition: attachement; filename=test.zip"
base64 test.zip
echo '--X12345'
) | sendmail -t
Can some please help. Thanks in advance.
You failed to provide empty line to mark end of main headers and end of mime part header.
(
cat - <<END
From: from#from.com
To: to#to.com
ubject: subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="X12345"
--X12345
Content-Type: application/zip;
Content-Transfer-Encoding: base64
Content-Disposition: attachement; filename=test.zip
END
base64 test.zip
echo '--X12345'
) | /usr/sbin/sendmail -t

HTML body and XLSX attachment in mail via UNIX using SENDMAIL command

I am trying to send a mail via UNIX with HTML body and xlsx file as attachment, but I am not able to attach the file with the mail, can anybody share some sample code? This is what I have tried, I have to send the XLSX file as attachment for simplicity I am trying it with CSV,
Unix var file is a CSV file which contains data as abc,xyz#gmail.com
echo $file
while read LINE
do
echo $LINE;
fund_provider_code=`echo $LINE | awk -F',' '{print$1}'`
len=`echo $fund_provider_code | awk '{print length}'`
length=`expr $len + 2`
email_list=`echo $LINE | cut -c $length-`
echo fund_code=$fund_provider_code
email=$email_list
(BOUNDARY='=== This is the boundary between parts of the message. ==='
ATTACHMENT="$attachment_file"
SUBJECT="$subject_text"
VERSION=1.0
print - 'To:' ${email_list}
print - 'Subject:' ${SUBJECT}
print - 'MIME-Version: 1.0'
print - 'Content-Type: MULTIPART/MIXED; '
print - 'Content-Type: TEXT/HTML; charset=US-ASCII'
print -
awk -v message1="${msg1}" ' BEGIN {
print "<html><body>"
print "<style>"
print "p.MsoNormal, li.MsoNormal, div.MsoNormal \{mso-style-parent:\"\"; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt;font-family:\"Arial\"; mso-fareast-font-family:\"MSMincho\";\}"
print "</style>"
print "<div class=Section1>"
print "<p class=MsoNormal style=\"mso-layout-grid-align:none;text-autospace:none\"><span style=\"font-size:12.0pt;font-family:'Arial';mso-bidi-font-family:'Arial'; color:Black\">Dear All,<o:p></o:p></span></p>"
print "</div></body></html>"
}' email_body.txt;
print -
print - "--${BOUNDARY}--"
print - 'ATTACH="/projects/dit/edw/EDW_OUTBOUND_FEEDS/bin/email.csv"'
print - 'Content-Type: TEXT/HTML, multipart/mixed, text/html, application/octet-stream ; name=email.csv'
print - "Content-Transfer-Encoding: base64"
print - 'Content-Disposition: attachment; filename=abc.csv'
uuencode "/projects/dit/edw/EDW_OUTBOUND_FEEDS/bin/email.csv" email.csv
print -
print - "--${BOUNDARY}--"
) | sendmail ${email_list}
done < "$file"
It looks like you have a discrepancy in the filename you want to send. The below assume you want to send file locally defined as:
/projects/dit/edw/EDW_OUTBOUND_FEEDS/bin/email.csv
--- however, you hadn't defined it properly, so I fixed the assignment so:
ATTACH="/projects/dit/edw/EDW_OUTBOUND_FEEDS/bin/email.csv"
Or did you want to use ATTACHMENT? I think you must clean this code.
I found no use of this line:
ATTACHMENT="$attachment_file"
var subject_text isn't defined, or is it in a part of the script we don't see in your OP?
var attachment_file isn't defined either.
I think you should use the file you want to send in a var: $ATTACH
echo $file
while read LINE
do
echo $LINE;
fund_provider_code=`echo $LINE | awk -F',' '{print$1}'`
len=`echo $fund_provider_code | awk '{print length}'`
length=`expr $len + 2`
email_list=`echo $LINE | cut -c $length-`
echo fund_code=$fund_provider_code
email=$email_list
(BOUNDARY='=== This is the boundary between parts of the message. ==='
ATTACHMENT="$attachment_file"
ATTACH="/projects/dit/edw/EDW_OUTBOUND_FEEDS/bin/email.csv"
SUBJECT="$subject_text"
VERSION=1.0
print - 'To:' ${email_list}
print - 'Subject:' ${SUBJECT}
print - 'MIME-Version: 1.0'
print - 'Content-Type: MULTIPART/MIXED; '
print - 'Content-Type: TEXT/HTML; charset=US-ASCII'
print -
awk -v message1="${msg1}" ' BEGIN {
print "<html><body>"
print "<style>"
print "p.MsoNormal, li.MsoNormal, div.MsoNormal \{mso-style-parent:\"\"; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt;font-family:\"Arial\"; mso-fareast-font-family:\"MSMincho\";\}"
print "</style>"
print "<div class=Section1>"
print "<p class=MsoNormal style=\"mso-layout-grid-align:none;text-autospace:none\"><span style=\"font-size:12.0pt;font-family:'Arial';mso-bidi-font-family:'Arial'; color:Black\">Dear All,<o:p></o:p></span></p>"
print "</div></body></html>"
}' email_body.txt;
print -
print - "--${BOUNDARY}--"
print - 'ATTACH="${ATTACH}"'
print - 'Content-Type: TEXT/HTML, multipart/mixed, text/html, application/octet-stream ; name="'$(basename ${ATTACH})'"'
print - "Content-Transfer-Encoding: base64"
print - 'Content-Disposition: attachment; filename="'$(basename ${ATTACH})'"'
uuencode $ATTACH $(basename ${ATTACH})
print - "--${BOUNDARY}--"
) | sendmail ${email_list}
done < "$file"
From examples I saw, if you use GNU uuencode, you may need to replace the line
uuencode $ATTACH $(basename $ATTACH)
by
uuencode --base64 $ATTACH $(basename $ATTACH)

send inline html charts and diagrams via unix

I am trying to generate some report and send it through unix. This report contains PIE-charts any idea how to inline send charts in mail body.
I tried below code. HTML is working in browser but after I send the HTML over mail it does not shows anything in mail body.
`MAILTO="x#a.com"
FROM='y#x.com'
CONTENT="pi.html"
SUBJECT="test"
(
echo "To:$MAILTO"
echo "From:$FROM"
echo "Cc:$CC"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $CONTENT
) | /usr/sbin/sendmail -t`
content of pi.html ( I have copied the content from the url and pasted in pi.html)
https://developers.google.com/chart/interactive/docs/gallery/piechart
Please suggest a way to handle this.

How to send HTML body email with multiple text attachments using sendmail

I want to send a HTML file as message body and want to attach multiple text files to this email message.
Since html file needs to be sent, sendmail has to be used ( I could not do it using mailx ).
How do you send HTML body email and multiple text attachments using sendmail?
Assuming you have uunecode available in your system you can send email with multiple attachments like this:
#!/bin/bash
...
...
...
BOUNDARY="=== This is the boundary between parts of the message. ==="
{
echo "From: $MAILFROM"
echo "To: $MAILTO"
echo "Subject:" $SUBJECT
echo "MIME-Version: 1.0"
echo "Content-Type: MULTIPART/MIXED; "
echo " BOUNDARY="\"$BOUNDARY\"
echo
echo " This message is in MIME format. But if you can see this,"
echo " you aren't using a MIME aware mail program. You shouldn't "
echo " have too many problems because this message is entirely in"
echo " ASCII and is designed to be somewhat readable with old "
echo " mail software."
echo
echo "--${BOUNDARY}"
echo "Content-Type: TEXT/PLAIN; charset=US-ASCII"
echo
echo "This email comes with multiple attachments."
echo
echo
echo "--${BOUNDARY}"
echo "Content-Type: application/zip; charset=US-ASCII; name="${ZIPFILE}
echo "Content-Disposition: attachment; filename="`basename ${ZIPFILE}`
echo
uuencode $ZIPFILE $ZIPFILE
echo
echo "--${BOUNDARY}--"
echo "Content-Type: application/pdf; charset=US-ASCII; name="${PDFFILE}
echo "Content-Disposition: attachment; filename="`basename ${PDFFILE}`
echo
uuencode $PDFFILE $PDFFILE
echo
echo "--${BOUNDARY}--"
} | /usr/lib/sendmail -t
I don't think sendmail is going to help you with that. Go for a client like mutt, and do e.g. mutt -a file1 -a file2 -- recipient#do.main. Or go for perl.
Here is a bash script I use to send reports I generate to people. They are sent as attachments. Place your HTML in the "body" variable of the script. I will leave the parametrization of the variables up to you.
#!/bin/bash
function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
from="me.last#company.com"
to="some.one#companyBlah.com"
subject="Your Report my Lord"
boundary="=== Boundary ==="
body="The reports are attached to this email"
declare -a attachments
attachments=( "fileOne.out" "fileTwo.out" "fileThree.out" "file-et-cetera.out")
# Build headers
{
printf '%s\n' "From: $from
To: $to
Subject: $subject
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
$body
"
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}--"
} | sendmail -t -oi

How can I send an email through the UNIX mailx command?

How can I send an email through the UNIX mailx command?
an example
$ echo "something" | mailx -s "subject" recipient#somewhere.com
to send attachment
$ uuencode file file | mailx -s "subject" recipient#somewhere.com
and to send attachment AND write the message body
$ (echo "something\n" ; uuencode file file) | mailx -s "subject" recipient#somewhere.com
Here you are :
echo "Body" | mailx -r "FROM_EMAIL" -s "SUBJECT" "To_EMAIL"
PS. Body and subject should be kept within double quotes.
Remove quotes from FROM_EMAIL and To_EMAIL while substituting email addresses.
mailx -s "subjec_of_mail" abc#domail.com < file_name
through mailx utility we can send a file from unix to mail server.
here in above code we can see
first parameter is -s "subject of mail"
the second parameter is mail ID and the last parameter is name of file which we want to attach
mail [-s subject] [-c ccaddress] [-b bccaddress] toaddress
-c and -b are optional.
-s : Specify subject;if subject contains spaces, use quotes.
-c : Send carbon copies to list of users seperated by comma.
-b : Send blind carbon copies to list of users seperated by comma.
Hope my answer clarifies your doubt.
Its faster with MUTT command
echo "Body Of the Email" | mutt -a "File_Attachment.csv" -s "Daily Report for $(date)" -c cc_mail#g.com to_mail#g.com -y
-c email cc list
-s subject list
-y to send the mail
From the man page:
Sending mail
To send a message to one or more people, mailx can be invoked with
arguments which are the names of
people to whom the mail will be sent.
The user is then expected to type in
his message, followed
by an ‘control-D’ at the beginning of a line.
In other words, mailx reads the content to send from standard input and can be redirected to like normal. E.g.:
ls -l $HOME | mailx -s "The content of my home directory" someone#email.adr
echo "Sending emails ..."
NOW=$(date +"%F %H:%M")
echo $NOW " Running service" >> open_files.log
header=`echo "Service Restarting: " $NOW`
mail -s "$header" abc.xyz#google.com, \
cde.mno#yahoo.com, \ < open_files.log
Customizing FROM address
MESSAGE="SOME MESSAGE"
SUBJECT="SOME SUBJECT"
TOADDR="u#u.com"
FROM="DONOTREPLY"
echo $MESSAGE | mail -s "$SUBJECT" $TOADDR -- -f $FROM
Here is a multifunctional function to tackle mail sending with several attachments:
enviaremail() {
values=$(echo "$#" | tr -d '\n')
listargs=()
listargs+=($values)
heirloom-mailx $( attachment=""
for (( a = 5; a < ${#listargs[#]}; a++ )); do
attachment=$(echo "-a ${listargs[a]} ")
echo "${attachment}"
done) -v -s "${titulo}" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://$1 \
-S from="${2}" \
-S smtp-auth-user=$3 \
-S smtp-auth-password=$4 \
-S ssl-verify=ignore \
$5 < ${cuerpo}
}
function call:
enviaremail "smtp.mailserver:port" "from_address" "authuser" "'pass'" "destination" "list of attachments separated by space"
Note: Remove the double quotes in the call
In addition please remember to define externally the $titulo (subject) and $cuerpo (body) of the email prior to using the function
If you want to send more than two person or DL :
echo "Message Body" | mailx -s "Message Title" -r sender#someone.com receiver1#someone.com,receiver_dl#.com
here:
-s = subject or mail title
-r = sender mail or DL

Resources