0

I am using Oracle 11.1 I have this plsql block which sends perfectly fine email with .csv attachment.

But I want to zip it and send. When I change the file extension as .zip, email comes fine as Report.zip ,but I cannot open the zip file, it says corrupted. How do I compress?

DECLARE
    p_email   email%ROWTYPE;
    p_subject VARCHAR2(255)  := 'Weekly Report';
    p_message CLOB;
    l_mailhost VARCHAR2(255) := 'localhost';
    l_mail_conn utl_smtp.connection;

    v_add_src  VARCHAR2(4000);
    v_addr     VARCHAR2(4000);
    slen NUMBER := 1;

    crlf VARCHAR2(2)  := chr(13)||chr(10);
    v_date VARCHAR2(15) := TO_CHAR(TRUNC(SYSDATE) -1,'MM_DD_YYYY');

/*Table header in attachment*/
v_col VARCHAR2(32000):= 'START_DATE'||CHR(166)||'END_DATE'||CHR(166)||'NAME'||crlf;

    CURSOR cur_query
    IS
    SELECT  
        START_DATE, END_DATE, NAME FROM TESTING

    BEGIN
    --SELECT TO_CHAR(TRUNC(SYSDATE) -1,'MM_DD_YYYY') INTO v_date FROM dual;
     SELECT * INTO p_email FROM email WHERE module_name = 'REPORT';
          p_message:= 
            '<html>
                 <BODY>
                 <P> <font color="black",font face ="arial",font size ="2.5">
                Hello All, <br/><br/>
                Attached weekly report <br/>
                <br/>Thank You
                 </P>
                </BODY>
            </html>';

            l_mail_conn := utl_smtp.open_connection(l_mailhost, 25);
            utl_smtp.helo(l_mail_conn, l_mailhost);
            utl_smtp.mail(l_mail_conn, p_email.sender);

            IF(INSTR(p_email.recipients,',') = 0) THEN
                utl_smtp.rcpt(l_mail_conn, p_email.recipients);
            ELSE
            v_add_src := p_email.recipients || ',';
                WHILE(INSTR(v_add_src,',',slen) > 0) LOOP
                v_addr := SUBSTR(v_add_src, slen, INSTR(SUBSTR(v_add_src, slen),',')-1);
                slen := slen+INSTR(SUBSTR(v_add_src, slen),',');
                 --Dbms_Output.put_line('rcpt ' || v_addr);
                utl_smtp.rcpt(l_mail_conn, v_addr);
                END LOOP;

            END IF; 

            utl_smtp.open_data(l_mail_conn );
            utl_smtp.write_data(l_mail_conn,
             'MIME-VERSION: 1.0' || crlf ||
             'FROM: '   || p_email.sender || crlf ||
             'Subject: '|| p_subject || crlf ||
             'TO: '     || p_email.recipients || crlf || 
             'CONTENT-TYPE: multipart/mixed;' || crlf ||
             ' boundary="---YOURBOUNDARY"' || crlf ||crlf);
   -- Email body
            utl_smtp.write_data(l_mail_conn, '-----YOURBOUNDARY'||crlf);
            utl_smtp.write_data(l_mail_conn, 'Content-Type: text/html' || crlf);
            utl_smtp.write_data(l_mail_conn, 'Content-Transfer-Encoding: 8bit' || crlf || crlf);
            utl_smtp.write_data(l_mail_conn, p_message||crlf);
    -- begin the attachment

            utl_smtp.write_data(l_mail_conn, '-----YOURBOUNDARY'||crlf);
            utl_smtp.write_data(l_mail_conn, 'Content-Type: text/plain;'||crlf); 
            utl_smtp.write_data(l_mail_conn, 'Content-Transfer-Encoding: 8bit' || crlf);
           --utl_smtp.write_data(l_mail_conn, 'Mime-Type: application/zip' || crlf);
            utl_smtp.write_data(l_mail_conn, 'Content-Disposition: attachment;'|| crlf);
            utl_smtp.write_data(l_mail_conn, ' filename="Report||.csv"'||crlf||crlf);
            utl_smtp.write_data(l_mail_conn,v_col);
          FOR rec IN cur_query
           LOOP
              utl_smtp.write_data(l_mail_conn, rec.start_date||CHR(166)||rec.end_date||CHR(166)||rec.NAME||||crlf);
           END LOOP;

        utl_smtp.close_data(l_mail_conn );
        utl_smtp.quit(l_mail_conn);

       EXCEPTION
        WHEN OTHERS THEN 
        DBMS_OUTPUT.PUT_LINE ('ERROR: '|| SQLCODE ||'Err Msg :'||SQLERRM);

     END;
2
  • This may help with how to zip in PL/SQL: community.oracle.com/thread/2192274 Commented Dec 26, 2013 at 21:21
  • Why don't you want to use Java? And can you use utl_compress, or does it need to be a format that doesn't support? (This question might be related, but not quite a duplicate). Commented Dec 26, 2013 at 23:53

1 Answer 1

1

In the message section where you're transferring the .csv file you've got the MIME Content-Type set to "text/plain". When you change the file name to Report.zip you need to change the Content-Type to "application/zip". Hopefully this will fix things up for you.

Share and enjoy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.