smtpAwaitCompletion (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.50 / 1.00.00


This function will block the thread while waiting for an email transfer to complete. An email must be created by smtpNew before this function can wait for completion.

It is possible to start waiting at any point in the lifetime of the email - from its creation to the completion of the email transfer.

 

When the email transfer is completed, and if an error is encountered during the transfer or the email is canceled (with smtpCancel), the email is removed from the outbox and all waiting threads will be resumed.

 

 

Input:

Handle : INT

The handle for the email.

 

Returns: INT

0

- Success.

-1

- General error.

-2

- SMTP interface is not open.

-5

- Invalid mail handle.

-8

- Mail server not found.

-9

- Mail transfer canceled.

-10

- Mail transfer error.

 

Declaration:

FUNCTION smtpAwaitCompletion : INT;
VAR_INPUT
  Handle : INT;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
  send : DINT;
  timer : TON;
  gps   : gpsFix;
END_VAR;
 
FUNCTION gps_log;
VAR
  fd   : FILE;
  str   : STRING;
END_VAR;
  IF fsFileExists(name := "smtptest.txt") THEN
     fd := fsFileOpen(name := "smtptest.txt");
  ELSE
     fd := fsFileCreate(name := "smtptest.txt");
  END_IF;
  IF fsFileStatus(fd := fd) = 0 THEN
     gps();
     str := strFormat(format := "\1,", v1 := gps.mode);
    IF gps.mode = 0 THEN
        str := str + ",,,,,,,";
    ELSE
        str := str + strFormat(format := "\1.\2.\3,", v1 := gps.year, v2 := gps.month, v3 := gps.day);
        str := str + strFormat(format := "\1:\2:\3,", v1 := gps.hour, v2 := gps.minute, v3 := gps.second);
        IF gps.mode = 1 THEN
           str := str + ",,,,,";
        ELSE
           str := str + strFormat(format := "\4,", v4 := gps.latitude);
           str := str + strFormat(format := "\4,", v4 := gps.longitude);
           str := str + strFormat(format := "\4,", v4 := gps.speed);
           str := str + strFormat(format := "\4,", v4 := gps.course);
           str := str + strFormat(format := "\1,", v1 := gps.height);
        END_IF;
    END_IF;
     str := str + strFormat(format := "\1,", v1 := gps.inview);
     str := str + strFormat(format := "\1$n", v1 := gps.used);
 
    fsFileWriteString(fd := fd, str := str);
    fsFileClose(fd := fd);
  END_IF;
END_FUNCTION;
 
PROGRAM example;
VAR
  md   : INT;
  rc   : INT;
END_VAR;
 
  gsmPower(power := ON);
  gpsPower(power := ON);
  netOpen(iface := 1);
  fsMediaOpen(media := 0);
  smtpOpen();
 
  timer.pt := 10000;
  send     := clockNow() + 86400;
  DebugMsg(message := "Application running");
 
BEGIN
  timer(trig := ON);
  IF timer.q THEN
     gps_log();
     timer(trig := OFF);
  END_IF;
 
  IF clockNow() > send THEN
    // Build mail to send
     md := smtpNew(Receiver := "device@tracking.com", Subject := strFormat(format := "\4 trace", v4 := boardSerialNumber()));
    smtpAddText(Handle := md, Message := strFormat(format := "Device: \1$n", v1 := boardType()));
    smtpAddText(Handle := md, Message := strFormat(format := "Fw:   \1.\2$n", v1 := boardVersion() / 100, v2 := boardVersion() MOD 100));
    smtpAddText(Handle := md, Message := strFormat(format := "App:  " + verGetAppName() + "$n"));
    smtpAddText(Handle := md, Message := strFormat(format := "Ver:  \1.\2$n", v1 := verGetAppVersion() / 100, v2 := verGetAppVersion() MOD 100));
    smtpAddAttachment(Handle := md, Filename := "smtptest.txt");
    // Send mail
     rc := smtpSendX(Handle := md);
    IF rc = 0 THEN
        // Wait until mail is sent
        rc := smtpAwaitCompletion(Handle := md);
        IF rc = 0 THEN
          fsFileDelete(name := "smtptest.txt");
          DebugMsg(message := "Mail sent");
           send := clockNow() + 86400;
        ELSE
          DebugFmt(message := "Failed to send mail! (errno=\1)", v1 := rc);
           send := clockNow() + 300;
        END_IF;
    ELSE
        DebugFmt(message := "Failed to send mail! (errno=\1)", v1 := rc);
        send := clockNow() + 300;
    END_IF;
  END_IF;
END;
END_PROGRAM;