Skip to main content

Hello Everyone,

I'm encountering an issue with Chinese characters when generating an email attachment. The goal is to list the Customers and Suppliers created after a certain date and send the list in CSV format via email. To achieve this, I wrote the following PL/SQL block. However, the Chinese characters in the Customer and Supplier names are appearing as garbled text in the CSV file. What am I doing wrong? Many thanks!

 

 

DECLARE
  last_run_    VARCHAR2(32000) := '&NEW:CF$_ECP_LAST_RUN';
  start_date_  DATE;
  customer_id_ VARCHAR2(32000);
  supplier_id_ VARCHAR2(32000);
  name_        NVARCHAR2(32000);
  filename_    VARCHAR2(32000) := 'new_customer_supplier.csv';
  subject_     VARCHAR2(32000) := 'New Customers and Suppliers';
  email_from_  VARCHAR2(32000) := 'from@something.com';
  email_to_    VARCHAR2(32000) := 'to@something.com';
  email_text_  VARCHAR2(32000);
  attachments_ Command_Sys.attachment_arr;
  attachment_  CLOB;
  exist_       VARCHAR2(32000) := '0';
  
  CURSOR get_cust_ IS
    SELECT 
      customer_id, 
      name
    FROM customer_info
    WHERE creation_date > start_date_
    ORDER BY name;
    
  CURSOR get_supp_ IS
    SELECT 
      supplier_id,
      name
    FROM supplier_info_general
    WHERE creation_date > start_date_
    ORDER BY name;
    
BEGIN
  
  start_date_ := TO_DATE(last_run_, 'YYYY-MM-DD') - 7;
  OPEN get_cust_;
  LOOP
    FETCH get_cust_ INTO customer_id_, name_;
    EXIT WHEN get_cust_%NOTFOUND;
    
    IF (exist_ = '0') THEN
      attachment_ := '"Customer No";"Name";"Address"' || chr(13) || chr(10);  
      exist_ := '1';
    END IF;
    attachment_ := trim(attachment_) || '"' || 
                  trim(customer_id_) || '";"' ||
                  trim(name_)        || '";"' || 
                  chr(13) || chr(10);

  END LOOP;
  CLOSE get_cust_;
  
  OPEN get_supp_;
  LOOP
    FETCH get_supp_ INTO supplier_id_, name_;
    EXIT WHEN get_supp_%NOTFOUND;
    
    IF (exist_ = '1') THEN
      attachment_ := trim(attachment_) || chr(13) || chr(10);  
      attachment_ := trim(attachment_) || '"Supplier No";"Name";"Address"' || chr(13) || chr(10);  
      exist_ := '2';
    END IF;
    IF (exist_ = '0') THEN
      attachment_ := trim(attachment_) || '"Supplier No";"Name";"Address"' || chr(13) || chr(10);  
      exist_ := '2';
    END IF;
    attachment_ := trim(attachment_) || '"' || 
                  trim(supplier_id_) || '";"' ||
                  trim(name_)        || '";"' || 
                  chr(13) || chr(10);

  END LOOP;
  CLOSE get_supp_;
  
  email_text_ := 'Hello,<br/><br/>Here attached you can find the requested excel sheet.<br/><br/>';
  email_text_ := trim(email_text_) || '___________________________________________________________________________________________________';
  email_text_ := trim(email_text_) || '<br/>This is an automatically generated email, please do not reply to this email.<br/>';
  
  Command_Sys.Add_Attachment( attachments_ => attachments_,
                              filename_    => filename_,
                              attachment_  => attachment_);
  
  Command_Sys.Mail( sender_      => 'IFSAPP',
                    from_        => email_from_,
                    to_list_     => email_to_,
                    cc_list_     => NULL,
                    bcc_list_    => NULL,
                    subject_     => subject_,
                    text_        => email_text_,
                    attachments_ => attachments_,
                    mail_sender_ => 'MAIL_SENDER1');

END;

Be the first to reply!

Reply