ESP32 IDF smtp (Simple Mail Transfer Protocol) support

ESP_Vikram
Posts: 25
Joined: Fri Nov 23, 2018 12:07 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby ESP_Vikram » Wed Feb 05, 2020 11:53 am

1. How do you specify which file to upload and how does it get to the ESP to then attach to the email?
Are you able to send the file as it is from the example?

I have embedded this file in the code in CMakelist.txt and component.mk under main/ directory. and used it in example file. You can grasp this pretty easily.

https://docs.espressif.com/projects/esp ... inary-data
For another bit of my project I am getting the ESP to call a number on event and wanted to play a recorded voice but couldn't work out how to do it.
Depending upon data, you should be able to play this. Please specify correct info for the attachment.

Code: Select all

"Content-Type: image/image/png;name=esp_logo.png\n"
"Content-Transfer-Encoding: base64\n"
"Content-Disposition:attachment;filename=\"esp_logo.png\"\n");
This is what I deed for png image! If mp3, use `audio/mpeg` etc.
As far as file is concerned, It doesn't matter unless you provide data(and extension in some cases) properly.
2. To make it a bit easier to incorporate your example into other projects, instead of using the example_connect() call, adding the individual calls would be easier to follow.
3. Thanks for adding the loop makes sense. As a suggestion, putting it into a separate function would help others to include into their project (e.g. in their project might be detect XYZ then send email). Also would show which variables need to be global as needed in the "connect to SMPT server" as well as the send_email function (could take, to/ from/ message etc. as parameters). Would also show how to deal with it outside/ together with the initialize task.
That would be good idea!
For now, I would keep the example simple and incorporate only few ideas out of these.
You might very easily modifications and do this in your example.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby leenowell » Wed Feb 05, 2020 6:26 pm

@Vikram

Thanks for your reply. I have attached a zip file with the original esp_logo file (should be exactly the one in the zip you sent) and the zero bytes file that I received via email (had to change the name so the 2 didn't conflict). Looking forward to the next version to test :) Will look into the binary file tip - looks good thanks

Lee.
Attachments
test_attachments.tar.gz
(30.4 KiB) Downloaded 630 times

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby leenowell » Thu Feb 06, 2020 8:44 pm

@Vikram

Hi,

I have finally had a chance to look at the code and have managed to fixed the attachment 0 bytes issue. I ended up changing the attachment header to be the following

Code: Select all

    len = snprintf((char *) buf, BUF_SIZE,
                   "Content-Type: image/png; name=\"esp_logo.png\"\r\n"
                   "Content-Disposition: attachment; filename=\"esp_logo.png\"\r\n"
            	   "Content-Transfer-Encoding: base64\r\n\r\n");
and it now seems to work fine :)

Thanks

Lee.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby leenowell » Fri Feb 07, 2020 7:37 am

@Vikram

I have now tried to include your example code into my overall project (it is cpp if that matters) and am having trouble with the attachment code. I get a stream of the following error from within the while loop whilst it is reading the attachment and sending it to the SMTP server
E (6131) smtp_example: mbedtls_ssl_write failed with error -0x50
E (6141) smtp_example: mbedtls_ssl_write failed with error -0x4e
E (6141) smtp_example: mbedtls_ssl_write failed with error -0x4e
etc etc etc
I had changed the ESP_LOGE to output (-ret) from ret to try and map to the codes in the header file but as you can see that didn't help unfortunately. Essentially I get -0x50 once and the -0x4e for every iteration of the while loop. Given the code is the same, I wonder whether it is something to do with the way I added the attachment file to my project? Attached is my CMakeLists.txt from within the main directory (the one at the project level I left unchanged).

Any ideas what is going on? As part of my incorporating into my project I am splitting the SMTP server connect from email send too so will post once I have it all working in case it helps anyone.

thanks

Lee.
Attachments
CMakeLists.txt
(386 Bytes) Downloaded 583 times

