You are on page 1of 32

Application Description 12/13

Sending of Emails to SMTP


Server with a S7-CPU
S7-400, S7-300, WinAC, STEP 7 V12

http://support.automation.siemens.com/WW/view/en/46817803
Warranty and Liability

Warranty and Liability

Note The Application Examples are not binding and do not claim to be complete
regarding the circuits shown, equipping and any eventuality. The application
examples do not represent customer-specific solutions. You are responsible for
ensuring that the described products are used correctly. These Application
Examples do not relieve you of your responsibility to use safe practices in
application, installation, operation and maintenance. When using these
application examples, you recognize that we cannot be made liable for any
damage/claims beyond the liability clause described. We reserve the right to
make changes to these Application Examples at any time and without prior
notice. If there are any deviations between the recommendations provided in this
application example and other Siemens publications e.g. catalogs the
contents of the other documents have priority.

We do not accept any liability for the information contained in this document.
Any claims against us based on whatever legal reason resulting from the use of
the examples, information, programs, engineering and performance data etc.,
described in this application example will be excluded. Such an exclusion will not
apply in the case of mandatory liability, e.g. under the German Product Liability Act
(Produkthaftungsgesetz), in case of intent, gross negligence, or injury of life, body
Siemens AG 2013 All rights reserved

or health, guarantee for the quality of a product, fraudulent concealment of a


deficiency or breach of a condition which goes to the root of the contract
(wesentliche Vertragspflichten). The damages for a breach of a substantial
contractual obligation are, however, limited to the foreseeable damage, typical for
the type of contract, except in the event of intent or gross negligence or injury to
life, body or health. The above provisions do not imply a change of the burden of
proof to your detriment.
Any form of duplication or distribution of these application examples or excerpts
hereof is prohibited without the expressed consent of Siemens Industry Sector.
Copyright

Caution
The functions and solutions described in this entry are mainly limited to the
realization of the automation task. Please furthermore take into account that
corresponding protective measures have to be taken in the context of industrial
security when connecting your equipment to other parts of the plant, the enterprise
network or the Internet. Further information can be found under the Entry ID
50203404.
http://support.automation.siemens.com/WW/view/en/50203404

Sending Emails with S7-CPU


Entry ID: 46817803, V1.0, 12/13 2
Table of Contents

Table of Contents
Warranty and Liability ................................................................................................. 2
1 Automation Task ................................................................................................ 4
2 Automation Solution ......................................................................................... 5
2.1 Solution overview ................................................................................. 5
2.2 Description of the core functionality ..................................................... 6
2.3 Hardware and software components used........................................... 7
2.4 Alternative ............................................................................................ 8
3 Functional Mechanisms of this Application ................................................... 9
3.1 Program overview ................................................................................ 9
3.2 Functionality of FB PN_MAIL ............................................................. 10
3.2.1 Program details on block FB PN_MAIL .............................................. 10
3.2.2 Call interface of the PN_MAIL (FB490) .............................................. 11
3.2.3 Initialization/reset of FB PN_MAIL ..................................................... 15
3.2.4 Email transmission ............................................................................. 16
3.2.5 FB PN_MAIL (FB490): STATUS and SFC_STATUS.................... 16
4 Installation and Commissioning .................................................................... 20
Siemens AG 2013 All rights reserved

4.1 Hardware setup CPU 315-2 PN/DP ................................................... 20


4.2 Software installation ........................................................................... 21
4.3 Establishing an Internet connection to the SMTP server ................... 22
5 Operating the Application ............................................................................... 23
5.1 Sending an email with the aid of the SMTP block FB PN_MAIL ....... 23
5.2 Using data blocks and watch table ..................................................... 24
Copyright

6 Appendix .......................................................................................................... 27
6.1 SMTP commands: Minimum command set ....................................... 27
6.2 SMTP connection process between client and server ....................... 27
6.3 Status and error messages of an SMTP server ................................. 29
7 Literature .......................................................................................................... 31
7.1 Bibliography........................................................................................ 31
7.2 Internet link specifications .................................................................. 31
7.2.1 Relevant RFCs ................................................................................... 32
8 History............................................................................................................... 32

Sending Emails with S7-CPU


Entry ID: 46817803, V1.0, 12/13 3
1 Automation Task

1 Automation Task
Introduction
The growing degree of networking in the automation industry also increases the
demand for the exchange of messages beyond the limits of local area networks
(LANs). Already existing infrastructure, such as, for example, existing server
constellations are to be used for the exchange.
One possibility for such communications is the use of email services via the
Intranet/Internet. This makes it possible to use already existing IT infrastructures in
order to transmit messages from the production level directly with standard IT
services.

Overview of the automation task


The figure below provides an overview of the automation task.
Figure 1-1
Siemens AG 2013 All rights reserved
Copyright

In an automation or process system, the sending of an email is to be triggered


depending on any operational event.
Within the scope of the configuration, any recipients can be configured. The content
of the email is to be freely configurable and is to include, for example, a plain-text
message of the event that has occurred or even specific operating variables.
Increased operational safety supports different mail server authentication methods.
This prevents the communication from being influenced by unauthorized third
parties.
A respective block for the S7-1200 and S7-1500 CPUs already exists. This
application provides these functions for S7-300 CPUs, S7-400 CPUs and
WinAC RTX.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 4
2 Automation Solution

2 Automation Solution
2.1 Solution overview
Schematic layout
The following figure gives a schematic overview of the most important components
of the solution:
Figure 2-1

Intranet/
Internet

FB
Terminal device
PN_Mail
(PC, smartphone, )
Ethernet
Siemens AG 2013 All rights reserved

