smtpNew (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.50 / 1.00.00


This function will create a new email.

After the email is created, use smtpAddText to add text to the message and smtpAddAttachment to attach files.

An email created by smtpNew can only be sent with the smtpSendX function.

 

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).

 

Returns: INT

Handle to the new email.

-1

- General error.

-2

- SMTP interface is not open.

-3

- Invalid parameter(s).

-4

- Outbox is full.

 

Declaration:

FUNCTION smtpNew : INT;
VAR_INPUT
  Receiver   : STRING;
  Receiver_cc : STRING;
  Subject     : STRING;
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;