smtpSendX (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Firmware version:

1.50 / 1.00.00


This function will start sending the email. An email must be created by smtpNew before it can be sent.

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 will return after starting to send the email while the transfer will continue asynchronously.

 

Once the transfer is started, use smtpStatus to get transfer progress and smtpAwaitCompletion to wait for the transfer to complete.

 

When the asynchronous transfer is completed, or if an error is encountered, 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.

-6

- The mail is already sent.

-7

- Network error.

-13

- SMTP parameters are not set or are illegal.

 

Declaration:

FUNCTION smtpSendX : 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;