<root>
<document scada='0' projtype='1' schema='103' license='1,FREE_VERSION,TCW03R' title='New Embedded Project' description='' fcversion='720896' target='ESP.ESP32.ESP32_WROOM_32' >
	<config data='' clkspd='240000000' simspd='0' usewdt='0' constif='0' commport='6' Use3V3='0' />
	<plugins >
		<dll_models enabled='1' />
	</plugins>
	<supplement use='1' head='#undef A
#undef B
#include &quot;esp_tls.h&quot;
#include &quot;mbedtls/base64.h&quot;
#include &quot;esp_crt_bundle.h&quot;
#include &lt;string.h&gt;

#define A 0
#define B 32

// Global handle for the TLS session
struct esp_tls *g_tls = NULL; 

// Professional Helper: Sends SMTP commands and verifies the numeric response code
int smtp_check(struct esp_tls *tls, const char *cmd, const char *code) {
    if (tls == NULL) return 0;
    char buf[256];
    
    // Send command if provided (otherwise just read)
    if (strlen(cmd) &gt; 0) {
        if (esp_tls_conn_write(tls, cmd, strlen(cmd)) &lt;= 0) return 0;
    }
    
    // Wait for server response (up to 2 seconds)
    for(int i=0; i &lt; 20; i++) {
        vTaskDelay(pdMS_TO_TICKS(100)); 
        int ret = esp_tls_conn_read(tls, buf, sizeof(buf)-1);
        if (ret &gt; 0) {
            buf[ret] = &apos;\0&apos;;
            if (strstr(buf, code) != NULL) return 1;
        }
    }
    return 0; // Timeout or code not found
}

