smtpSend (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.50 / 1.00.00


This function will send an email.

The network interface must be connected and the SMTP interface must be configured to use an email server (with smtpSetConfig) before this function is called.

The function creates the email in the outbox and then waits for the mail transfer to complete before returning.

Because of this, the message text is limited to the size of STRING.

 

Note: the outbox can only contain two emails.

 

 

Input:

Receiver : STRING

The email address of the receiver (max. 40 characters).

 

Receiver_cc : STRING

The email address of an optional receiver (max. 40 characters).

 

Subject : STRING

The subject of the email (max. 40 characters).

 

Message : STRING

The text message of the email.

 

Returns: INT

0

- Success.

-1

- General error.

-2

- SMTP interface is not open.

-3

- Invalid parameter(s).

-4

- Outbox is full.

-7

- Network error.

-8

- Email server not found.

-10

- Email transfer error.

-13

- SMTP parameters are not set or are illegal.

-14

- The server certificate failed to validate.

-15

- The secure connection failed.

 

Declaration:

FUNCTION smtpSend : INT;
VAR_INPUT
  Receiver   : STRING;
  Receiver_cc : STRING;
  Subject     : STRING;
  Message     : STRING;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR_INPUT
  alarm : BOOL;
END_VAR;
 
VAR
  fired : BOOL;
END_VAR;
 
PROGRAM example;
VAR
  rc     : INT;
  Subject : STRING;
END_VAR;
 
  gsmPower(power := ON);
  netOpen(iface:=1);
  smtpOpen();
 
  // Wait for network connection (after this, we are connected to the Internet)
  WHILE NOT netConnected(iface:=1) DO
    DebugMsg(message:="Waiting for network connection");
    Sleep(delay:=2500);
  END_WHILE;
 
BEGIN
  // Is there an alarm
  IF alarm AND NOT fired THEN
     Subject := strFormat(format := "Device \4", v4 := boardSerialNumber());
     rc := smtpSend(Receiver := "devices@alarms.com",
                    Receiver_cc := "cc_devices@alarms.com",
                    Subject := Subject,
                    Message := "Alarm is triggered!");
    IF rc <> 0 THEN
        DebugFmt(message := "Error sending mail (errno=\1)", v1 := rc);
    ELSE
        DebugMsg(message := "Mail sent");
    END_IF;
  END_IF;
  fired := alarm;
END;
END_PROGRAM;