Is it possible to send multiple attachments with uuencode and sendmail?
In a script I have a variable containing the files that need to be attached to a single e-mail like:
$attachments=attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf
Also a $template variable like:
$template="Subject: This is the subject
From: no-reply#domain.com
To: %s
Content-Type: text/plain
This is the body.
"
I have come up with:
printf "$template" "$recipient" |
sendmail -oi -t
Somewhere within this I must attach everything in the $attachments variable?
uuencode attachemnts and send via sendmail
Sending MIME attachemnts is better.
uuencode is simpler to implement in scripts but email some clients DO NOT support it.
attachments="attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf"
recipient='john.doe#example.net'
# () sub sub-shell should generate email headers and body for sendmail to send
(
# generate email headers and begin of the body asspecified by HERE document
cat - <<END
Subject: This is the subject
From: no-reply#domain.com
To: $recipient
Content-Type: text/plain
This is the body.
END
# generate/append uuencoded attachments
for attachment in $attachments ; do
uuencode $attachment $attachment
done
) | /usr/sbin/sendmail -i -- $recipient
For what its worth, mailx also works well.
mailx -s "Subject" -a attachment1 -a attachement2 -a attachment3 email.address#domain.com < /dev/null
Related
I use the code below and it's working fine.
dl=xyz#somebody.com
mail -t $dl << EOM
Subject: Report Status $(date)
Content-Type: text/html
mail text
EOM
However, when I try to get the mail id from file dl.txt, no mail is sent.
dl='/script/user/dl.txt'
mail -t $(echo cat $dl) << EOM
subject: Report Status $(date)
content-Type: text/html
mail text
EOM
Note: text file contains mail ids.
If you try to run
dl='/script/user/dl.txt'
echo cat $dl
The output will be
cat /script/user/dl.txt
Remove echo or better change code like this
to=$(cat '/script/user/dl.txt')
mail -t $to << EOM
Subject: Report Status $(date)
Content-Type: text/html
mail text
EOM
I wish to have a portable Unix way of programmatically sending an e-mail with an attachment. The "standard" Unix way of sending an e-mail seems to be mail(1), but there seems to be no portable way of specifying an attachment. (Yes, I know that some versions of mail(1) have an -a or an -A option, but that's not standard, so I cannot rely on it.)
I'll be using ruby for this, but I want a generic solution, i.e., a special ruby configuration involving (say) using Net::SMTP and specifying details like the SMTP server &c. should not be in the application, but the application should issue a command like mail <some other stuff here> and from there the system should deliver the mail (with a suitable ~/.mailrc or whatever).
mail command will work only if it can find a SMTP host. There is a number of ways it can be done (sSMTP, Postfix, sendmail). The most "normal" way is to have sendmail running on your computer, which means localhost itself is an SMTP server. I don't know of a way of getting the SMTP configuration automatically if you don't have sendmail installed.
Therefore, assuming you know the SMTP server (localhost in this example), from Tutorials Point a pure Ruby mail attachment:
require 'net/smtp'
filename = "/tmp/test.txt"
# Read a file and encode it into base64 format
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m") # base64
marker = "AUNIQUEMARKER"
body = <<EOF
This is a test email to send an attachement.
EOF
# Define the main headers.
part1 = <<EOF
From: Private Person <me#fromdomain.net>
To: A Test User <test#todmain.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary = #{marker}
--#{marker}
EOF
# Define the message action
part2 = <<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{body}
--#{marker}
EOF
# Define the attachment section
part3 = <<EOF
Content-Type: multipart/mixed; name = \"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename = "#{filename}"
#{encodedcontent}
--#{marker}--
EOF
mailtext = part1 + part2 + part3
# Let's put our code in safe area
begin
Net::SMTP.start('localhost') do |smtp|
smtp.sendmail(mailtext, 'me#fromdomain.net', ['test#todmain.com'])
end
rescue Exception => e
print "Exception occured: " + e
end
I understood how I can send a HTML message with sendmail doing:
(
echo "From: me#example.com";
echo "To: you#example.com";
echo "Subject: this is my subject";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "My <b>HTML message<\b> goes here!";
) | sendmail -t
I also manages to send an attachement with mail doing
uuencode file.pdf file.pdf | mail -s "my subject" you#example.com
but I fail to send an HTML message with an attachement (.pdf).
Note that I failed to install mutt or mpack (using homebrew) so far so I would love a solution that works with mail, mailx or sendmail. I am on Mac OS X 10.11
What you need to do is use multipart mime.
Your Content-Type should be something like:
Content-Type: multipart/mixed; boundary=multipart-boundary
Your multipart-boundary can be any string you like.
Then you output a line "--multipart-boundary" followed by headers, then body for each part.
For example:
--multipart-boundary
Content-Type: text/html
My <b>HTML message<\b> goes here!
--multipart-boundary
Content-Type: application/pdf
Content-Disposition: attachment; filename=file.pdf
Content-Transfer-Encoding: base64
**cat your base64 encoded file here **
--multipart-boundary--
The extra two dashes at the end mark the end of last part. You can add as many attachments as you like.
I am sending mail from a ruby script as shown below -
body = File.new "body.html","w"
body.puts "Result body"
cmd = "mutt -e \"set content_type=text/html\" -s \"Automation Result\" -a \"Result.xls\" -- xxx#yyy.com < body.html"
system(cmd)
it is sending the mail with attachment but with a blank body.
Strange thing is when I do
mutt -e "set content_type=text/html" -s "Automation Result" -a
"Result.xls" -- xxx#yyy.com < body.html
it sends the email with the attachment and html body. Any idea what's wrong and how to fix it?
Even
mail = Mail.new do
from 'automation_root#bidstalk.com'
to 'maitreya#bidstalk.com'
subject 'Hello'
body File.read('body.html')
add_file :filename => 'Result.xls', :content => File.read('Result.xls')
charset = "UTF-8"
end
is sending mails without body.
The mistake I was doing was I was opening the file, writing to it but not closing it before sending the mail so it was basically not saved and was 'null' when I was sending the mail.
just added -
body.close before the mutt statement and it worked fine.
In my script i want to send a mail with some subject, some text as body along with csv file as attachement
My problem is subject has special characters in portuguese language
like this
Subject: Relatório de utilização do QRCODE
i am using sendmail command to send mail because i need to change sender name(not email id)
I tried this :
Subject=Relatório de utilização do QRCODE
mnth=$(date '+%m/%Y' --date="1 month ago")
echo 'mês:'$mnth>>mailBody.html
echo 'contagem de registros:'11090>>mailBody.html
cat mailBody.html>out.mail
echo "$mnth"
uuencode QR_Log.csv QR_Report_$fname.csv >> out.mail
sendmail -F "xyzname" "$subject" -f "receiver#abc.com" <out.mail
echo "mail sent"
when i run the above script i am getting message like this :
Syntax error in mailbox address "Relat??rio.de.utiliza????o.do.QRCODE"
(non-printable character)
mail sent
How can i achieve this please Help me...
Help is very much appreciated. I'll just wait
Thanks in advance
I wrote a shell script like this and I got a valid title. Try to rewrite the code for sending mail like MIME:
#!/bin/bash
echo 'To: you#domain.net'>>test.html
echo 'From: Some User <user#domain.net>'>>test.html
echo 'Subject: Relatório de utilização do QRCODE'>>test.html
echo 'MIME-Version: 1.0'>>test.html
echo 'Content-Type: text/html; charset="utf-8"'>>test.html
echo 'Content-Disposition: inline'>>test.html
echo ''>>test.html
echo '<span style="color:red;">Your message goes here</span>'>>test.html
sendmail -i -t < test.html
rm test.html
Let me know if this helped :)
Below is my old answer...
Not a linux guy but this may help you. First you must encode the subject to base64. For example:
echo 'your subject' | openssl base64
Let's say you've put the encoded string into $subject variable. Next you set the subject like this when sending email:
"=?UTF-8?B?$subject?="
Basically try to put =?UTF-8?B? before the base64-encoded subject and ?= after it without spaces.
As I said I'm not too much of a linux guy but you'll manage :)
Let me know if it helped.
rfc2045 - (5) (Soft Line Breaks) The Quoted-Printable encoding REQUIRES that encoded lines be no more than 76 characters long.
For bash shell script code:
#!/bin/bash
subject_encoder(){
echo -n "$1" | xxd -ps -c3 |awk -Wposix 'BEGIN{
BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
printf " =?UTF-8?B?"; bli=8
}
function encodeblock (strin){
b1=sprintf("%d","0x" substr(strin,1,2))
b2=sprintf("%d","0x" substr(strin,3,2))
b3=sprintf("%d","0x" substr(strin,5,2))
o=substr(BASE64,b1/4 + 1,1) substr(BASE64,(b1%4)*16 + b2/16 + 1,1)
len=length(strin)
if(len>1) o=o substr(BASE64,(b2%16)*4 + b3/64 + 1,1); else o=o"="
if(len>2) o=o substr(BASE64,b3%64 +1 ,1); else o=o"="
return o
}{
bs=encodeblock($0)
bl=length(bs)
if((bl+bli)>64){
printf "?=\n =?UTF-8?B?"
bli=bl
}
printf bs
bli+=bl
}END{
printf "?=\n"
}'
}
SUBJECT="Relatório de utilização"
SUBJECT=`subject_encoder "${SUBJECT}"`
echo '<html>test</html>'| mail -a "Subject:${SUBJECT}" -a "MIME-Version: 1.0" -a "Content-Type: text/html; charset=UTF-8" you#domain.net