问题描述
嗨,团队,
我刚刚读了 “使用UTL_SMTP发送HTML” 的帖子。我是pl/sql的初学者。我需要您的帮助,如何通过以下代码运行 “从v $ session中选择 *”,然后以HTML格式将输出作为邮件发送。
我刚刚读了 “使用UTL_SMTP发送HTML” 的帖子。我是pl/sql的初学者。我需要您的帮助,如何通过以下代码运行 “从v $ session中选择 *”,然后以HTML格式将输出作为邮件发送。
create or replace procedure html_email( p_to in varchar2, p_from in varchar2, p_subject in varchar2, p_text in varchar2 default null, p_html in varchar2 default null, p_smtp_hostname in varchar2, p_smtp_portnum in varchar2) is l_boundary varchar2(255) default 'a1b2c3d4e3f2g1'; l_connection utl_smtp.connection; l_body_html clob := empty_clob; --This LOB will be the email message l_offset number; l_ammount number; l_temp varchar2(32767) default null; begin l_connection := utl_smtp.open_connection( p_smtp_hostname, p_smtp_portnum ); utl_smtp.helo( l_connection, p_smtp_hostname ); utl_smtp.mail( l_connection, p_from ); utl_smtp.rcpt( l_connection, p_to ); l_temp := l_temp || 'MIME-Version: 1.0' || chr(13) || chr(10); l_temp := l_temp || 'To: ' || p_to || chr(13) || chr(10); l_temp := l_temp || 'From: ' || p_from || chr(13) || chr(10); l_temp := l_temp || 'Subject: ' || p_subject || chr(13) || chr(10); l_temp := l_temp || 'Reply-To: ' || p_from || chr(13) || chr(10); l_temp := l_temp || 'Content-Type: multipart/alternative; boundary=' || chr(34) || l_boundary || chr(34) || chr(13) || chr(10); ---------------------------------------------------- -- Write the headers dbms_lob.createtemporary( l_body_html, false, 10 ); dbms_lob.write(l_body_html,length(l_temp),1,l_temp); ---------------------------------------------------- -- Write the text boundary l_offset := dbms_lob.getlength(l_body_html) + 1; l_temp := '--' || l_boundary || chr(13)||chr(10); l_temp := l_temp || 'content-type: text/plain; charset=us-ascii' || chr(13) || chr(10) || chr(13) || chr(10); dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); ---------------------------------------------------- -- Write the plain text portion of the email l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(p_text),l_offset,p_text); ---------------------------------------------------- -- Write the HTML boundary l_temp := chr(13)||chr(10)||chr(13)||chr(10)||'--' || l_boundary || chr(13) || chr(10); l_temp := l_temp || 'content-type: text/html;' || chr(13) || chr(10) || chr(13) || chr(10); l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); ---------------------------------------------------- -- Write the HTML portion of the message l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(p_html),l_offset,p_html); ---------------------------------------------------- -- Write the final html boundary l_temp := chr(13) || chr(10) || '--' || l_boundary || '--' || chr(13); l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); ---------------------------------------------------- -- Send the email in 1900 byte chunks to UTL_SMTP l_offset := 1; l_ammount := 1900; utl_smtp.open_data(l_connection); while l_offset < dbms_lob.getlength(l_body_html) loop utl_smtp.write_data(l_connection, dbms_lob.substr(l_body_html,l_ammount,l_offset)); l_offset := l_offset + l_ammount ; l_ammount := least(1900,dbms_lob.getlength(l_body_html) - l_ammount); end loop; utl_smtp.close_data(l_connection); utl_smtp.quit( l_connection ); dbms_lob.freetemporary(l_body_html); end;复制
专家解答
如果例程当前写出html部分,则可以将其替换为对v $ session的查询,然后构造html以从该数据输出。
例如
';
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);
----------------------------------------------------
-- Write the final html boundary
l_temp := chr(13) || chr(10) || '--' || l_boundary || '--' || chr(13);
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);
----------------------------------------------------
-- Send the email in 1900 byte chunks to UTL_SMTP
l_offset := 1;
l_ammount := 1900;
utl_smtp.open_data(l_connection);
while l_offset < dbms_lob.getlength(l_body_html) loop
utl_smtp.write_data(l_connection,
dbms_lob.substr(l_body_html,l_ammount,l_offset));
l_offset := l_offset + l_ammount ;
l_ammount := least(1900,dbms_lob.getlength(l_body_html) - l_ammount);
end loop;
utl_smtp.close_data(l_connection);
utl_smtp.quit( l_connection );
dbms_lob.freetemporary(l_body_html);
end;
例如
create or replace procedure html_email( p_to in varchar2, p_from in varchar2, p_subject in varchar2, p_text in varchar2 default null, p_html in varchar2 default null, p_smtp_hostname in varchar2, p_smtp_portnum in varchar2) is l_boundary varchar2(255) default 'a1b2c3d4e3f2g1'; l_connection utl_smtp.connection; l_body_html clob := empty_clob; --This LOB will be the email message l_offset number; l_ammount number; l_temp varchar2(32767) default null; begin l_connection := utl_smtp.open_connection( p_smtp_hostname, p_smtp_portnum ); utl_smtp.helo( l_connection, p_smtp_hostname ); utl_smtp.mail( l_connection, p_from ); utl_smtp.rcpt( l_connection, p_to ); l_temp := l_temp || 'MIME-Version: 1.0' || chr(13) || chr(10); l_temp := l_temp || 'To: ' || p_to || chr(13) || chr(10); l_temp := l_temp || 'From: ' || p_from || chr(13) || chr(10); l_temp := l_temp || 'Subject: ' || p_subject || chr(13) || chr(10); l_temp := l_temp || 'Reply-To: ' || p_from || chr(13) || chr(10); l_temp := l_temp || 'Content-Type: multipart/alternative; boundary=' || chr(34) || l_boundary || chr(34) || chr(13) || chr(10); ---------------------------------------------------- -- Write the headers dbms_lob.createtemporary( l_body_html, false, 10 ); dbms_lob.write(l_body_html,length(l_temp),1,l_temp); ---------------------------------------------------- -- Write the text boundary l_offset := dbms_lob.getlength(l_body_html) + 1; l_temp := '--' || l_boundary || chr(13)||chr(10); l_temp := l_temp || 'content-type: text/plain; charset=us-ascii' || chr(13) || chr(10) || chr(13) || chr(10); dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); ---------------------------------------------------- -- Write the plain text portion of the email l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(p_text),l_offset,p_text); ---------------------------------------------------- -- Write the HTML boundary l_temp := chr(13)||chr(10)||chr(13)||chr(10)||'--' || l_boundary || chr(13) || chr(10); l_temp := l_temp || 'content-type: text/html;' || chr(13) || chr(10) || chr(13) || chr(10); l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); -- -- write out v$session as html table -- l_temp := ''; l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); for i in ( select sid, serial#, username from v$session ) loop l_temp := ''; l_offset := dbms_lob.getlength(l_body_html) + 1; dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp); end loop; l_temp := '
'||i.sid||' | '||i.serial#||' | '||i.username||' |
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Oracle RAC 一键安装翻车?手把手教你如何排错!
Lucifer三思而后行
598次阅读
2025-04-15 17:24:06
【纯干货】Oracle 19C RU 19.27 发布,如何快速升级和安装?
Lucifer三思而后行
577次阅读
2025-04-18 14:18:38
XTTS跨版本迁移升级方案(11g to 19c RAC for Linux)
zwtian
492次阅读
2025-04-08 09:12:48
Oracle数据库一键巡检并生成HTML结果,免费脚本速来下载!
陈举超
475次阅读
2025-04-20 10:07:02
【ORACLE】记录一些ORACLE的merge into语句的BUG
DarkAthena
459次阅读
2025-04-22 00:20:37
Oracle 19c RAC更换IP实战,运维必看!
szrsu
437次阅读
2025-04-08 23:57:08
【ORACLE】你以为的真的是你以为的么?--ORA-38104: Columns referenced in the ON Clause cannot be updated
DarkAthena
435次阅读
2025-04-22 00:13:51
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
421次阅读
2025-04-17 17:02:24
火焰图--分析复杂SQL执行计划的利器
听见风的声音
368次阅读
2025-04-17 09:30:30
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
358次阅读
2025-04-15 14:48:05