• Asktom:Sending HTML using UTL_SMTP


    Hi Tom
    
    I hope I'm phrasing this correctly...
    
    I'd like to be able to send an HTML formatted email from the database using the UTL_SMTP 
    package.  I don't see any way of setting the MIME type.
    
    Is this beyond the scope of UTL_SMTP?
    
    thanks in advance!
    
    Ken
     

    and we said...

    I asked Tyler Muth (tyler.muth@oracle.com) to answer this one since I knew he already had 
    the code (we use it on our Oracle Calendar software shipped with the email server to send 
    reminders for appointments).  Tyler provided this answer for us:
    
    ...
    
    You are correct in you observation that there is no MIME type parameter in
    UTL_SMTP, but this does not limit the types of email you can send.  It's
    actually very flexible, in that UTL_SMTP allows you to contruct and pass in
    the whole email message.  What this means to you is that constructing an
    HTML message is possible using UTL_SMTP, but your going to have to do some
    work to create it.
    
    The folowing is the basic structure of the message you need to construct:
    
    
    Normal Headers
    Content-Type: multipart/alternative;
    boundary="some_unique_string_not_in_email"
    
    --some_unique_string_not_in_email
    Content-Type: text/plain; charset=us-ascii
    
    Text email message here.
    
    --some_unique_string_not_in_email
    Content-Type: text/html; charset=us-ascii
    
    <html>
        <head>
            <title>some subject</title>
        </head>
        <body>
            Your <b>Html</b> email message here.
        </body>
    </html>
    
    --some_unique_string_not_in_email--
    
    
    Ok, that looks hard, but if you use this procedure I wrote, its really quite easy, it 
    does all of the work for you:
    
    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;
    /
    show errors
  • 相关阅读:
    函数与方法(方法前 +
    1362 : 修补木桶 -- 最长的最短边
    149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
    148. Sort List -- 时间复杂度O(n log n)
    133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
    debug && release
    静态库 && 动态库
    枚举
    获取当前用户所使用的是什么浏览器
    java实现在图片上编辑文本内容
  • 原文地址:https://www.cnblogs.com/tracy/p/2055747.html
Copyright © 2020-2023  润新知