meta data for this page
  •  

ibec_smtp_SendMail

This function sends an email using SMTP protocol.

Syntax

  function ibec_smtp_SendMail(SMTPHost : string; SMTPPort : string; 
                              UserName : string; Password : string; From : string; 
                              To : string; CC : string; BCC : string; Subject : string; 
                              Message : string; AttachedFiles : string;
                              AdditionalHeaders : string; Options : string; 
                              CallbackBlock : string) : variant;

Features:

  • Support of SSL (Secure Sockets Layer) and TLS (Transport Layer Security) has been implemented.
  • TLS is used automatically if the SMTP server allows it, i.e. STARTTLS is presented in response to the EHLO command. If for some reason you don't need TLS while communicating with an SMTP server - specify 'NoTLS' in the list of options.
  • To use SSL you must specify 'SSL' or 'UseSSL' in the list of options. Ensure that the SMTP server requires an SSL connection, otherwise the connection will not be established.
  • There are some changes in the authentication procedure. In previous versions of the ibec_smtp_SendMail function, the AUTH LOGIN method was used by default. Now the preferred method of authentication is determined by the response to the HELO/EHLO command. It is also possible to specify it manually using the AUTHTYPE option. The following methods are supported: NONE, PLAIN, LOGIN, CRAM-MD5, AUTO. AUTO is a default authentication method.
  • ibec_smtp_SendMail returns NULL if the message has been sent successfully. Otherwise it returns a string containing error message(s).

Options (still in work):

AuthType E.g. AuthType=NONE
Priority Highest/High/Normal/Low/Lowest
ibec_smtp_SendMail(...,
                 'Encoding=windows-1251; Confirm; Priority=Highest; AuthType=NONE',
                  cbb);

See also the examples below.

Example 1

execute ibeblock
as
begin
  CRLF = ibec_CRLF();
  cbb = 'execute ibeblock (Vals variant)
        as
        begin
          sPref = '''';
          sEvent = Vals[''EVENT''];
          if ((sEvent = ''COMMAND'') or (sEvent = ''HEADER'')) then
            sPref = ''==> '';
          else if (sEvent = ''RESPONSE'') then
            sPref = ''<== '';
          sMes = sPref + Vals[''TEXT''];
          ibec_Progress(sMes);
          LogFile = ibec_GetGlobalVar(''LogFileH'', null);
          if (LogFile is not NULL) then
            ibec_fs_Writeln(LogFile, sMes);
       end';

  sMessage = 'Just a test' + CRLF +
            'This message was sent by ibec_smtp_SendMail function';

  sAttachments = 'D:\smtpsendmail.ibeblock' + CRLF +
                'D:\script.sql';

  sAddHeaders  = 'IBE-Type: IBEBlock' + CRLF +
                'IBE-Comment: Just a comment';

  LogFile = ibec_fs_OpenFile('D:\smtp.log', __fmCreate);
  try
    if (LogFile is not null) then
      ibec_SetGlobalVar('LogFileH', LogFile);
     ibec_smtp_SendMail('mail.myserver.com',
                      'smtp',
                      'Bill',
                      'windows_must_die!',
                      '"Bill Gates" <Bill@microsoft.com>',
                      'all@world.com',
                      '',
                      '',
                      'Test message from IBEBlock ibec_smtp_SendMail function',
                      :sMessage,
                      :sAttachments,
                      :sAddHeaders,
                      'Encoding=windows-1251; Confirm; Priority=Highest',
                      cbb);
  finally
    ibec_fs_CloseFile(LogFile);
  end;
end

Example 2: Using SSL

   Res = IBEC_SMTP_SENDMAIL('smtp.gmail.com', '465',  -- GMAIL requires SSL connection on this port 
                        'some.user@gmail.com',  'masterkey',
                        'some.user@gmail.com',
                        'another.user@gmail.com',
                        '',
                        '', 
                        'Subject of message',
                        'Text of message',
                        '',
                        '',
                        'encoding="UTF-8"; ContentType=plain; Priority=High; UseSSL', null);  

Example 3: Using TLS

   Res = IBEC_SMTP_SENDMAIL('smtp.gmail.com', '25',  -- GMAIL allows TLS on this port; it will be used automatically 
                        'some.user@gmail.com',  'masterkey',
                        'some.user@gmail.com',
                        'another.user@gmail.com',
                        '',
                        '', 
                        'Subject of message',
                        'Text of message',
                        '',
                        '',
                        'encoding="UTF-8"; ContentType=plain; Priority=High; AuthType=LOGIN', null);