smtpAddAttachment (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.50 / 1.00.00


This function will attach a file to the email. An email must be created by smtpNew before files can be attached.

Once the file is attached, it cannot be removed again, and the only option is to cancel the email (with smtpCancel) and start over again.

 

Note: only one file can be attached to the email.

 

 

Input:

Handle : INT

The handle for the email.

 

Filename : STRING

The name and path of the file to add.

 

 

Returns: INT

0

- Success.

-1

- General error.

-2

- SMTP interface is not open.

-3

- Invalid parameter(s).

-5

- Invalid email handle.

-6

- The email is already sent.

-11

- There is no room for more files in the email.

-12

- File not found.

 

Declaration:

FUNCTION smtpAddAttachment : INT;
VAR_INPUT
  Handle   : INT;
  Filename : 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;