2012-11-29 50 views
0

APEX版本:4.1.1APEX - 電子郵件與.CSV附件

Example: 

http://apex.oracle.com/pls/apex/f?p=27554:53

User name: demo 
PW: demo 

我使用下面的博客(http://spendolini.blogspot.com/2006/04/custom-export-to-csv.html)來創建一個.CSV文件,但怎麼辦我將該.CSV文件添加爲在APEX中創建/生成的電子郵件的附件?我想要有一個頁面進程創建.CSV文件,將該.CSV文件添加到APEX生成的電子郵件,並在APEX頁面上單擊按鈕時發送該電子郵件。

我還使用以下Oracle doc作爲指導。

當點擊「應用更新」按鈕,當前網頁的過程代碼: (當代碼運行時創建一個.csv文件,電子郵件發送,但郵件不包含該.csv文件作爲附件)

declare 
    l_body_html varchar2(4000); 
    l_subj varchar2(200); 
    l_from varchar2(200); 
    l_cc_address varchar2(2000); 
    l_cc varchar2(500); 
    l_id NUMBER; 

begin 

     l_subj := '***TEST - DQ MANUAL EDIT - Daily Field Report - ***TEST'; 

l_body_html := '<html> 
<head> 
<style type="text/css"> 
body{font-family: Calibri, Arial, Helvetica, sans-serif; 
font-size:11pt; 
margin:30px; 
color:#1F497D; 
background-color:#ffffff;} 

</style> 
</head> 
<body>'; 

     l_body_html := l_body_html || '<body><div><p><span style="font-size:12.0pt;font-family:Calibri,sans-serif;color:#1F497D;font-weight:bold;">Attached is the manual edit report for today.</b>'; 

    l_body_html := l_body_html; 

    l_body_html := l_body_html; 

    l_body_html := l_body_html || '<p>' || ' will contact you in the near future to prioritize the request and discuss an estimated completion date. Please note that many requests are received daily and it can take several days to couple of weeks on some requests. If this request is of a high critical nature, please feel free to contact directly or reply to this email.</p>'; 

l_body_html := l_body_html || '</div></body></html>'; 


begin 
-- Set the MIME type 
owa_util.mime_header('application/octet', FALSE); 
-- Set the name of the file 
htp.p('Content-Disposition: attachment; filename="Manual_Edit_Selected_Records.csv"'); 
-- Close the HTTP Header 
owa_util.http_header_close; 
-- Loop through all rows in EMP 
htp.prn('Data Fields,Grouping,Channel,Month,Year,Edits' || chr(13)); 
for x in (select 
dq_attribute, 
dq_grouping, 
case 
when DQ_CHANNEL = 'Retail' then 'Retail/CD' 
when DQ_CHANNEL = 'Consumer Direct' then 'Retail/CD' 
else DQ_CHANNEL 
end as dq_channel, 
to_char(to_date(INSERT_DATE,'yyyy-mm'),'Month') as Month, 
to_char(to_date(INSERT_DATE,'yyyy-mm'),'YYYY') as Year, 
case when count(reason_for_change)=0 then 'N/A' else to_char(count(reason_for_change)) end as Edits 
from DQ_MANUAL_EDIT 
where 
decode(NVL(:P50_DQ_ANALYST, 'ALL'), 'ALL','ALL',DQ_ANALYST) = NVL(:P50_DQ_ANALYST, 'ALL') 
and decode(NVL(:P50_DQ_FIELD, 'ALL'), 'ALL','ALL',DQ_ATTRIBUTE) = NVL(:P50_DQ_FIELD, 'ALL') 
and decode(NVL(:P50_DQ_CHANNEL, 'ALL'), 'ALL','ALL',DQ_CHANNEL) = NVL(:P50_DQ_CHANNEL, 'ALL') 
and decode(NVL(:P50_DQ_GROUPING, 'ALL'), 'ALL','ALL',DQ_GROUPING) = NVL(:P50_DQ_GROUPING, 'ALL') 
and decode(NVL(:P50_DQ_IMS, 'ALL'), 'ALL','ALL',DQ_IMS_NUM) = NVL(:P50_DQ_IMS, 'ALL') 
and NVL(to_char(to_date(:P50_EDIT_DATE_FROM,'MM/DD/YYYY'),'MM/DD/YYYY'), to_date(sysdate,'MM/DD/YYYY')) <= trunc(INSERT_DATE) 
and NVL(to_char(to_date(:P50_EDIT_DATE_TO, 'MM/DD/YYYY'),'MM/DD/YYYY'), to_date(sysdate-28,'MM/DD/YYYY')) >= trunc(INSERT_DATE) 
Group by 
dq_attribute, 
INSERT_DATE, 
dq_grouping, 
dq_channel 
order by to_char(to_date(INSERT_DATE,'yyyy-mm'),'YYYY') desc, 
to_char(to_date(INSERT_DATE,'yyyy-mm'),'mm'), dq_attribute asc 
) 
loop 
-- Print out a portion of a row, 
-- separated by commas and ended by a CR 
htp.prn(
'"' ||x.dq_attribute||'","'|| 
x.dq_grouping ||'","'|| 
x.dq_channel ||'","'|| 
x.Month ||'","'|| 
x.Year ||'","'|| 
x.Edits || '"' || chr(13)); 
end loop; 
-- Send an error code so that the 
-- rest of the HTML does not render 
htmldb_application.g_unrecoverable_error := true; 
end; 



    l_id := APEX_MAIL.SEND(
    --P_TO  => :P53_EMAIL_TO, 
    P_TO  => '[email protected]', 
    P_FROM  => '[email protected]', 
    P_BODY  => l_body_html, 
    P_BODY_HTML => l_body_html, 
    P_SUBJ  => l_subj); 

     FOR c1 IN (SELECT filename, blob_content, mime_type 
     FROM APEX_APPLICATION_FILES 
     WHERE ID IN (123,456)) LOOP 

     APEX_MAIL.ADD_ATTACHMENT(
      p_mail_id => l_id, 
      p_attachment => c1.blob_content, 
      p_filename => c1.filename, 
      p_mime_type => c1.mime_type); 
     END LOOP; 
    COMMIT; 


    wwv_flow_mail.push_queue(
    P_SMTP_HOSTNAME => 'mailhost.company.net', 
    P_SMTP_PORTNO => '31' 
    ); 
end; 
+0

也許我今天只是很密集,但我不確定這裏的問題是什麼。你已經發布了一些看起來至少含糊不清的代碼。運行代碼時是否出現錯誤?如果是這樣,有什麼錯誤?電子郵件有問題嗎?如果是這樣,問題是什麼? –

+0

@Justin Cave我的問題是:如何在一個頁面進程中創建.CSV文件並將其作爲電子郵件的附件?上面的代碼基本上作爲兩個獨立的實體工作:創建.CSV文件併發送郵件(不帶附件)。我想把它們合併成一個過程。 –

回答

0

找到有關此主題的Oracle論壇了有益的探討,這將有助於回答你的問題:https://forums.oracle.com/forums/thread.jspa?threadID=989396

的討論是一個電子郵件附件是不是從電子郵件本身,單獨的物理文件,該文件當你第一次創建一個物理文件然後草擬一個em時,可能是你的問題所有發送它。當我們使用網絡郵件客戶端或電子郵件軟件應用程序發送電子郵件時,它不會以我們看到的方式工作。請參閱Bill Verreyne對utl_mail包的工作原理的說明,該包說明如何通過修改電子郵件的MIME-BODY來添加附件。

簡而言之,這個概念只是把你的數據在電子郵件本身的身體,但確定是應該被視爲「附件」通過適當的MIME頭哪一部分。 UTL_MAIL看起來是最簡單的方法來管理這個,雖然你可能會手工製作一些簡單的標題,有一定的耐心和反覆試驗,或者用APEX_MAIL包進行進一步的實驗。

我複製了最重要的部分,這是一個例子電子郵件的身體是什麼樣子與「附件」 ......注意,在電子郵件中沒有物理文件在所有。它從頭到尾讀取一條電子郵件消息:

From: [email protected] 
To: [email protected] 
Subject: =?ISO-8859-1?Q?Sample=20Mail=20from=20UTL=5FMAIL?= 
X-Priority: 3 
Content-Type: multipart/mixed; boundary="------------4D8C24=_23F7E4A13B2357B3" 
Return-Path: [email protected] 
Date: 19 Nov 2009 07:45:15 +0200 
Mime-Version: 1.0 

This is a multi-part message in MIME format. 
--------------4D8C24=_23F7E4A13B2357B3 
Content-Type: text/plain 
Content-Transfer-Encoding: 7bit 

This is the e-mail message part that the mail reader will display. 


--------------4D8C24=_23F7E4A13B2357B3 
Content-Type: text/csv; name="sample.csv" 
Content-Transfer-Encoding: quoted-printable 
Content-Disposition: inline; filename="sample.csv" 

col1,col2,col3 
col1,col2,col3 
col1,col2,col3 
--------------4D8C24=_23F7E4A13B2357B3--