« Close

Datasheets and User Guides

App Notes

Software & Driver

 

7.2 - Sending Email Out of DAQFactory

Sending email out of DAQFactory is quite simple. You have to set a couple system variables and then call Email.Send(). First, however, you need to know your SMTP server's address, username and password. This can usually be pulled out of your normal email program. Before we get into it, though, please note that DAQFactory does not support SSL, so won't work with gmail accounts since they require an SSL connection.

Since most of the email settings are the same for every email, we recommend putting that code in a sequence marked Auto-Start instead of calling it every time. It doesn't really matter, but it makes things more efficient. So:

1) Right click on SEQUENCES: in the Workspace and select Add Sequence. Give it the name StartUp and click OK.

2) In the sequence editor, check the box labeled Auto-Start, then put the following in the script, adjusting for your server settings:


email.strHost = "mail.myISP.com" 
email.strUserName = "[email protected]" 
email.strPassword = "password" 
email.strReplyAddress = "[email protected]" 
email.strAuthenticate = "AuthLogin"

You may need to tweak this last item. Depending on your server, it could be "NoLogin", "AuthLogin" or "LoginPlain". Also, most servers require a ReplyAddress, so make sure and set this one as well.

3) Click Apply, then go to Debug - Run this Sequence from the DAQFactory menu to actually set these settings.

Now that the basics are set, you can create code to actually send the email. Unless you are only going to send a message from one place, you may want to create a little Sequence function you can call to send an email with a given message. For example, if you are sending messages for alarms:

4) Right click on SEQUENCES: in the Workspace and select Add Sequence. Give it the name SendEmail and click OK.

Note we can't use "Email" as a name because it is used by DAQFactory.

5) In the sequence editor, put the following script, adjusting as needed:


function SendEmail(string Message) 
   email.strTo = "[email protected]" 
   email.strCC = "[email protected]" 
   email.strSubject = "ALARM!!!!!" 
   email.strBody = message 
   email.Send()

Now that you have that function you can call it from elsewhere in DAQFactory. For example, lets say we want to send an email whenever our Pressure channel goes above 80 (from our earlier examples):

6) Click on Pressure in the workspace under CHANNELS: (this assumes you still have the Pressure channel from a few chapters ago).

7) Select the Event tab and enter this script:


if ((Pressure[0] > 80) && (Pressure[1] <= 80)) 
   SendEmail("Pressure is greater than 80.  Its going to blow!!!!") 
endif 

8) Click Apply. Now if the pressure goes above 80, an email will be sent.

Now you may be wondering about the if() statement we used. If we had done: "if (Pressure[0] > 80)" instead, then DAQFactory would fire off an email for every reading of pressure that is over 80. Instead, we are only sending an email as the pressure goes over 80. Pressure[0] is the most recent reading, and Pressure[1] is the next most recent reading. && means "AND", so only if the most recent reading is > 80, AND the next most recent reading is <= 80 will an email be generated.

Note: email takes a finite amount of time and is done asynchronously, meaning when you do email.send() to send the email, the email is sent in the background and the function returns immediately. Because of this, you should be careful not to consecutively call email.send() too quickly. Although it depends on your server, at least 5 seconds between sends is recommended.

Attaching files:

DAQFactory email also supports attaching of files. This is great if you want to be able to send the daily logs every night. We talked about how to create daily files in the Logging chapter. To attach the daily log to an email requires us to expand the script we made to change the logging file. Here's the modified script, with the assumption that you've set the To, Subject and Body elsewhere:


logging.mylog.strFileName = "c:\mydata_" + formatdatetime("%y%m%d",systime()) + ".csv"
private string lastdate = formatdatetime("%d",systime()) while(1) if (formatdatetime("%d",systime() != lastdate)) // save the attachment file BEFORE we change the logging set name email.strFile = logging.mylog.strFileName logging.mylog.strFileName = "c:\mydata_" + formatdatetime("%y%m%d",systime()) + ".csv" // give the logging set time to close yesterday's file before we send the email delay(10) email.send() endif delay(1) endwhile