' body='' />
	<debug />
	<traces />
	<ghost >
		<FK2 >
			<data name='FK2D0' port='4294967295' pin='4294967295' />
			<data name='FK2D1' port='4294967295' pin='4294967295' />
			<data name='FK2D2' port='4294967295' pin='4294967295' />
			<data name='FK2D3' port='4294967295' pin='4294967295' />
			<data name='FK2D4' port='4294967295' pin='4294967295' />
			<data name='FK2D5' port='4294967295' pin='4294967295' />
			<data name='FK2D6' port='4294967295' pin='4294967295' />
			<data name='FK2D7' port='4294967295' pin='4294967295' />
			<data name='FK2D8' port='4294967295' pin='4294967295' />
			<data name='FK2D9' port='4294967295' pin='4294967295' />
			<data name='FK2D10' port='4294967295' pin='4294967295' />
			<data name='FK2D11' port='4294967295' pin='4294967295' />
			<data name='FK2D12' port='4294967295' pin='4294967295' />
			<data name='FK2D13' port='4294967295' pin='4294967295' />
			<data name='FK2D14' port='4294967295' pin='4294967295' />
			<data name='FK2D15' port='4294967295' pin='4294967295' />
			<data name='FK2A0' port='4294967295' pin='4294967295' />
			<data name='FK2A1' port='4294967295' pin='4294967295' />
			<data name='FK2A2' port='4294967295' pin='4294967295' />
			<data name='FK2A3' port='4294967295' pin='4294967295' />
			<data name='FK2A4' port='4294967295' pin='4294967295' />
			<data name='FK2A5' port='4294967295' pin='4294967295' />
		</FK2>
		<ICD >
			<data name='AnalogPrescaleValue' value='19' />
			<data name='DigitalSampleRate' value='100000' />
			<data name='BreakpointCount' value='8' />
			<data name='CallStackDepthCount' value='8' />
			<data name='ClockPort' value='1' />
			<data name='ClockPin' value='6' />
			<data name='DataPort' value='1' />
			<data name='DataPin' value='7' />
			<data name='UseDefaultPins' value='1' />
			<data name='WrapEnabled' value='1' />
			<data name='CommsDelayOverridden' value='0' />
			<data name='CommsDelay' value='1' />
			<data name='CalculatedCommsDelay' value='1' />
			<data name='AnalogEB2PrescaleValue' value='2' />
			<data name='DigitalEB2SampleRate' value='100000' />
		</ICD>
		<pins >
			<digital A='0' B='0' C='0' D='0' E='0' F='0' G='0' H='0' I='0' J='0' K='0' L='0' M='0' N='0' O='0' P='0' Q='0' R='0' S='0' T='0' U='0' V='0' W='0' X='0' Y='0' Z='0' />
			<analog A='0' B='0' C='0' D='0' E='0' F='0' G='0' H='0' I='0' J='0' K='0' L='0' M='0' N='0' O='0' P='0' Q='0' R='0' S='0' T='0' U='0' V='0' W='0' X='0' Y='0' Z='0' />
		</pins>
	</ghost>
	<components >
		<settings autoimg='0' center='1' unitscale='0' fixedscale='0' fixedx='25' fixedy='25' fixedz='25' headcode='1' />
		<definition guid='70181a5c-b5e6-4d4e-843b-73324e531531' vstate='30' vmin='1' vmaj='1' srcleaf='ESP_Mail_Client.fcfx' visiblename='ESP_Mail_Client' description='' category='Comms: Networking' category2='' category3='' bIs2dOnly='0' bIs3dOnly='0' bIsWysiwyg='0' catenable='1' author='' manuname='' manucode='' sysinfo='139' keywords='' dynamic='1' scadaCompatible='0' embeddedCompatible='1' showmacros='1' iconpath='..\..\Downloads\GImage.png' />
		<component class_type='root' codename='ESP_Mail_Client' panelId='-1' x='0' y='0' z='0' xsz='1' ysz='1' zsz='1' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='1' layer='0' poslock='0' ancTop='0' ancBottom='0' ancLeft='0' ancRight='0' ancMinX='0' ancMinY='0' comp2dType='0' >
			<resources />
			<properties />
			<values />
			<events />
			<apis >
				<api name='Send_Mail' alt='Send_Mail' type='1' proto='1' />
			</apis>
			<variables >
				<variable public='0' >
					<def class_type='variable' name='false' type='b1' description='' isconst='1' isHidden='0' isinit='1' usrinit='0' setinit='0' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='true' type='b1' description='' isconst='1' isHidden='0' isinit='1' usrinit='1' setinit='1' />
				</variable>
			</variables>
			<macros >
				<macro >
					<flowline name='Send_Mail' description='' statediag='0' >
						<return name='Return' type='b1' description='' isconst='0' isHidden='0' isinit='0' usrinit='0' setinit='' />
						<param name='HOST_ADDRESS' type='T8' description='SMTP server address. (e.g., &quot;smtp.gmail.com&quot;)' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='64' />
						</param>
						<param name='PORT' type='u16' description='The secure SSL port number. (Use 465 for Gmail).' isconst='0' isHidden='0' isinit='0' usrinit='0' setinit='' />
						<param name='USER_NAME' type='T8' description='Your full sender email address. (e.g., &quot;yourname@gmail.com&quot;).' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='64' />
						</param>
						<param name='PASSWORD' type='T8' description='Your 16-character Google App Password (not your login password).' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='32' />
						</param>
						<param name='DESTINATION' type='T8' description='Recipient E-mail addresses, comma-separated. (e.g., &quot;a@mail.com, b@mail.com&quot;). 256B Max.' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='256' />
						</param>
						<param name='SUBJECT' type='T8' description='The title of the email. (e.g., &quot;ESP32 System Alert&quot;). 64B Max' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='64' />
						</param>
						<param name='MSG_LINE1' type='T8' description='First line of the message body. (e.g., &quot;Status: System Online&quot;). 128B Max' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='128' />
						</param>
						<param name='MSG_LINE2' type='T8' description='Second line of the message body. (e.g., &quot;Sensor Reading: 25.4&quot;). 128B Max' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='128' />
						</param>
						<param name='MSG_LINE3' type='T8' description='Third line of the message body. (e.g., &quot;Battery Level: 85%&quot;). 128B Max' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='128' />
						</param>
						<param name='MSG_LINE4' type='T8' description='Fourth line of the message body. (e.g., &quot;Location: Gate 1&quot;). 128B Max' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='128' />
						</param>
						<command class_type='native' title='Code' textshow='0' ccode='/* 1. Initialization &amp; Return Value */
FCR_RETVAL = 0; 
g_tls = NULL; 

