Please enable JavaScript to view this site.

DAQFactory User's Guide

Navigation: 12 Networking / Connectivity

12.3 Email - SMTP outgoing

Scroll Prev Top Next More

With DAQFactory you can compose and send emails automatically.  Use this to email a daily update, or to email when an alarm occurs.   Email is implemented using an internal object.  That object has a number of member variables and a single member function.  To send an email, you should instantiate the CEmail class, set the variables, then call send().  For example:

 

private semail = new(CEMail)

semail.strhost = "smtp.gmail.com"

semail.port = 587

semail.strUserName = "sample@gmail.com"

semail.strPassword = "xxxx"

semail.strAuthenticate = "Auto"

semail.strBody = "this is a test"

semail.strReplyAddress = "sample@gmail.com"

semail.strReplyName = "sample@gmail.com"

semail.strSubject = "test subject"

semail.strTo = "myFriend@gmail.com"

semail.strConnectionType = "STARTTLS"

semail.strSSLProtocol = "TLSv1_2"

semail.Send()

You can keep the object around (say in a global variable), change any of the parameters and call Send() again to send another email.  If you want to send emails from multiple sequences you should instead instantiate separate objects for each sequence (i.e. the private semail = new(CEmail) line) so they do not conflict with each other.

Note that for backwards compatability, there is a pre-instantiated object called Email with the exact same variables and function.  When sending emails using this object, the send() function is asynchronous and will return immediately which makes it difficult to do error handling.  For this reason and the fact the internal object can only work on one email at a time, we recommend migrating to the CEmail object.  

strHost: the name of your smtp host.  For example: semail.strHost = "smtp.gmail.com"

strUserName: your smtp username

strPassword: your smtp password (optional)

strEncoding: the encoding of your message.  Default is "iso-8859-1"

strTo: the email address you’d like to send the message to

strCC: the CC for the email

strSubject: the subject of your email

strFile: the full path to a file you would like to attach to the email

strReplyAddress: the email that shows up on as the reply.  This is required.

strReplyName: the name that shows up as the reply.  Note that many servers require this as well, even if its just the same as the Reply Address.

strBCC: the BCC for the email

strBody: the body text of the email

AutoDial: if you are on a dialup connection, set this to 1 to have DAQFactory dial your ISP when the email is sent.  Defaults to 0.

Mime: set to 1 for Mime type attachment.  Defaults to 0.

HTML: set to 1 for an HTML body.  Defaults to 0.

Port: set to the port of your SMTP server.  Defaults to 25.  For SSL it is usually 465 or 587.  For gmail, we used STARTTLS on 587.

strAuthenticate: can be one of three strings to determine the authentication method required for your SMTP server: "NoLogin", "AuthLogin", "LoginPlain".  If you do not require a password, use the first (the default).  Usually if you do require a password you will use AuthLogin, but if that does not work you can try LoginPlain or Auto.  For gmail we use "Auto"

strConnectionType: can be one of four strings to determine whether SSL is used and the type of SSL: "PlainText", "SSL_TLS", "STARTTLS", "AutoUpgradeToSTARTTLS".  If you do not require SSL, use PlainText (the default).  If you do, which one to select will depend on the server.  For gmail, we use "STARTTLS".

strSSLProtocol: can be one of five strings to determine which SSL protocol to use: "SSLv2orv3", "TLSv1", "TLSv1_1", "TLSv1_2", "DTLSv1".  This setting only applies if the connection type is not PlainText.  Typically you will use SSLv2orv3 for normal SSL, and TLSv1_2 for TLS.  For gmail, we use "TLSv1_2".

Send(): Once you have set the desired variables, call the Send() function to actually send the email.  You can send multiple emails by simply changing the desired variables and calling send again, or instantiate a new CEmail object.  For example if you wanted to send yourself an email with some values every 12 hours, you could set all the initial variables once and then set the body only and call send every 12 hours.  This works great under a single sequence.  If you are sending emails from multiple sequences, you should definitely instantiate a new CEmail object for each sequence.

Note: Send() is now a synchronous call, meaning it will block and not return until the email is sent or there is an error.  If there is an error, it will be thrown.  You can catch the error using try/catch blocks (see section 5.19).  If you don't catch the error, the sequence will stop, and the error displayed in the command/alert window.