Excel attachment in e-mail

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Excel attachment in e-mail
# 1  
Old 11-08-2017
Excel attachment in e-mail

i am trying to get an SQL output into an excel attachment and the other output into mail body as excel format. i am getting the excel format in the mail body, but i am not getting the excel attachment. and the output is coming in one cell in the excel. is it possible to get the excel output with Bash script. if so can anyone please help me on this.

Thanks.

Last edited by maddelav; 11-08-2017 at 06:39 AM..
This User Gave Thanks to maddelav For This Post:
# 2  
Old 11-08-2017
Dear member,
I have a few to questions pose in response first:-
  • What have you tried so far?
  • Have you searched for existing threads on attachments?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Thanks, in advance,
Robin
# 3  
Old 11-08-2017
Given the vague request, I'm afraid I can give a vague answer: Yes, it's deemed possible if .csv (text) files are acceptable admitting the fact that EXCEL binary data are not usually written by *nix tools.
# 4  
Old 11-08-2017
Hi, Initially i had generated CSV through SQL and then trying to convert CSV to excel. here i am getting the output in one cell or it is throwing an error as excel format corrupted. i am not having any tools to convert. so trying to convert it manually and its format is corrupted once i the file is sent to the mail.and OS is windows and Linux server version RHEL 6.3

Thanks in advance.
# 5  
Old 11-08-2017
Data please. Errors welcome.
# 6  
Old 11-08-2017
Then I would suggest you have something wrong, but sadly I cannot connect to your server to diagnose it today. If you actually show us what you have tried and what errors you get then we might be able to help.
Otherwise, it's like ringing a mechanic to say just "My car doesn't work" and hanging up. We now into a long list of questions trying to guess symptoms to get somewhere near to a cause.

Please can you show us what you have tried and describe & show us the output/errors.




Thanks, in advance,
Robin
# 7  
Old 11-08-2017
Code:
att_csv="/authlistener/ProdA/service/attchment.csv"
att_op="/authlistener/ProdA/service/attachment.xml"
from="venkata.maddela@cgi.com"
to="venkata.maddela@cgi.com"
subject="test with attachment"
boundary="ZZ_/afg6432dfgkl.94531q"
body="This is the body of our email"
att_query="SELECT DECODE(SUBSTR(TRAN.iin,5,2),'25','34',mbr.mbr_no) Country_No,
       DECODE(SUBSTR(TRAN.iin,5,2),'25','Hungary',mbr.name) Country_Name,
    CASE TRAN.DENY_fLAG
    WHEN 'N' THEN 'Approved'
    ELSE 'Declined'
   END Status,
     SUM (TRAN.TRAN_AMOUNT) TOTAL_AMOUNT, COUNT(*) TOTALTRANSACTIONS
     FROM TRAN
     LEFT OUTER JOIN MBR ON substr(TRAN.iin,5,2)    = (MBR.mbr_no)
     LEFT OUTER JOIN CONDITION ON TRAN.CONDITION_ID = condition.condition_id
         WHERE POST_DATE BETWEEN  TO_DATE('20170601', 'yyyymmdd')
      AND  TO_DATE('20170630', 'yyyymmdd')
      AND  TRAN.post_ts  BETWEEN TO_DATE('01 JUN 2017 00:00:00', 'DD MON YYYY HH24:MI:SS') AND TO_DATE('30 JUN 2017 23:59:59', 'DD MON YYYY HH24:MI:SS')
     GROUP BY DECODE(SUBSTR(TRAN.iin,5,2),'25','34',mbr.mbr_no),
                  DECODE(SUBSTR(TRAN.iin,5,2),'25','Hungary',mbr.name),
                          TRAN.DENY_fLAG
         ORDER BY Country_Name;"
CON_STRING="atlas/atlas@atlasprd"

cat > $att_op <<EOF
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author></Author>
  <LastAuthor></LastAuthor>
  <Created>2017-08-13T11:58:30Z</Created>
  <LastSaved>2017-08-13T12:07:08Z</LastSaved>
  <Company>CGI-INDIA</Company>
  <Version>14.00</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>4680</WindowHeight>
  <WindowWidth>14355</WindowWidth>
  <WindowTopX>360</WindowTopX>
  <WindowTopY>120</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
EOF

getResultSet()
{
#echo -e "Query :=========>"$QUERY
sqlplus -s $CON_STRING <<EOF
set term off
set echo off
set underline off
set pagesize 0
set sqlprompt "
set lines 1000 pages 1000
set linesize 1000
set colsep ","
set trimspool on
set heading on
set newpage 0
set headsep off
set feedback off
spool tmp.csv
$att_query
spool off
EOF
}

