Receive Email


Recommended Posts

hi guru

 

I send mail out of daqfactory, but how do i receive email to switch a relay on or off?

been through the help but hit a blank. I got the first part of the sequence going, without errors, then i lose it.

could you post a little example please, so i can see.

 

thanks

mike

Link to comment
Share on other sites

Here's a routine I wrote many years ago to allow me to manage a task list through email.  I'd email a special email with commands to add/ remove tasks, and DAQFactory would list all my tasks, as well as email me on my regular email a daily summary.  Here's just the retrieval part.  Hopefully it helps.  It was written a while ago, so might not take advantage of some of the newer features, but the pop3 parts haven't changed since then.  Note that DAQFactory does not support SSL, so you can't use server's like gmail that require SSL.

 



try
   pop3.strServer = "mail.myserver.com"
   pop3.strUserName = "s@myserver.com"
   pop3.strPassword = "task"
   private count
   private size
   private string command
   private string task
   private string sdate
   private date
   count = pop3.Connect()
//   ? "found "+doubletostr(count)+ " messages..."
   while (count > 0)
      count--
      size = pop3.RetrieveMessageSize(count)
      if (size < 100000)
         pop3.RetrieveMessage(count)
         task = rtrim(pop3.GetBody())
         command = left(pop3.GetSubject(),3)
         command = makelower(command)
         sdate = mid(pop3.GetSubject(),4,100)
         sdate = makelower(sdate)
         switch
            case (command == "add")
               switch
                  case (left(sdate,1) == "d")
                     date = systime() + 86400 * strtodouble(mid(sdate,1,5))
                  case (left(sdate,1) == "w")
                     date = systime() + 86400 * 7 * strtodouble(mid(sdate,1,5))
                  default
                     if (getlength(sdate) < 8)
                        date = systime()
                     else
                        date = evaluate(mid(sdate,6,2)+"y"+left(sdate,2)+"m"+mid(sdate,3,2)+"d")
                     endif
               endcase
               task.time[0] = date
               private.c = 0
               while(c < 1000) // safety at 1000
                  command = parse(task,c,chr(10))
                  if (command == "")
                     break
                  endif
                  c++
                  command.time[0] = task.time[0]
                  tasklist.addvalue(command)
               endwhile
            case (command == "del")
               deletetask(task)
            case (command == "")
               task.time[0] = systime()
               tasklist.addvalue(task)               
            default
               if (find(pop3.GetSubject(),"to do list",0) >= 0)
                  task=ltrim(pop3.GetBody())
                  private c
                  c = 0
                  while(c < 1000) // safety at 1000
                     command = parse(task,c,chr(10))
                     if (command == "")
                        break
                     endif
                     if (left(command,1) == "d")
                        private.loc = find(command,"~",0)
                        if (loc >= 0)
                           command = mid(command,loc+1,2000)
                           deletetask(command)
                        endif
                     endif
                     c++
                  endwhile
               endif  
         endcase
      endif
      pop3.DeleteMessage(count)
   endwhile
   pop3.Disconnect()
   sorttasklist()
   beginexport(tasklistset)
catch()
   ? strLastError
endcatch  
         
         
   

Link to comment
Share on other sites

Well, you need quite a bit of that code.

try
   pop3.strServer = "mail.myserver.com"
   pop3.strUserName = "s@myserver.com"
   pop3.strPassword = "task"
   private count
   private size
   private string task
   count = pop3.Connect()
   while (count > 0)
      count--
      size = pop3.RetrieveMessageSize(count)
      if (size < 100000)
         pop3.RetrieveMessage(count)
         task = rtrim(pop3.GetBody())
      endif
      pop3.DeleteMessage(count)
   endwhile
   pop3.Disconnect()
catch()
   ? strLastError
endcatch  

That's it trimmed down.  After the line that reads "task = pop3....." you'll want to parse task, which is the body of the incoming email, for what you want to do.  For example:

 

if (task == "Set relay to 1")

   m2force = 1

endif

 

But I wouldn't do something as simple as that, otherwise anyone could send an email and possibly trigger the output.  Remember that emails are sent plain text and are readable by anyone that is looking.  Consider using a rotating key or similar method to ensure that someone can't read a message sent, and then send the exact same message to the system to send the same control command.

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.