Anshu
Anshu

Reputation: 374

Send emails using oracle with sender's name

I have this procedure to send emails. I was wondering is there any way to add senders displaying name in addition to the email address, like in .NET?

I have attached the working code. The problem is is doesn't add the senders name.

   p_smtp_host := '192.xxx.xxx.xxx';
   p_smtp_port := 25;

   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);
   process_recipients(l_mail_conn, p_to);
   process_recipients(l_mail_conn, p_cc);
   process_recipients(l_mail_conn, p_bcc);

   UTL_SMTP.open_data(l_mail_conn);

   UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
   UTL_SMTP.write_data(l_mail_conn, 'To: ' || p_to || UTL_TCP.crlf);

   IF TRIM(p_cc) IS NOT NULL THEN
     UTL_SMTP.write_data(l_mail_conn, 'CC: ' || REPLACE(p_cc, ',', ';') || UTL_TCP.crlf);
   END IF;

   IF TRIM(p_bcc) IS NOT NULL THEN
    UTL_SMTP.write_data(l_mail_conn, 'BCC: ' || REPLACE(p_bcc, ',', ';') || UTL_TCP.crlf);
   END IF;

   UTL_SMTP.write_data(l_mail_conn, 'From: ' || p_from || 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_text_msg IS NOT NULL THEN
    UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
    UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);

     UTL_SMTP.write_data(l_mail_conn, p_text_msg);
     UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
   END IF;

   IF p_html_msg IS NOT NULL THEN
     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);

Upvotes: 0

Views: 1587

Answers (1)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59436

Provide the mail like this

FromName := 'Wernfried Domscheit';
FromMail := '[email protected]';
UTL_SMTP.WRITE_DATA(l_mail_conn, 'From: "'||FromName||'" <'||FromMail||'>'||UTL_TCP.CRLF);

Please note, by default SMTP supports only ASCII characters in From field, in case your name may contain Non-ASCII characters (e.g. 'Peter Müller') you should write it as this:

IF CONVERT(FromName, 'US7ASCII') = FromName THEN
    UTL_SMTP.WRITE_DATA(l_mail_conn, 'From: "'||FromName||'" <'||FromMail||'>'||UTL_TCP.CRLF);
ELSE
    UTL_SMTP.WRITE_DATA(l_mail_conn, 'From: =?UTF-8?B?'|| UTL_ENCODE.TEXT_ENCODE(FromName, NULL, UTL_ENCODE.BASE64) ||'?= <'||FromMail||'>'||UTL_TCP.CRLF);
END IF; 

In order to check whether FromName is plain ASCII I prefer CONVERT(FromName, 'US7ASCII') = FromName rather than REGEXP_LIKE(FromName, '^[ -~]+$'), see Find out if a string contains only ASCII characters

Upvotes: 4

Related Questions