convertATTCSV2HTML()
{
[ "$#" -ne 1 ] && f_Usage && exit -1

file=$1

echo "<table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="10" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">"
head -n 1 $file | \
    sed -e 's/^/<Row ss:Index="5"><Cell ss:Index="2"><Data ss:Type="String">/' -e 's/,/<\/Data><\/Cell><Cell><Data ss:Type="String">/g' -e 	
's/$/<\/Data><\/Cell><\/R    ow>/'
tail -n +2 $file | \
    sed -e 's/^/<Row ss:Index="5"><Cell ss:Index="2"><Data ss:Type="String">/' -e 's/,/<\/Data><\/Cell><Cell><Data ss:Type="String">/g' -e 					
's/$/<\/Data><\/Cell><\/R    ow>/
'
echo "</table>"
}

#trigger SQL Script
getResultSet

#Converting CSV to HTML Table format
#convertATTCSV2HTML tmp.csv >> $att_op

cat >> $att_op <<EOF
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Selected/>
   <TopRowVisible>1</TopRowVisible>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>10</ActiveRow>
     <ActiveCol>1</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Sheet2">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Sheet3">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>
EOF

get_mimetype(){
  # warning: assumes that the passed file exists
  file --mime "$1" | sed 's/.*: //'
}

sendWithAttachment()
{
declare -a attachments

attachments=( "$1" )

# 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 "Warning: attachment $file not found, skipping" >&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   # one may also use -f here to set the envelope-from
}

#unix2dos $att_op $att_op
mv tmp.csv tmp.xls
sendWithAttachment tmp.xls

#rm tmp.csv


Placed XML template in the script and the output of the script should merged in XML template and the template should e mailed. but the output is corrupted.

Thanks.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) when displaying full line and multi-line sample input, output, and code segments.


---------- Post updated at 07:38 AM ---------- Previous update was at 07:22 AM ----------

yes given the same path..

Thanks.

Last edited by Don Cragun; 11-08-2017 at 08:24 AM.. Reason: Change ICODE tags to CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sending Excel Files as attachment using Mail

Hie, I need to attach an .xlsx file as an attachment to a mail. I have used the mail option but i dont think there is anything for attachment. Can you show me how else can i do it? I am not allowed to install mutt since it is a workplace and they have their restrictions. And its a Bash Shell (2 Replies)
Discussion started by: barryallen
2 Replies

2. Shell Programming and Scripting

Excel file attachment showing data in single column

Hi All, found similar posts, but not exatcly what i wanted. I have an text file like below and am trying to send mail as an excel file but when i get the excel file as the attachment, all the data is coming in the first column. I need below data in 4 columns. Unix file Name,ID,Trade,Date... (3 Replies)
Discussion started by: robinbannis
3 Replies

3. Shell Programming and Scripting

mailx not sending excel attachment properly

Hi, I am trying to send email with attachment using mailx command. I am using the folowing command: uuencode XX_HWSW_BUYERWISE_88963631_1.xls XX_HWSW_BUYERWISE_88963631_1.xls | mailx -s "Test Mail as Attachment" oracleams@xyz.com I get the email in the inbox. However, when I try to open the... (5 Replies)
Discussion started by: asp_julius
5 Replies

4. Shell Programming and Scripting

MIME - HTML mail with Excel attachment - Please help!

#!/bin/ksh ( echo "MIME-Version: 1.0" echo "Content-Type: multipart/mixed; boundary=frontier" echo "--frontier" echo "Content-Type: text/html" echo "Content-Disposition: inline" echo "\n" echo "<html><body><h1>this is html formatted text</h1></body></html>" echo "--frontier" echo... (1 Reply)
Discussion started by: thulasidharan2k
1 Replies

5. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

6. Shell Programming and Scripting

Mail attachment with unix mail

Hi Could someone help me with the details on how to send an attachment through mail in unix. I have an html file in my unix machine and I want this to be send to some mail id, plese help with the steps. Regards Ajay (2 Replies)
Discussion started by: ajaykumarboyana
2 Replies

7. UNIX for Dummies Questions & Answers

Excel attachment in e-mail

Hi, I have to send the query output as an excel attachmet from UNIX Shell script, for this i have written following shell script and sql file this shell script has created the ho.xls file with correct query output but it doesn't generate the mail that shold contain query output as an excel... (5 Replies)
Discussion started by: karthikeyanjo
5 Replies

8. UNIX for Advanced & Expert Users

mailx commannd - Mail and Attachment in same mail

Hi , I am using mailx command for sending the mails. Rightnow I am sending the attachment (by using uuencode $filename) as a seperate mail.I wanna send the attachment also with the same mail. (6 Replies)
Discussion started by: sharif
6 Replies

9. Shell Programming and Scripting

Send email with attachment in form of excel in unix

Hi, I have a shell script which send email with an attachment in the form of an email. However, the when I open the attachment, all the data comes in one column. How do I format the data in the excel sheet while sending the email? Thanks (8 Replies)
Discussion started by: bdebroy
8 Replies

10. UNIX for Dummies Questions & Answers

mail attachment

This question has been asked many time before. I did search other questions. But was not able to work out. Really sorry for repeating the same question. I have mail and mailx and sendmail installed. It Tried this uuencode FILENAME.txt FILENAME.txt | mail mymail@yahoo.com the attachment... (3 Replies)
Discussion started by: sushrut
3 Replies
Login or Register to Ask a Question