// Static buffers move memory usage from Stack to Heap to prevent Stack Overflow
static char mail_cmd[600]; 
static char dest_copy[300]; 
static unsigned char b_out[128]; 
static char b64_cmd[256];

esp_tls_cfg_t cfg = {
    .crt_bundle_attach = esp_crt_bundle_attach,
    .timeout_ms = 10000,
};

struct esp_tls *handle = esp_tls_init();
if (handle == NULL) return FCR_RETVAL;

/* 2. Connect to Server */
if (esp_tls_conn_new_sync(FCL_HOST_ADDRESS, strlen(FCL_HOST_ADDRESS), FCL_PORT, &amp;cfg, handle) == 1) {
    g_tls = handle;
    vTaskDelay(pdMS_TO_TICKS(1500)); 

    /* 3. Authentication Sequence */
    if (!smtp_check(g_tls, &quot;EHLO ESP32\r\n&quot;, &quot;250&quot;)) goto close;
    if (!smtp_check(g_tls, &quot;AUTH LOGIN\r\n&quot;, &quot;334&quot;)) goto close;

    size_t b_len;
    // Encode &amp; Send Username
    mbedtls_base64_encode(b_out, 128, &amp;b_len, (unsigned char*)FCL_USER_NAME, strlen(FCL_USER_NAME));
    b_out[b_len] = &apos;\0&apos;;
    snprintf(b64_cmd, sizeof(b64_cmd), &quot;%s\r\n&quot;, (char*)b_out);
    if (!smtp_check(g_tls, b64_cmd, &quot;334&quot;)) goto close;

    // Encode &amp; Send Password
    mbedtls_base64_encode(b_out, 128, &amp;b_len, (unsigned char*)FCL_PASSWORD, strlen(FCL_PASSWORD));
    b_out[b_len] = &apos;\0&apos;;
    snprintf(b64_cmd, sizeof(b64_cmd), &quot;%s\r\n&quot;, (char*)b_out);
    if (!smtp_check(g_tls, b64_cmd, &quot;235&quot;)) goto close;

    /* 4. Envelope - Multi-Recipient Handling */
    char *token;

    // Set Mail From
    snprintf(mail_cmd, sizeof(mail_cmd), &quot;MAIL FROM:&lt;%s&gt;\r\n&quot;, FCL_USER_NAME);
    if (!smtp_check(g_tls, mail_cmd, &quot;250&quot;)) goto close;
    
    // Split DESTINATION by commas
    strncpy(dest_copy, FCL_DESTINATION, sizeof(dest_copy));
    token = strtok(dest_copy, &quot;,&quot;);
    while (token != NULL) {
        while(*token == &apos; &apos;) token++; // Clean leading spaces
        
        snprintf(mail_cmd, sizeof(mail_cmd), &quot;RCPT TO:&lt;%s&gt;\r\n&quot;, token);
        if (!smtp_check(g_tls, mail_cmd, &quot;250&quot;)) goto close;
        
        token = strtok(NULL, &quot;,&quot;);
    }

    /* 5. Message Content */
    if (smtp_check(g_tls, &quot;DATA\r\n&quot;, &quot;354&quot;)) {
        snprintf(mail_cmd, sizeof(mail_cmd), &quot;Subject: %s\r\n&quot;, FCL_SUBJECT);
        esp_tls_conn_write(g_tls, mail_cmd, strlen(mail_cmd));
        
        snprintf(mail_cmd, sizeof(mail_cmd), &quot;To: %s\r\n\r\n&quot;, FCL_DESTINATION);
        esp_tls_conn_write(g_tls, mail_cmd, strlen(mail_cmd));
        
        // Body Content - 4 Lines
        esp_tls_conn_write(g_tls, FCL_MSG_LINE1, strlen(FCL_MSG_LINE1));
        esp_tls_conn_write(g_tls, &quot;\r\n&quot;, 2);
        esp_tls_conn_write(g_tls, FCL_MSG_LINE2, strlen(FCL_MSG_LINE2));
        esp_tls_conn_write(g_tls, &quot;\r\n&quot;, 2);
        esp_tls_conn_write(g_tls, FCL_MSG_LINE3, strlen(FCL_MSG_LINE3));
        esp_tls_conn_write(g_tls, &quot;\r\n&quot;, 2);
        esp_tls_conn_write(g_tls, FCL_MSG_LINE4, strlen(FCL_MSG_LINE4));
        esp_tls_conn_write(g_tls, &quot;\r\n&quot;, 2);
        
        if (smtp_check(g_tls, &quot;.\r\n&quot;, &quot;250&quot;)) {
            FCR_RETVAL = 1; 
        }
    }

close:
    smtp_check(g_tls, &quot;QUIT\r\n&quot;, &quot;221&quot;);
    esp_tls_conn_destroy(g_tls);
    g_tls = NULL;
} else {
    esp_tls_conn_destroy(handle);
}' language='C' />
					</flowline>
				</macro>
				<macro >
					<flowline name='Main' description='' statediag='0' >
						<return name='Return' type='v0' description='' isconst='0' isHidden='0' isinit='0' usrinit='' setinit='' />
					</flowline>
				</macro>
			</macros>
			<component class_type='ref' guid='5c934fad-aebc-4d51-bb76-fc0c4fae27f1' vmin='0' vmaj='1' codename='LibraryComponentSimple1' panelId='0' x='76' y='-76' z='0' xsz='100' ysz='100' zsz='100' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='0' layer='2' poslock='1' ancTop='0' ancBottom='0' ancLeft='0' ancRight='0' ancMinX='0' ancMinY='0' comp2dType='0' >
				<resources />
				<properties />
				<values >
					<value target='Text::sText' data='ESP Mail
