Blog

Sending Emails with a Siemens 1500 PLC

Sending Emails with a Siemens 1500 PLC

Out of the box, Siemens 1500 PLCs can send emails through a third-party email service provider. This interface supplies a mechanism for alerting relevant personal of faults, warnings, and other status information.

The following guide outlines the steps required for establishing a secure connection to an email server and using the TMAIL_C function block to send messages.

This configuration requires a 1500 PLC with minimum firmware V2.0. If you are using a PLC which does not meet these requirements, you may need an additional communication module to establish a connection to the server. These steps also assume that your PLC is connected to the internet via a router.

The basic tasks are outlined below:

  • Configure email server
  • Obtain encryption certificate
  • Import certificate into TIA Portal
  • Update the PLC hardware configuration
  • Configure the TMAIL_C function block
  • Use the TMAIL_C function block

I used TIA Portal V15.1 for this walkthrough. The procedure may differ slightly for different versions.

Configuring Email Server

For this example, we created a dedicated Gmail account to act as an email server. This server communicates with the PLC via Simple Mail Transfer Protocol (SMTP) and transmits emails to the final recipient. For most providers, the default settings will need to be changed to enable the sending of messages via an external program. If you're using a Gmail account, turn off two-step verification and enable less secure app access in your account settings. Without this access, the PLC connection to the server will be rejected.

configure email server

Obtaining Encryption Certificate

The Transport Layer Security (TLS) protocol is used to encrypt the SMTP communications between the PLC client and the email server to ensure a secure connection. Consequently, the PLC needs the certificate associated with the email service provider to sign and encrypt messages.

In Internet Explorer, the name of the relevant certificate can be obtained by navigating to your service providers webpage and opening the security report. When I created this walkthrough, Gmail used the Google Trusted Services – GlobalSign Root CA-R2 certificate.

plc encryption certificate

After determining the correct name, locate the certificate in your computer's certificate manager and export it as a *.cer file in the DER encoded binary format. In the next step, we will import this certificate into TIA Portal before adding it to the PLC hardware configuration. This allows the PLC to communicate with the email server using the additional security provided by the TLS protocol.

exporting tasks

Importing Certificate into TIA Portal

After configuring the email server and obtaining the correct certification file, the remaining configuration is performed in TIA Portal. First, a project administrator must be specified, providing access to the additional security features of the project. These features allow us to add a third party certificate to the project.

To create an administrator, navigate to the security settings for the project and define the necessary credentials. Note, these credentials will be required to access the project in the future.

project admin

After defining a security administrator, the security settings of the project should include additional security features, including the certificate manager. To import the certificate file, navigate to the certificate authority, and right-click to import the file. After importing, the certificate containing the public key required for the SMTP connection should appear in the manager.

Note the expiration date of the certificate, as it may need updating in the future. Furthermore, ensure that the PLC time is set correctly, as the factory default may fall outside of the certificate’s valid range.

importing certificcate

Updating PLC Hardware Configuration

After importing the certificate into the project, the hardware configuration of the PLC must be updated to establish a connection to the email server. This process involves adding the certificate to the device and configuring the DNS server settings.

First, navigate to the certificate manager in the Protection & Security settings of the device configuration. Enable the use of the global security settings to provide full functionality. Then, add the certificate as a partner device. Note that if the certificate manager settings tab does not appear, then your hardware likely does not support this configuration. In this case, you may need to use an external communication module to use the TMAIL_C function block.

certificate manager

After adding the certificate to the hardware configuration, we need to verify the DNS server settings of the PLC. A Domain Name System (DNS) server is required to associate domain names with the IP addresses needed for routing information between the PLC and the email server.

For this application, the router connecting the PLC to the internet will act as the DNS server. Consequently, the router address must match the DNS server address in the hardware configuration. After verifying the router address in the PLC’s PROFINET interface, navigate to the DNS configuration and add it to the list of DNS server addresses.

If you are unsure the proper address, you can use the ipconfig command on a PC networked to the PLC to determine the default gateway of your local network.

profinet interface ethernet addressesprofinet interface dns config

Configuring the TMAIL_C Function Block

After the hardware configuration for the PLC is complete, the final task involves setting up the TMAIL_C function block to send messages. This block can be found under Communication in the Instruction Window.

tmail_C

The MAIL_ADDR_PARAM input is used to define the connection details. For an S7-1500 CPU, the TMail_V4_SEC system data type should be used to establish a secure connection using IPv4. Note, Siemens provides several additional data types for communicating with a server using different protocols.

The parameters of the TMail_V4_SEC data type need to be defined as follows:

  • InterfaceId: The hardware identifier of the PROFINET interface being used to communicate with the email server. This value is found in the system constants list located in the device configuration.
    local profinet interface
  • ID: The connection identifier. This value should likely be 1.
  • ConnectionType: Defines the connection type. Use 16#20 for IPv4.
  • ActiveEstablished: Defines if connections are actively or passively established. Since the PLC is acting as the SMTP client, it must actively establish a connection to communicate with the server. Therefore, this value must be true.
  • WatchDogTime: The duration before a communication timeout occurs
  • MailServerAddress: The IP address of the configured email server. Pinging the SMTP server from any PC determines this value.
    mail server addresses
  • UserName: The username for your email server account
  • PassWord: The password for your email server account. Note, the PLC instance memory will store this password in plain text.
  • From:
    • LocalPartPlusAt: The local part of your email address plus the "@" symbol. For a Gmail account, this will be your username followed by "@."
    • FullQualifiedDomainName: The domain name of your email service provider. For example, "gmail.com."
  • Remote Port: This is the remote TCP port used for the connection. The default port for SMTP communications is 587. Some legacy systems may use alternative ports, but they are generally less secure and not recommended.
  • ActivateSecureConn: This value should be true to activate the security functionality of the connection. If it is false, a non-secure connection will be established which may be rejected by the server.
  • ExtTLSCapabilities: Allows for extended capabilities of the TLS handshaking protocol. This is not required for this configuration.
  • TLSServerCertRef: Reference to the encryption certificate of the email server. Use the integer ID of the imported certificate.

address parameters

Using the TMAIL_C Function Block

After configuring the address parameters, using the TMAIL_C block is relatively straightforward. A message's recipient, subject, and body are defined using the FBs respective inputs. Optionally, an attachment can be defined, allowing arrays of data to be included as an attachment to the email.

A message request is triggered on the rising edge of the REQ input. The block executes asynchronously over several cycles, during which the BUSY output will remain high. If the email is sent successfully, the DONE output will pulse for a single scan. If it fails, the ERROR output will pulse, and the STATUS will update with the relevant error code.

function block for sending email

There you have it! These steps provide the basic outlining for setting up an SMTP client on a Siemens 1500 PLC. For additional information, see the related Siemens application example in addition to the help files associated with the TMAIL_C connect block.

Feel free to contact DMC with any questions or project inquiries and learn more about our PLC Programming services and our Siemens expertise.

Comments

There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above:

Related Blog Posts