2016-03-07 46 views
0

在我的項目中,要求從數據庫(Oracle)發送HTML電子郵件(通知)。我正在使用UTL_SMTP實用程序。但是,通知模板需要存儲在其中一個數據庫列中,每個通知模板都包含HTML文本和變量。來自oracle數據庫的HTML電子郵件

請在下面找到UTL_SMTP代碼:

CREATE OR REPLACE FUNCTION Email_Temp(
p_to   IN NVARCHAR2, 
p_subject  IN NVARCHAR2, 
p_template_id IN Form_Templates.template_id%type) 
RETURN   NVARCHAR2 
AS 
Ret_String  NVARCHAR2(10); 
p_from   NVARCHAR2 (2000) := 'This is fixed(Database server)'; 
p_smtp_host  NVARCHAR2 (2000) := 'Database'; 
p_smtp_port  NUMBER := 25; 
p_html_msg  NVARCHAR2 (4000) := NULL; 
l_mail_conn  UTL_SMTP.connection; 
l_boundary  NVARCHAR2 (50) := '----=*#abc1234321cba#*='; 
BEGIN 

l_mail_conn := UTL_SMTP.open_connection (p_smtp_host, p_smtp_port); 
UTL_SMTP.helo (l_mail_conn, p_smtp_host); 
UTL_SMTP.mail (l_mail_conn, p_from); 
UTL_SMTP.rcpt (l_mail_conn, p_to); 
UTL_SMTP.open_data (l_mail_conn); 
UTL_SMTP.write_data (l_mail_conn, 'From: ' || p_from || UTL_TCP.crlf); 
UTL_SMTP.write_data (l_mail_conn, 'Sent: ' || TO_CHAR (SYSDATE,'FMDay, FMMonthDD, YYYY HH12:MI:SS PM') || UTL_TCP.crlf); 
UTL_SMTP.write_data (l_mail_conn, 'To: ' || p_to || UTL_TCP.crlf); 
UTL_SMTP.write_data (l_mail_conn, 'Subject: ' || p_subject || UTL_TCP.crlf); 
UTL_SMTP.write_data (l_mail_conn, 'Reply-To: ' || p_from || UTL_TCP.crlf); 
UTL_SMTP.write_data (l_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf); 
UTL_SMTP.write_data (l_mail_conn 
        , 'Content-Type: multipart/alternative; boundary="' || l_boundary || '"' || UTL_TCP.crlf 
        || UTL_TCP.crlf 
        ); 

IF p_html_msg IS NOT NULL 
THEN 
select html_string into p_html_msg from Form_Templates where template_id = p_template_id; 
    UTL_SMTP.write_data (l_mail_conn, '--' || l_boundary || UTL_TCP.crlf); 
    UTL_SMTP.write_data (l_mail_conn, 'Content-Type: text/html; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf); 
    UTL_SMTP.write_data (l_mail_conn, p_html_msg); 
    UTL_SMTP.write_data (l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf); 
END IF; 

UTL_SMTP.write_data (l_mail_conn, '--' || l_boundary || '--' || UTL_TCP.crlf); 
UTL_SMTP.close_data (l_mail_conn); 
UTL_SMTP.quit (l_mail_conn); 
Ret_String := 'Sucess'; 
RETURN Ret_String; 
END; 
/

現在,它們將被存儲在數據庫中的字段(固定的文本和變量)每個模板看起來像下面的示例:

<html> 
<body> 
<h4 style="color:blue;">Assignee Notification</h4><br> 
Hello Assignee, 
<br><br> 
You have been assigned a document. 
<br><br> 
<font size="3" color="blue"><b>Project Name:</b></font> variable 
<br> 
<font size="3" color="blue"><b>Document Nickname: </b></font> variable 
<br><br> 
</body> 
</html> 

我需要調用郵件函數的模板,但同時,模板應該包含固定文本和帶有值的變量。

回答

0

這是我寫的代碼,它是做你想做的。這不是最優雅的解決方案,並且在有多個字段需要替換的地方不容易擴展。 它需要一個電子郵件模板表,其中列出了所有模板和一個子語言環境表,其中包含多種語言的樣板文本。這個想法的本質是設置你的模板,以便你需要替換的字符串用'XXXXXXXX'或'YYYYYYYY'等文本字符串表示。

PROCEDURE Send_assignment_mail (to_user_in  IN NUMBER, 
            by_user_in  IN NUMBER := NULL, 
            case_id_in  IN NUMBER, 
            case_type_in IN NUMBER, 
            mail_type_in IN NUMBER := NULL, 
            extra_text_in IN VARCHAR2 := NULL) 
    IS 
     /****************************************************************************** 
     PURPOSE: sends an assignment email with link 
     ******************************************************************************/ 
     v_user_email   VARCHAR2 (200); 
     v_locale_id    NUMBER (10); --the language of the user 
     v_byuser_name   VARCHAR2 (4000); 
     v_subject    VARCHAR2 (200); 
     v_body     VARCHAR2 (4000); 
     v_file_no    VARCHAR2 (40); 
     v_weblink    VARCHAR2 (250); 
     v_mail_type    NUMBER (10); 
     --email type is for assignment emails in email_template table 
     c_email_type CONSTANT NUMBER (1) := 1; 
     c_replace  CONSTANT VARCHAR2 (8) := 'XXXXXXXX'; 
    BEGIN 
     --set a default template if none is provided 
     IF mail_type_in IS NULL 
     THEN 
     v_mail_type := c_email_type; 
     ELSE 
     v_mail_type := mail_type_in; 
     END IF; 

     --get the language the user wishes to receive the email in 
     SELECT cmu.email, cmu.locale_id 
     INTO v_user_email, v_locale_id 
     FROM app_user cmu 
     WHERE cmu.ID = to_user_in; 

     v_byuser_name := Get_by_user_name (by_user_in, v_locale_id); 


     --get more information about the item you are notifying them about 
     v_file_no := GET_FILE_NUM (case_id_in, case_type_in); 

     SELECT emi.subject, 
       REPLACE (emi.text1, c_replace, v_file_no) 
      || UTL_TCP.CRLF 
      || UTL_TCP.CRLF 
      || REPLACE (emi.text2, c_replace, v_byuser_name) 
      || UTL_TCP.CRLF 
      || UTL_TCP.CRLF 
      || v_weblink 
      || UTL_TCP.CRLF 
      || UTL_TCP.CRLF 
      || extra_text_in 
     INTO v_subject, v_body 
     FROM EMAIL_TEMPLATE_LOCALE emi 
     WHERE EMI.PK_ID = v_mail_type AND EMI.LOCALE_ID = v_locale_id; 

     --call your generic mail procedure 
     MAIL_PKG.SEND (g_from, 
           v_user_email, 
           NULL, 
           v_subject, 
           v_body); 
     --log that you sent the mail so if the user denies receiving it 
     --you have something to show 
     LOG_EMAIL (to_user_in, 
       v_mail_type, 
       v_subject, 
       case_id_in); 
    END Send_assignment_mail; 
+0

你的解決方案是沒用的,你沒有提供代碼(或任何其他來源)的包'MAIL_PKG'。 –

+0

@WernfriedDomscheit OP不詢問如何發送電子郵件。他們問如何修改電子郵件中的模板。 – kevinsky

相關問題