Client' />
					<value target='Text::nVerticalAlignment' data='002' />
				</values>
				<events />
				<apis />
				<variables />
				<macros />
			</component>
		</component>
	</components>
	<scadaresourcelookup />
	<keymap />
	<panel2d shadows='0' lighting='2' brightness='0' >
		<background rgb='7292207' img='' style='0' />
		<camera xe='0' ye='0' ze='1862.61' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<viewport dx='454' dy='808' zoom='29.4931' fix_topleft='0' />
		<page x='1000' y='1000' show='0' rgb='7360576' />
		<winpos ID='7005' RectRecentFloat='1368,148,1830,1022' RectRecentDocked='0,0,454,837' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
	</panel2d>
	<panel3d shadows='0' lighting='2' brightness='0' perspective='1' >
		<background rgb='8409120' img='' style='0' />
		<table rgb='7360576' img='' style='0' size='0' />
		<camera xe='0' ye='0' ze='781.66' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<camerakey0 xe='0' ye='0' ze='300' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<camerakey1 xe='-3.67394e-14' ye='0' ze='-300' xt='0' yt='0' zt='0' xquat='0' yquat='1' zquat='0' wquat='6.12323e-17' />
		<camerakey2 xe='0' ye='-300' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.707107' yquat='0' zquat='0' wquat='0.707107' />
		<camerakey3 xe='-3.67394e-14' ye='300' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-4.32978e-17' yquat='0.707107' zquat='0.707107' wquat='4.32978e-17' />
		<camerakey4 xe='-300' ye='-6.66134e-14' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.5' yquat='0.5' zquat='0.5' wquat='0.5' />
		<camerakey5 xe='300' ye='-6.66134e-14' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.5' yquat='-0.5' zquat='-0.5' wquat='0.5' />
		<camerakey6 xe='-173.205' ye='-173.205' ze='173.205' xt='0' yt='0' zt='0' xquat='-0.424708' yquat='0.17592' zquat='0.339851' wquat='0.820473' />
		<winpos ID='7004' RectRecentFloat='1631,626,1879,1080' RectRecentDocked='0,0,484,812' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
	</panel3d>
	<panels2d count='1' >
		<panel2d_0 id='0' name='2D Panel' type='0' >
			<winpos ID='1171' RectRecentFloat='1056,261,1554,711' RectRecentDocked='1016,183,1564,995' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='1' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='1' />
			<background fill='288230376151053813' showgrid='1' gridstyle='1' gridsize='10' gridbrush='71776119075691740' snaptogrid='1' fitcomp='0' disp_x='300' disp_y='400' scale='100' disp_col='16777215' />
		</panel2d_0>
	</panels2d>
	<layout >
		<view type='0' name='Main' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAARAQAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='Send_Mail' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAARAQAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
	</layout>
</document>
</root>