ESP_Vikram
Posts: 25
Joined: Fri Nov 23, 2018 12:07 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby ESP_Vikram » Sat Feb 08, 2020 11:16 am

@leenowell replied your DM.
E (6131) smtp_example: mbedtls_ssl_write failed with error -0x50
E (6141) smtp_example: mbedtls_ssl_write failed with error -0x4e
E (6141) smtp_example: mbedtls_ssl_write failed with error -0x4e

I had changed the ESP_LOGE to output (-ret) from ret to try and map to the codes in the header file but as you can see that didn't help unfortunately. Essentially I get -0x50 once and the -0x4e for every iteration of the while loop.
These are defined in `mbedtls/include/net_sockets.h` if you want to take a look and find out the issue.

Also, the example is now part of IDF master on GitHub https://github.com/espressif/esp-idf/co ... 16fd90e78a. Not much different than the last preview I shared.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby leenowell » Sat Feb 08, 2020 11:24 am

@ESP_Vikram

Thanks very much for your reply. Will take a look at the error messages but the odd thing is that this only seems to occur when you make the ssl a global variable and separate the code for sending email into a different function from the SMTP connect.

Which version of the IDF are you compiling against? In case I had an environment problem, I re-pulled v4.0 idf from git and curiously now getting errors about esp_netif_init not being declared. Searching through the idf files, it does indeed no longer appear to be there although my previous version of v4.0 (I believe it is) does have it ?

thanks

Lee.

ESP_Vikram
Posts: 25
Joined: Fri Nov 23, 2018 12:07 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby ESP_Vikram » Sat Feb 08, 2020 12:29 pm

The example is added on master branch.
You may create a patch and apply on `release/v4.0` branch if you want and it should work fine with that branch as well.
...now getting errors about `esp_netif_init` not being declared.
Is it possible that you missed `submodule update` at some point?

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby leenowell » Sat Feb 08, 2020 1:12 pm

@ESP_Vikram

Hi Vikram,

In the end I removed v4.0 (looks like the latest version has an issue when you pull it via git) and pulled the master branch instead. I have tested the latest code in the master branch and it still has the 0 bytes attachment issue. I applied my fix from before and it works fine. So if you use this instead it should be ok

Code: Select all

    len = snprintf((char *) buf, BUF_SIZE,
                   "Content-Type: image/png; name=\"esp_logo.png\"\r\n"
                   "Content-Disposition: attachment; filename=\"esp_logo.png\"\r\n"
            	   "Content-Transfer-Encoding: base64\r\n\r\n");
        	  
Will now try to refactor and split the connection from the send email.

Thanks

Lee.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby leenowell » Sun Feb 09, 2020 11:54 am

@ESP_Vikram

Hi Vikram,

I have been doing some more testing and noticed that the first sentence of the email body doesn't come through. The fix was to change the \n to \r\n\r\n on the first line as follows.

Code: Select all

    len = snprintf((char *) buf, BUF_SIZE,
                   "Content-Type: text/plain\r\n\r\n"
                   "This is a simple test mail from the SMTP client example.\r\n"
                   "\r\n"
                   "Enjoy!\n\n--XYZabcd1234\n");
    ret = writeSSLData(&m_SSL, (unsigned char *) buf, len);
thanks

Lee.

ESP_Vikram
Posts: 25
Joined: Fri Nov 23, 2018 12:07 pm

Re: ESP32 IDF smtp (Simple Mail Transfer Protocol) support

Postby ESP_Vikram » Sun Feb 09, 2020 5:10 pm

@leenowell

Thanks for your reports.

For gmail server and gmail's web client as well as Samsung Email client work fine for me.

I need some info to reproduce issues at my end to add and check if fixes work.

1. What's the email server?
2. Which email client do you use to receive email?

Thanks again for your findings.

Who is online

Users browsing this forum: No registered users and 134 guests