SMTP Server

FB
PN_Mail

S7-300/S7-400/WinAC
Copyright

The core of the automation solution is the FB PN_MAIL. It controls the sending of
emails via the SMTP server via the blocks of the open communication (OUC
blocks: TCON, TSEND, TRCV, TDISCON)
While the open communication blocks control the basic TCP/IP services,
FB PN_MAIL operates the higher-level email transmission services and
communicates with the mail server. The Internet standard SMTP (Simple Mail
Transfer Protocol) is used as a protocol.
Once the mail server has received the message, the email is considered sent by
the PLC. Further sending of the email via Intranet/Internet cannot be influenced by
the PLC.

Delimitation
This application does not include a description of TCP/IP communication basics or
the use of Simple Mail Transfer Protocols. Basic knowledge of these fields is
assumed or can be found in the documents listed in the appendix.
The application describes only the use and properties of the used communication
blocks. It is assumed that all other components are available, in particular a
correctly configured SMTP server. The configuration of these components is not
part of this application.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 5
2 Automation Solution

Note The open communication blocks TCON, TSEND, TRCV and TDISCON are used
for the TCP/IP communication. The configuration data for these blocks is defined
in the FB PN_MAIL block in the structure TCON_PAR.

2.2 Description of the core functionality


FB PN_MAIL is an asynchronous communication block. It allows to send an email
from an S7 CPU that supports open communication to any SMTP server to which
the PLC is connected via Industrial Ethernet (not necessarily via PROFINET) at
any time. This means that the PLC can be programmed so that the sending of the
email is always triggered as a response to any operational event (messages,
values of operating variables, expiry of deadlines, ...).
Among other things, the configuration enables the user to set the following
parameters:
IP address of the SMTP server
User name and password for authentication at the SMTP server, if required
Recipients address and, where necessary, CC recipient address
Siemens AG 2013 All rights reserved

Subject line
Priority
ASCII plain text and where necessary an email attachment

FB PN_MAIL is not intended for receiving messages (e.g. POP).

Advantages of this solution


Copyright

The solution presented here offers you the following advantages:


SMTP is a powerful, established, flexible method.
An Ethernet structure is available in nearly every location, i.e. there are hardly
any additional costs.
SMTP mail services are available worldwide.
Recipients have numerous options to check the emails (PC, smartphones, ).

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 6
2 Automation Solution

2.3 Hardware and software components used


Hardware components
FB PN_MAIL and the underlying open communication blocks can be executed on
the following hardware platforms:
CPU 31x PN/DP (as of V3.2, CPU 314C-2 PN/DP as of V3.3) (Link \8\)
S7-400 PN-CPUs
ET200S/IM 151-8
ET200PRO/ IM 154-8 CPU
WinAC, e.g. with SIMATIC Microbox PC
You can use the block from the example project.

Standard software components


The blocks are executable with STEP 7 V12 SP1 and higher. On WinAC
computers, the WinAC application version 2.6 or higher can be operated.
Table 2-1
Component No. Order number
SIMATIC STEP 7 PROFESSIONAL V12 SP1 1 6ES7822-1AA00-0YA5
Siemens AG 2013 All rights reserved

Sample files and projects


The following list includes all files and projects that are used in this example.
Table 2-2
Component Note
46817803_SMTP_Mail_V12_CODE_V1_0.zip Sample project and
Copyright

function blocks
46817803_SMTP_Mail_V12_DOKU_V1_0_en.pdf This document.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 7
2 Automation Solution

2.4 Alternative
Overview
The PN_Mail block included in this application is only intended for the use in the
hardware platforms listed in chapter 2.3.
In order to send mails as email client with the S7-CPUs, you have other options,
too.

S7-1200/S7-1500 CPUs
There are ready system blocks for S7-1200 and S7-1500 in the STEP 7 V12 library
(TM_Mail and TMAIL_C).
Instructions for the use of the two blocks can be found in the online help of the TIA
Portal.

S7-300/S7-400 CPs
The following CPs can also be configured as email client:
CP 343 -1 Advanced
CP343-1 IT
CP443-1 Advanced
Siemens AG 2013 All rights reserved

