Search code examples
gmailoracle12crhel7postfix-mta

Send email from Oracle 12c with postfix on Oracle Linux 7


I have postfix service on my Oracle Linux and they work well done. I use function

create or replace procedure Send_Mail(Msg_To varchar2, Msg_Subject varchar2, Msg_Text varchar2) is
    c        Utl_Smtp.Connection;
    Rc       integer;
    Msg_From varchar2(50) := 'me@me.com'; -- email of my company which hosted on Gmail
    Mailhost varchar2(30) := 'smtp.gmail.com';

begin
    c := Utl_Smtp.Open_Connection(Mailhost, 587);
    Utl_Smtp.Helo(c, Mailhost);
    Utl_Smtp.StartTLS(c);
    Utl_Smtp.Mail(c, Msg_From);
    Utl_Smtp.Rcpt(c, Msg_To);

    Utl_Smtp.Data(c,
                  'From: Oracle Database' || Utl_Tcp.Crlf || 'To: ' || Msg_To || Utl_Tcp.Crlf || 'Subject: ' || Msg_Subject || Utl_Tcp.Crlf ||
                  Msg_Text);
    Utl_Smtp.Quit(c);

exception
    when Utl_Smtp.Invalid_Operation then
        Dbms_Output.Put_Line(' Invalid Operation in Mail attempt 
using UTL_SMTP.');
    when Utl_Smtp.Transient_Error then
        Dbms_Output.Put_Line(' Temporary e-mail issue - try again');
    when Utl_Smtp.Permanent_Error then
        Dbms_Output.Put_Line(' Permanent Error Encountered.');
end;

which I found on http://www.dba-oracle.com/t_utl_smtp_utility.htm

when I run function

BEGIN
send_mail(msg_to      => 'me@me.com',
          msg_subject => 'Test subject',
          msg_text    => 'Test text');
END;

I get a error:

ORA-29279: SMTP permanent error: 530 5.7.0 Must issue a STARTTLS command first. im3sm2477330wjb.13 - gsmtp /* 132/5 selected symbols */

what I did wrong or what I must do?


Solution

  • The error is mentioned in the documentation, which also tells you how to secure the SMTP connection using SSL/TLS. You need to call utl_smtp.starttls().

    ...
    begin
        c := Utl_Smtp.Open_Connection(Mailhost, 587);
        Utl_Smtp.Ehlo(c, Mailhost);
        Utl_Smtp.StartTLS(c);
        Utl_Smtp.Ehlo(c, Mailhost);
        Utl_Smtp.Mail(c, Msg_From);
        Utl_Smtp.Rcpt(c, Msg_To);
        ...