CP443-1 IT
Instructions for the use of communication processors as email client can be found
in the manuals of the communication processors used by you (e.g. for the CP343-1
Advanced (6GK7343-1GX31-0XE0) see
http://support.automation.siemens.com/WW/view/en/30564821
http://support.automation.siemens.com/WW/view/en/30564821 ).
Copyright

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 8
3 Functional Mechanisms of this Application

3 Functional Mechanisms of this Application


3.1 Program overview
Figure 3-1

TCON

TSEND

FB1 FB PN_Mail
OB1
(FB1) (FB490)

TRECV

TDISCON
Siemens AG 2013 All rights reserved

User blocks System functions

OB 1 cyclically calls FB 1 (substitute OB), which in turn calls FB PN_MAIL in each


cycle. The parameters for calling the FB PN_MAIL are retained in DB Param_DB
and DB FB1_DB, whilst the DB PN_MAIL_DB is the instance data block for
Copyright

FB PN_MAIL.
You can freely adjust the numbers of the individual blocks for your user program.
All communication with the SMTP server takes place via FB PN_MAIL.
Except for serving as a wrapper for FB PN_MAIL, the only function of FB 1 is to
intermediately store the pending error codes (STATUS, SFC_STATUS, see 0) in
another variable for only one cycle in the event of an error, so that they are
available for later analysis.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 9
3 Functional Mechanisms of this Application

3.2 Functionality of FB PN_MAIL


3.2.1 Program details on block FB PN_MAIL

FB 490 is an asynchronous function block. Since processing the sending of an


email covers several CPU cycles, the FB must be called at regular intervals (ideally
in each CPU cycle), at least in the time between triggering the sending of the email
and terminating the dialog with the SMTP server.

Supported authentication methods


FB PN_MAIL supports two authentication methods:
Anonymous, i.e. without user authentication. Nowadays, this method is
rarely used for data security reasons. Open mail relays that do not
require user identification are considered potential gateways for spam and
malware.
SMTP AUTH, currently considered as the standard for configuring mail
servers. With Extended SMTP, it is available on almost all common mail
servers.
SMTP AUTH offers four different protocols for handling the authentication: login,
plain, CRAM-MD5 and NTLM.
FB PN_MAIL only supports the login protocol.
Siemens AG 2013 All rights reserved

Syntax checks
For performance reasons, the syntax of transferred email addresses is not checked
for correctness. This applies particularly to the parameters TO_S, CC and FROM
listed below.
Copyright

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 10
3 Functional Mechanisms of this Application

3.2.2 Call interface of the PN_MAIL (FB490)

Figure 3-2 Call FB PN_MAIL


Siemens AG 2013 All rights reserved

When calling, the following parameters are transferred to FB PN_MAIL. The


respective parameter type (in, in/out, out), the data type and the optional
assignment of the parameter are listed in brackets.

REQ (in, BOOL)


This parameter controls the overall email transmission process as a whole.
Copyright

A positive edge FALSE TRUE starts a new email transmission. In all other
cases an already started transmission is continued or if no transmission job is to
be processed the block returns straight away. (However, see also COM_RST).
For details on the transmission process, see .

ADR_MAIL_SERVER (in, DWORD)


The absolute IP address of the SMTP server to which the FB transfers the email
transmission job.
The address is transferred as a string of four bytes; the most significant byte of the
DWORD contains the first byte of the IP address, the least significant byte of the
DWORD includes the last byte of the IP address. A parameter value
DW#16#C0A800C8 thus represents the IP address 192.168.0.200. (16#C0 =
192, etc.)

WATCH_DOG_TIME (in, TIME)


Specifies the maximum time that may pass until a connection is successfully
established.
If a connection to the specified SMTP server cannot be established within this
specified time, the block automatically disconnects the connection.
When using a direct Ethernet connection, the connection is usually established
within a few seconds.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 11
3 Functional Mechanisms of this Application

Note Please note that after the watchdog timeout, the FB still needs time to disconnect
the connection. This means that you should continue the FB calls, even in case
of a watchdog timeout with a respective error value.

USERNAME (in, ANY, optional)


Name for a user account on the selected SMTP server.
USERNAME is independent of the email address of the sender of the message to
be transmitted.
If no user name is entered or if USERNAME is an empty string, the FB attempts
to transmit the email without authentication. This will fail if the SMTP has not been
set up as an open relay.

PASSWORD (in, ANY, optional)


Password for the user account specified under USERNAME on the selected SMTP
server.

TO_S (in, ANY)


Recipients email address as an ASCII string with a maximum length of
240 characters.
Siemens AG 2013 All rights reserved

The parameter starts with the TO: keyword, followed by a space and the email
address between pointed brackets, for example: TO: < mail@example.com >,.
The specification is completed with a comma.

Note Email addresses on the FB input have the following syntax:


<mail@example.com>,
Copyright

CC (in, ANY, optional)


Email address of the recipient of an email copy as an ASCII string with the same
format as TO_S.
Although SMTP basically allows a list of CC recipients, only one address can be
transferred to FB PN_MAIL.

FROM (in, ANY)


Email address of the sender as an ASCII string like TO_S, but starting with the
keyword FROM:.
The FROM address does not have to be identical to the above USERNAME.
If the recipient replies to this email using Reply, the reply will be sent to the
address specified in this parameter. The validity of the FROM address is not
verified, i.e. it is basically possible to enter any information.

SUB (in, ANY, optional)


Subject line of the email as an ASCII string such as, for example, fault in the
system. (The keyword SUBJECT: is completed by FB PN_MAIL.)
If SUB is missing, the email will be sent with a blank subject line.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 12
3 Functional Mechanisms of this Application

X_PRIORITY (in, WORD, optional)


Parameter that contains the email transmission priority.
The X-PRIORITY or IMPORTANCE fields of the email are set with the aid of this
parameter.
Valid values for X_PRIORITY are 1, 3 and 5:
Table 3-1: Equivalent of the values for priority and importance
X-PRIORITY IMPORTANCE
1 high
3 --
5 low
Priority 3 corresponds to normal importance; when using this value, the
IMPORTANCE field of the email is normally not included in the email.
For all other values (or when not using this parameter), no X-
PRIORITY/IMPORTANCE fields are entered in the message.
Please note that mail servers are not committed to prioritizing email transmission
depending on X-PRIORITY or IMPORTANCE. In fact, most mail servers
completely ignore these fields and there is only a different display on the client (e.g.
MS Outlook).
Siemens AG 2013 All rights reserved

SENDER_TIMEZ (in, STRING[6], optional)


The time zone of the sending client as a string with up to 6 ASCII characters, e.g.
+0200.
1
While the FB independently determines the time and date of sending, the S7-
300/S7-400 CPU has no possibility to independently determine in which time zone
it is. Therefore, this data must be transferred when calling the FB.
The time zone indicates the difference between local time and Universal
Coordinated Time (UTC). The string consists of one + or - sign, followed by two
Copyright

digits for the hours and two additional digits for the minute difference from UTC.
During the standard time period, Germany is in zone +0100, during the daylight
saving time period the country is in zone +0200.
If no SENDER_TIMEZ is transferred or if the length of the transferred string differs
from 5 characters, the FB takes on the default value +0100.
There is no check for implausible values (e.g., +3599).

TEXT (in, ANY, optional)


ASCII string of the message text to be transmitted.
The string must not exceed a maximum length of 240 characters.
The parameter may be omitted, but it is non-permissible to transfer an empty
string.

ATTACHMENT (in, ANY, optional)


Pointer to a memory area that is attached to the message as a binary MIME
2
attachment .
The attachment must not exceed a maximum length of 65534 bytes.

1
The time at which the mail is transferred to FB PN_MAIL is valid.
2
MIME = "Multi Purpose Internet Mail Enhancement"

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 13
3 Functional Mechanisms of this Application

The parameter may be omitted, but it is non-permissible to transfer an empty string


.
Transfer a NIL anypointer (Syntax: NULL; setting:16#0100 0000 0000 0000 0000 )
if you do not sent an attachment but you still want to interconnect the input.

Note It is recommended to work with a temporary anypointer and to assign it before


calling the FB of PN_Mail. More details on setting up the any data type can be
found in the online help of the TIA portal.
If no attachment is to be sent, a NIL anypointer can be interconnected (either
NULL at the input or with the first byte 1 of the 16#01 anypointer and the
remaining 9 bytes 16#00).

DEVICE_ID (in, BYTE)


Hardware ID, based on which the TCON block identifies the PLC for establishing a
connection. Information regarding which DEVICE_ID has to be specified with which
PLC can be found in the online support portal under entry ID 51339682.

CONN_ID (in, WORD)


Siemens AG 2013 All rights reserved

Connection ID, which is used by the OUC (Open User Communication) blocks.
Make sure that this ID on the PLC is not yet assigned (for example, in Netpro or
through other communication blocks).

COM_RST (in/out, BOOL)


Flag with which an initialization of the FB is triggered.
After processing the initialization, the FB independently resets the flag. Before the
first sending of an email COM_RST has to be set to TRUE once.
Copyright

For details of the initialization, see 0.

BUSY (out, BOOL)


Flag that indicates whether an email is currently being transmitted.
From the moment the transmission procedure starts until it stops, BUSY has the
value TRUE; otherwise, it has the value FALSE.

DONE (out, BOOL)


TRUE indicates that the email has been successfully transmitted to the SMTP
server. TRUE is only pending for one cycle.

ERROR (out, BOOL)


Indicates whether an error has occurred during email processing and transmission.
If an error has occurred, the value TRUE is only pending for one cycle and is then
reset to FALSE.
STATUS or SFC_STATUS (see below) provide detailed error codes.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 14
3 Functional Mechanisms of this Application

STATUS (out, INT)


Status code of FB PN_MAIL.
This value indicates the current status of FB PN_MAIL. This can be both, a normal
status and an error message within the scope of the email transmission or an error
message received by the SMTP server that has been passed on.
This value is only pending for one cycle and is then reset to FALSE.

SFC_STATUS (out, INT)


Status code of a called SFC.
Passed on status of an SFC called by FB PN_MAIL. This code can also be used
for error analysis. A memory area transferred as an attachment that is too large
may, for example, result in an error when calling SFC 20 BLKMOV.
This value is only pending for one cycle and is then reset to FALSE.

3.2.3 Initialization/reset of FB PN_MAIL

Setting the COM_RST parameter triggers an initialization or a reset of the PN_Mail


(FB490) function block in running operation. The initialization has to be started
under the following conditions:
An initialization has to be carried out before the first use of the FB.
Siemens AG 2013 All rights reserved

It may be necessary to reset the FB if the connection has been aborted


during SMTP communication or if another unexpected event has occurred.
(In the case of most errors, however, the FB independently responds with
a reset, at the latest after the transmission period set in the
WATCH_DOG_TIME has elapsed.)
When parameters such as IP address of the SMTP server are changed in
running operation.
For performance and data consistency reasons, the complete parameter set
Copyright

transferred to the FB is not accepted to the instance data block of the FB each time
3
FB PN_MAIL is called. Instead, the following parameters are updated in the
instance DB only when the FB is called with COM_RST = TRUE:
USERNAME
PASSWORD
CC
TEXT
ATTACHMENT
DEVICE_ID

3
Since the processing of a mail transmission job covers several CPU cycles, the risk of data
inconsistencies would be high if, for example, the content of the buffer for texts or attachments
would be changed in the middle of the mail transmission.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 15
3 Functional Mechanisms of this Application

3.2.4 Email transmission

Transmission procedure
The mail transmission is triggered by a positive edge at the REQ input of the FB
PN_MAIL (FB490).
While the email is being transmitted, BUSY has the value TRUE and STATUS or
SFC_STATUS indicate the relevant current values of FB PN_MAIL or system
blocks called by this FB.
Once the transmission has been completed, BUSY returns to FALSE, and DONE
and if necessary ERROR are set.
The following table shows an overview of possible combinations of the BUSY,
DONE and ERROR values:
Table 3-2
BUSY DONE ERROR State.
0 0 0 No send job is pending
1 -- -- Send job is being processed
0 1 0 Send job successfully completed
0 0 1 Send job has been aborted with an error message (see
values of STATUS and SFC_STATUS)
Siemens AG 2013 All rights reserved

Check whether the transmission was successful


The FB transfers the email to the SMTP server for further transmission and this
transfer completes the send operation of FB PN_MAIL.
This does not mean that the email has been successfully transmitted. In fact, the
FB receives no feedback whatsoever on whether or when the email has reached
the recipient(s). The only way to find out whether sending has been successful or
not, is to check the recipients mailbox (or the SMTP server log).
Copyright

3.2.5 FB PN_MAIL (FB490): STATUS and SFC_STATUS

The following table provides an overview of the values the STATUS and
SFC_STATUS parameters can take on. In addition, it provides information on how
to respond to errors that have occurred.

Table 3-3
Return value Return value Explanation Notes
STATUS SFC_STATUS
(W#16#...) (W#16#...)
0000 Execution of FB PN_MAIL has Completing FB PN_MAIL
been completed without error without error does not
(if DONE = TRUE). automatically mean that the
sent mail arrives at the
intended recipient (see 3.2.4)
7001 FB PN_MAIL is active (BUSY = 1).
7002 7002 FB PN_MAIL is active (BUSY = 1).

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 16
3 Functional Mechanisms of this Application

Return value Return value Explanation Notes


STATUS SFC_STATUS
(W#16#...) (W#16#...)
8xxx xxxx Execution of FB PN_MAIL has For detailed information on the
been completed with an error code evaluation of the
of the communication SFCs called SFC_STATUS parameter,
internally or SFC 20 BLKMOV. please refer to the
documentation /2/ Open
Communication via Industrial
Ethernet or the
documentation Copying
Memory Areas with SFC 20
BLKMOV (see \5\) or the
online help of STEP 7 V12.
800z xxxx The error message originates from See also the SFC 20
SFC 20 BLKMOV and indicates BLKMOV documentation
an error that has occurred when (see \5\).
copying one of the parameters. The
last digit identifies the responsible
parameter:
z=1 TO_S
z=2 CC
z=3 FROM
z=4 SUB
Siemens AG 2013 All rights reserved

z=5 TEXT
z=6 ATTACHMENT
z=7 USERNAME
z=8 PASSWORD
8010 xxxx Error while establishing the COM_RST may not have
connection. been set after loading the
instance DB, see 3.2.3.
Also check the specified
connection parameters and
Copyright

whether the addressed SMTP


server is active.
8011 xxxx Error while sending data. For more information on the
evaluation of SFC_STATUS,
please refer to the description
of the FB 65 TCON, FB 63
TSEND, FB 64 TRCV, FB
66 TDISCON blocks in the
documentation /2/ Open
Communication via Industrial
Ethernet.
8012 xxxx Error while receiving data. Among other things, this error
results from an incorrectly
configured DEVICE_ID, or if
no block reset (3.2.3) was
performed after changing the
parameters.
8013 xxxx Error while establishing the
connection.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 17
3 Functional Mechanisms of this Application

Return value Return value Explanation Notes


STATUS SFC_STATUS
(W#16#...) (W#16#...)
8014 Connection cannot be established. You may have entered an
incorrect mail server IP
address
(ADDR_MAIL_SERVER) or
too short a period for
(WATCH_DOG_TIME) for
establishing a connection. It is
also possible that the CPU is
not connected to the network
or that the CPU configuration
is incorrect.
82xx, Error messages from this number See also chapter 6.3 in the
84xx, range originate from the mail server appendix.
85xx and, except for the leading digit 8,
correspond to the error number of
SMTP.
The following lines list several error
codes that may occur when using
FB PN_MAIL:
8450 Action not performed: Mailbox not Try again later.
available/cannot be accessed.
Siemens AG 2013 All rights reserved

8451 Action aborted: Local error during Try again later.


processing
8500 Syntax error: Error not detected. Check the parameters of the
This also includes the error of a FB PN_MAIL.
command string that is too long. Try to send an email without
This may also be caused by the fact authentication. To do so,
that the email server does not replace the USERNAME
support the LOGIN authentication parameter by an empty string.
method. Make sure that your SMTP
Copyright

server is configured in a way


so that it accepts LOGIN
authentications. (see 3.2.1)
8501 Syntax error: Wrong parameter or You may have entered an
argument address with incorrect syntax
for TO_S or CC. (Missing
pointed brackets, missing
comma, or similar incorrect
entries, see 3.2.2)
8502 Command unknown or not Check your entries, particularly
implemented. the FROM parameter. It may
be incomplete and you may
have forgotten @ or ., see
3.2.2.
8535 SMTP authentication incomplete. You may have entered a non-
existing user name or an
incorrect password for the
account.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 18
3 Functional Mechanisms of this Application

Return value Return value Explanation Notes


STATUS SFC_STATUS
(W#16#...) (W#16#...)
8550 Mail server cannot be accessed, You may have entered an
you have no access rights. incorrect user name or
password or the mail server
does not support your LOGIN.
Another cause could be the
incorrect entry of the domain
name after "@" in TO_S or
CC.
8552 Action aborted: Allotted memory Try again later.
size exceeded
8554 Transmission failed. Try again later.
Siemens AG 2013 All rights reserved
Copyright

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 19
4 Installation and Commissioning

4 Installation and Commissioning


Requirements
For the commissioning of the example application and the sending of emails the
following requirements have to be fulfilled:
The S7-CPU has to be able to reach the SMTP server via the following IP
configuration:
IP: 172.16.50.5
Subnet mask: 255.255.0.0
Alternatively, you can also change the IP address of the CPU.
The SMTP server has to be correctly set up and has to have access to the
internet.

Note
The installation and commissioning is described with a CPU 315-2 PN/DP. You can
also use any other PLC, listed in 2.3.

4.1 Hardware setup CPU 315-2 PN/DP


Siemens AG 2013 All rights reserved

Figure 4-1

SMTP Server
PC
CPU 315-2 PN/DP

LAN/WAN/
Ethernet Internet
Copyright

Please observe the regulations for the configuration of an S7 station.

No. Instruction Comments


1. Insert the power supply and the
CPU into the respective rack.
2. Wire the CPU with the power Mind the correct polarity!
supply.
3. Connect the CPU via Ethernet
cable with the SMTP server.
4. Connect the power supply unit to
the network.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 20
4 Installation and Commissioning

4.2 Software installation


Table 4-1 Installing the software components
No. Action Comments
1. Install STEP 7 Professional V12.0 SP1 Observe the notes of the system manual
http://support.automation.siemens.com/WW/
view/en/77991795
http://support.automation.siemens.com/WW/
view/en/77991795 .
2. Load the You can get to the entry via the following
46817803_SMTP_Mail_V12_CODE_V1_0.zip link:
example project from the Siemens Online Support http://support.automation.siemens.com/WW/
site. view/en/46817803

Opening and loading the TIA Portal project


Table 4-2
No. Action Comments
1. Load the
Siemens AG 2013 All rights reserved

46817803_SMTP_Mail_V12_CODE_V1_0.zip
file onto your engineering station and unzip the
folder.
2. Double click the 46817803_SMTP.ap12 icon in
the program folder.
The project now opens in TIA V12.
3. Click onto CPU S7-300 and download the user
program into the CPU via Online > Download
and reset PLC program.
Copyright

4. Now you can monitor the individual variables How to send an email to the SMTP server is
and the state of the CPU. described in chapter 5.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 21
4 Installation and Commissioning

4.3 Establishing an Internet connection to the SMTP server


The function of FB PN_MAIL requires an Internet/Intranet connection to be
established to an SMTP server you have selected.
Make sure that the SMTP server has an account whose access data matches the
data you provide FB PN_MAIL with. This is particularly important if the SMTP
server is not open, i.e. if it does not allow anonymous relaying of messages.

NOTE For details on configuring your Internet connection, please refer to the
documentation of your Internet service provider (ISP) and follow the relevant
instructions!

NOTE Make sure that your selected SMTP server is configured so that it accepts one of
the authentication methods supported by FB PN_MAIL (see 0)!
Siemens AG 2013 All rights reserved
Copyright

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 22
5 Operating the Application

5 Operating the Application


5.1 Sending an email with the aid of the SMTP block
FB PN_MAIL
To send an email to the SMTP server after configuring it accordingly, follow the
steps in the table below:
Table 5-1

1. Transfer the IP address of your SMTP server in FB 1 as ADR_MAIL_SERVER


parameter.
2. Specify the device ID of your PLC in FB1 at the DEVICE_ID input of FB PN_Mail.
Information regarding the device ID of your PLC can be found, amongst other
information, in the online support portal under entry ID 51339682.
3. Specify the connection ID to be used at the CONN_ID parameter. Note that this
ID must not be used in the user program or for the configuration. (Example:
3.2.2)
4. Fill DB Param_DB with the addresses and contents you want to send (see
chapter 0 and Figure 5-1).
5. Assign the ATTACHMENT input with details what data you want to send as
Siemens AG 2013 All rights reserved

attachment to the email. Make sure that the default value of the attachment (no
attachment is sent) is NULL.
6. Download the configuration to the CPU.
Copyright

7. Perform a reset of FB PN_MAIL by setting FB1_DB.COM_RST to TRUE (see


chapter 0).
After a brief moment, FB1_DB.COM_RST is reset to FALSE.
8. Set FB1_DB.Req to TRUE, in order to start the mail transfer process.
For a few moments, FB1_DB.Busy changes to TRUE. Memory area
PN_Mail_DB.buffer displays the SMTP server messages as plain text.

9. After completing the transmission, FB1_DB.Busy is reset to FALSE.


(FB1_DB.Done is set to TRUE for one cycle and then cleared; this process is
probably too fast to be viewed in the watch table.)
10. If an error preventing email transmission has occurred, FB1_DB.Error is set to
TRUE for one cycle. In this case, the variables FB1_DB.Status and
FB1_DB.sfc_Status receive the values last valid for STATUS or SFC_STATUS.
11. Check whether your email is received at the recipient. Depending on the load of
the SMTP server, this may take between a few seconds to a few minutes.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 23
5 Operating the Application

12. To send another email, reset FB1_DB.Req to FALSE and then back to TRUE.
(The mail transfer is triggered by a rising edge of FB1_DB.Req.)
13. If you are changing the receiver addresses between two mails, repeat step 0 and
thus carry out a block initialization.

5.2 Using data blocks and watch table


You can modify the supplied configuration example by adjusting the values of
DB Param_DB or the variables provided in the email watch table. In addition, the
email watch table enables you to follow the processing of the email transmission
and to check the results.
FB 1 uses the contents of DB Param_DB as parameters for calling FB PN_MAIL.
For the individual formats of the parameters, please refer to chapter 3.2.2.
Figure 5-1: DB Param_DB
Siemens AG 2013 All rights reserved
Copyright

Your values for USERNAME and PASSWORD probably differ from the values
shown here and must match the accounts that have been set up on the SMTP
server you are using.
A number of parameters for FB PN_MAIL are not taken from the DB Param_DB,
but are transmitted directly, amongst others, the REQ input as well as the IP
address of the SMTP mail server (ADR_MAIL_SERVER).

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 24
5 Operating the Application

Figure 5-2
Siemens AG 2013 All rights reserved
Copyright

The program section following the call of FB 1 is used to permanently freeze error
messages that are normally only present for one cycle and that would otherwise
have been overwritten after the next CPU cycle: If an error occurred (ERROR_FB),
the 'Error' flag is set and the values for STATUS and SFC_STATUS are copied into
the variables FB1_DB.Status and FB1_DB2.sfc_Status.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 25
5 Operating the Application

Figure 5-3 shows Email watch table while processing an email transmission job.
Figure 5-3: "Email" watch table (section)

The meaning of the entries of the watch table in detail (for further details of the
individual parameters of FB PN_MAIL see also 3.2.2):
Table 5-2
Siemens AG 2013 All rights reserved

Memory area Meaning


FB1_DB.Com_RST COM_RST = TRUE causes
initialization/reset of FB PN_Mail.
FB1_DB.Req A positive edge triggers a mail
transmission.
FB1_DB.Busy Displays whether the block currently
processes a mail transmission.
FB1_DB.Status_FB FB status.
Copyright

FB1_DB.Status Saved status of FB PN_Mail


FB1_DB.sfc_Status Saved error messages of an instruction of
FB PN_Mail
FB1_DB.Device_ID Currently used device ID, depending on
PLC used. See also online help of STEP 7
V12.
PN_Mail_DB.ADR_MAIL_SERVER Transferred IP address of SMTP server in
hex format.
PN_Mail_DB.TCONPAR.RemoteAddress.ADD First byte of the IP address in the
R[1] connection properties. Adopted by
COM_RST=TRUE from
PN_Mail_DB.ADR_MAIL_SERVER.
PN_Mail_DB.state Current state of the state machine
PN_Mail_DB.buffer1 Internal buffer of FB PN_Mail. Shows,
among others, responses of SMTP server.
PN_Mail_DB.DATA Shows which command is next sent to
SMTP server.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 26
6 Appendix

6 Appendix
6.1 SMTP commands: Minimum command set
The command set that SMTP servers must understand is limited. The minimum
implementation is already possible with only eight different commands:
Table 6-1: Minimum command set for an SMTP server
SMTP command Description
HELO/EHLO HELO or EHLO starts the SMTP session and identifies the client to the server.
(Hello/Extended
Hello)
MAIL MAIL initiates the email transmission and also provides the senders address.
RCPT (Recipient) RCPT indicates the address of one or several recipients. This command can be
executed multiple times.
DATA DATA initiates the transmission of the email message. The end of the email
message is marked by CRLF.CRLF.
RSET (Reset) RSET aborts an already initiated email transmission. The connection between
client and server remains established.
VRTY (Verify) The recipients address can be verified using VRFY.
Siemens AG 2013 All rights reserved

EXPN (Expand) Most MTAs treat EXPN like VRFY.


NOOP NOOP causes a response from the server. This prevents disconnection due to
timeout.

6.2 SMTP connection process between client and server


The table below shows an example of the message traffic that is generated when
Copyright

sending an email between the client and its server.


The commands and the actual email contents are exchanged as ASCII text
commands. This (and the use of relatively long timeouts) enables the user to
manually send an email by establishing a Telnet connection to the server, for
which the following mail commands are entered via the terminal.

Table 6-2: SMTP dialog process between client and server when sending an email without authentication
Client Server Explanation
220
smtp.example.co
m ESMTP Postfix
HELO If at this point the client sends EHLO instead of HELO, the
relay.example.org server will respond with a list of supported authentication
methods and an authentication dialog can start.
250 Hello
relay.example.org,
I am glad to meet
you
MAIL Account of the sender of the message. If respective
FROM:<bob@exam authentication methods are used, this account must exist
ple.org> on the server.

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 27
6 Appendix

Client Server Explanation


250 OK
RCPT A list of recipients accounts starts.
TO:<alice@example
.com>
250 OK Each recipients address is acknowledged by the server.
RCPT
TO:<alice@example
.com>
250 OK
DATA The list of recipients ends, the main part of the email
starts.
354 End data with Information from the server: The main part continues until
<CR><LF>.<CR> a line is transferred that consists only of the decimal point
<LF> ..
From: "Bob Sender and recipient fields follow. These fields are for
Example" information only and do not necessarily have to match the
<bob@example.org above recipients accounts!
>
To: "Alice Example"
<alice@example.co
m>
Siemens AG 2013 All rights reserved

Cc:
theboss@example.c
om
Date: Tue, 15 Jan Date and time of the message as provided by the client.
2008 16:02:43 -0500 PRIORITY and IMPORTANCE fields can be inserted here.
Subject: Test
message

Hello Alice.
Copyright

This is a test
message with 5
header fields and 4
lines in the message
body.
Your friend,
Bob
. Line with only one decimal point as the end of the email
body.
250 Ok: queued Acknowledgement of the server.
as 12345
QUIT
221 Bye

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 28
6 Appendix

6.3 Status and error messages of an SMTP server


When processing the email traffic, the SMTP server sends all status or error
messages as a three-digit decimal number, followed by the description in plain text.
Such a message looks, for example, as follows:
354 End data with.
The number 352 describes the server status (in this case: The server is ready to
receive the body of the email) while the rest of the line indicates a possible human
operator on the client side. (Here: The transmission of the email body is completed
by entering a line that only consists of one decimal point ..)
The status number is sufficient to evaluate the SMTP server status.
The first digit indicates the status number category:
2xx: Positive response, the command was executed
3xx: Positive response, but the server needs more information for
successful execution
4xx: Temporary error, for example no connection available. When trying
again, it is possible that the error no longer occurs
5xx: Permanent error, for example command not understood. No
improvement by repeating the same command.
Siemens AG 2013 All rights reserved

Table 6-3: SMTP server status codes


Status
code English description
211 System status or system help reply.
214 Help message.
220 Domain service ready. Ready to start TLS.
221 Domain service closing transmission channel.
Copyright

250 OK, queuing for node node started. Requested mail action okay, completed.
251 OK, no messages waiting for node node. User not local, will forward to forwardpath.
252 OK, pending messages for node node started. Cannot VRFY user (e.g., info is not local), but
will take message for this user and attempt delivery.
253 OK, messages pending messages for node node started.
354 Start mail input; end with <CRLF>.<CRLF>.
355 Octet-offset is the transaction offset.
421 Domain service not available, closing transmission channel.
432 A password transition is needed.
4
450 Requested mail action not taken: mailbox unavailable. ATRN request refused.

4
ATRN: Authenticated Turn, a process for mail transmission with the use of dynamic IP
addresses. Also called ODMR (On-Demand Mail Relay).

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 29
6 Appendix

Status
code English description
451 Requested action aborted: local error in processing. Unable to process ATRN request now.
452 Requested action not taken: insufficient system storage.
453 You have no mail.
454 TLS not available due to temporary reason. Encryption required for requested authentication
mechanism.
458 Unable to queue messages for node node.
459 Node node not allowed: reason.
500 Command not recognized: command Syntax error.
501 Syntax error, no parameters allowed.
502 Command not implemented.
503 Bad sequence of commands.
504 Command parameter not implemented.
521 Machine does not accept mail.
530 Must issue a STARTTLS command first. Encryption required for requested authentication
mechanism.
534 Authentication mechanism is too weak.
538 Encryption required for requested authentication mechanism.
Siemens AG 2013 All rights reserved

550 Requested action not taken: mailbox unavailable.


551 User not local; please try forwardpath.
552 Requested mail action aborted: exceeded storage allocation.
553 Requested action not taken: mailbox name not allowed.
554 Transaction failed.
Copyright

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 30
7 Literature

7 Literature
7.1 Bibliography
This list is not complete and only represents a selection of relevant literature.
Table 7-1
Topic Title
/1/ STEP7 Automating with STEP7 in AWL and SCL
Hans Berger
Publicis Publishing
ISBN 978-3-89578-412-5
/2/ Open Open Communication via Industrial Ethernet, Siemens
communication documentation A5E00711636-01

7.2 Internet link specifications


The following list is by no means complete and only provides a selection of
appropriate information.
Table 7-2
Siemens AG 2013 All rights reserved

Topic Title
\1\ Link to this document http://support.automation.siemens.com/WW/view/en/46817803
\2\ Siemens I IA/DT Customer Support http://support.automation.siemens.com
\3\ Manual: TeleService S7-Library V6.1 http://support.automation.siemens.com/WW/view/en/24781774
\4\ Manual: S7-CPs for Industrial http://support.automation.siemens.com/WW/view/en/30374198
Ethernet Configuring and
Commissioning, Part A (see
chapter 8)
Copyright

\5\ Manual: System and Standard http://support.automation.siemens.com/WW/view/en/44240604


Functions for S7-300/400 Volume 1
and Volume 2, (see chapter 3.1)
\6\ FAQ: STEP 7 Hardware Support http://support.automation.siemens.com/WW/view/en/23183356
Packages (HSPs as at: 12/2010) for
installing hardware updates in the
Hardware Configuration
\7\ FAQ: How can you update the http://support.automation.siemens.com/WW/view/en/22374877
hardware catalog without direct
Internet access?
\8\ SIMATIC S7-300, CPU 31xC and http://support.automation.siemens.com/WW/view/en/12996906
CPU 31x: Technical Data
\9\ System manual STEP 7 V12 http://support.automation.siemens.com/WW/view/en/77991795
\10\ SIMATIC NET, program blocks for http://support.automation.siemens.com/WW/view/en/30564821
SIMATIC NET S7-CPs, programming
manual

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 31
8 History

7.2.1 Relevant RFCs

SMTP was formalized in a series of RFCs (Requests for Comments; directives,


definitions and suggestions for standardization published by the Internet
Engineering Task Force).
The following table lists the most important relevant RFCs:
Table 7-3
RFC Topic URL
no.
-- Overview of all published RFCs http://tools.ietf.org/rfc
821 SMTP definition http://tools.ietf.org/html/rfc821
822 Format for ARPA Internet Text Messages, http://tools.ietf.org/html/rfc822
predecessors of email
2195 CRAM-MD5 authentication http://tools.ietf.org/html/rfc2195
2554 SMTP extension by SMTP authentication http://tools.ietf.org/html/rfc2554
method
2595 PLAIN and LOGIN authentication methods http://tools.ietf.org/html/rfc2595
2821 Extension of SMTP, obsoletes RFC 821 http://tools.ietf.org/html/rfc2821
5
2822 E-mail format , obsoletes RFC 822 http://tools.ietf.org/html/rfc2822
Siemens AG 2013 All rights reserved

2831 DIGEST-MD5 authentication method http://tools.ietf.org/html/rfc2831


5321 Definition of SMTP with further updates, http://tools.ietf.org/html/rfc5321
obsoletes RFC 2821

8 History
Copyright

Table 8-1
Version Date Modifications
V1.0 12/13 First version for STEP 7 V12

5
Here still called Internet Message Format

Sending Emails with S7-CPU


V1.0, Entry ID: 46817803 32