Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 11:25 09 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Sending EMails from the PicoMiteWeb

     Page 1 of 3    
Author Message
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 01:29am 31 Mar 2023
Copy link to clipboard 
Print this post

For anyone who is experimenting with the PicoMiteWeb you can also use it to send emails.  The following is an extract from the PicoMiteWeb user manual (still under development).

==========================================================

When you have a remote device like the PicoMiteWeb it is useful to send emails from it to raise the alarm over faults, report on its status and so on.  The PicoMiteWeb can do this using the SMTP protocol to connect to a server which will then relay the email to its destination.

The following example uses the free SMTP relay service offered by Twilio SendGrid (https://sendgrid.com/) which allows for 100 emails per day (plenty for the PicoMiteWeb).  This example follows the protocol described on their web page  https://docs.sendgrid.com/for-developers/sending-email/getting-started-smtp  which goes into more detail.

To get started you need to create a free login at SendGrid and generate an API key which is a 79 character alpha numeric string that is used to authenticate your access to their servers.  This key then needs to be converted to a Base64 encoded string (using https://www.base64encode.org  ) which will result in a 92 character alpha numeric string.  This should be used to replace the nnnnnnnnnn string in the first line of the following program.  The other four lines at the start of the program should also be replaced with your data:

Const key$ =      "nnnnnnnnnn"
Const mailfrom$ = "from@server.com"
Const mailto$ =   "to@server.com"
Const subject$ =  "Test EMail"
Const message$ =  "Test of SMTP relay"

Const cr = Chr$(13)+Chr$(10)
Dim buff%(4096/8)
Dim String body$

body$ = "From: " + mailfrom$ + cr
body$ = body$ + "To: " + mailto$ + cr
body$ = body$ + "Subject: " + subject$ + cr + cr
body$ = body$ + message$ + cr + "." + cr

WEB OPEN TCP CLIENT "smtp.sendgrid.net", 2525
WEB TCP CLIENT REQUEST "AUTH LOGIN" + cr, buff%()
WEB TCP CLIENT REQUEST "YXBpa2V5" + cr, buff%()
WEB TCP CLIENT REQUEST key$ + cr, buff%()
WEB TCP CLIENT REQUEST "MAIL FROM: " + mailfrom$ + cr, buff%()
WEB TCP CLIENT REQUEST "RCPT TO: " + mailto$ + cr, buff%()
WEB TCP CLIENT REQUEST "DATA" + cr, buff%()
WEB TCP CLIENT REQUEST body$, buff%()
pause 500
WEB CLOSE TCP CLIENT
if LInStr(buff%(), "250 Ok:") = 0 Then Print "Email send failed"

These days using an SMTP relay service is complicated by variations in the SMTP protocol used by each vendor and the various protections in place to reduce the amount of spam.  This example is specific to the SMTP protocol as used by SendGrid however other services such as Google Mail can also be used but the program must be modified to accommodate their own version of the SMTP protocol.

Geoff
Geoff Graham - http://geoffg.net
 
NPHighview

Senior Member

Joined: 02/09/2020
Location: United States
Posts: 195
Posted: 02:21am 01 Apr 2023
Copy link to clipboard 
Print this post

Whew! I thought you were going to show an SMTP server implementation in MMBasic!
Live in the Future. It's Just Starting Now!
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8600
Posted: 11:55am 01 Apr 2023
Copy link to clipboard 
Print this post

Having passed the third degree from SendGrid to get an account this works nicely IFF you follow Geoff's instructions (I missed the base64 encode of the key to start with)

Now my Pico can send me emails with updates as an alternative to MQTT
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3022
Posted: 01:16pm 01 Apr 2023
Copy link to clipboard 
Print this post

+1 for this capability. I will be using it when I get back to the PicoMiteWeb "Solar Greenhouse"--most of my "smart" sensors email me when something is out of range--or just for daily reporting.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
PhilP

Newbie

Joined: 25/01/2017
Location: United Kingdom
Posts: 31
Posted: 02:18pm 11 Apr 2023
Copy link to clipboard 
Print this post

The ability to send emails from the PicomiteWeb seems to be the solution to no longer having access to 3G in the UK at the end of the year. My SIM800 and SIM900 are not compatible with 4G so a new approach is needed.
I was excited by Geoff's message and have been trying to get the program working. I have read and re-read the SendGrid documents but still can't get my head around about how to set up some of the fields. I have navigated the complex authentication process, obtained the API key and encoded it in Base64 so that is fine.
My problems come with trying to understand who am I emailing 'from'. I want to use my normal email address i.e. me@gmail.com as the recipient but I am confused about who is the sending 'server'. Is it the PicomiteWeb itself and if so what is it's server name. Or do I have to have an email program running as well with another email address. Can I send and receive from the same email address?
I have tried various combinations but they all result failed messages.
I think I am floundering in treacle at the moment. Can anyone offer any suggestions please?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8600
Posted: 03:00pm 11 Apr 2023
Copy link to clipboard 
Print this post

mailfrom is any valid email address you normally use that you have verified on the sender authentication screen (single sender authentication)

mailto is any email address that will be the recipient of the message
 
DaveJacko
Regular Member

Joined: 25/07/2019
Location: United Kingdom
Posts: 53
Posted: 09:45pm 11 Apr 2023
Copy link to clipboard 
Print this post

Welcome PhilP  !
do have a look at 2G, works with my SIMx00 and texts on EE
maybe they are turning off 3G, but good old 2G is used by a lot of UK infrastructure.
I've looked into the issue, and feel that 2G will be around for quite a while.
I share your pain, the internet protocol is sooo complicated,
I've pretty much given up on trying to understand it fully.
cheers Dave
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 10:41am 12 Apr 2023
Copy link to clipboard 
Print this post

  PhilP said  My problems come with trying to understand who am I emailing 'from'. I want to use my normal email address i.e. me@gmail.com as the recipient but I am confused about who is the sending 'server'

You are connecting to an SMTP Relay (not a server). Normally when an email is to be sent the sender (ie, the WebMite) would directly connect to the receiving machine (ie, hotmail.com) and transfer the message using the SMTP protocol.  However, this is complicated for a little device like the WebMite and anyway, in these days of spam emails, the receiving machines have lots of restrictions (like only accepting connections from recognised sources, only accepting encrypted connections, etc).

With an SMTP relay service (like SendGrid) they check who you are and they guarantee that you are not a spammer by monitoring your outgoing traffic, etc.  So mail receivers trust SendGrid and will accept emails that SendGrid have relayed from your WebMite.  This is why people like SendGrid make you jump thru hoops when you sign up for a free account... they want to be sure that you are not a spammer.

So, SendGrid is just a relay service.  They accept your email and send it on to its destination.  If you used me@gmail.com as your From address then that is what the receiver will perceive it as having originated from.

  PhilP said  Or do I have to have an email program running as well with another email address.

Defiantly not.

  PhilP said  Can I send and receive from the same email address?

Yes.

To make it clearer.  If you have an email address like me@gmail.com you can sign up for a free account with SendGrid and in the process tell them your email address (ie, me@gmail.com).  They will then verify that you do indeed own that address.  You can then create a key which will be linked to the email address that you gave them.

On the WebMite you use the key (after converting it to Base64) and use your registered email address (ie, me@gmail.com) as the From address.  This last bit is important, SendGrid will not relay the message if the address in the From field is different from your registered address.

The To address can be whoever you want including yourself.  Both the From and To addresses can use the same address and, in that case, you will end up seeing the message from the WebMite in your InBox.

It can help to see the messages sent back by SendGrid as they often explain in plain English why they rejected your email.  You can easily do that by putting the command LONGSTRING PRINT buff%() after each WEB TCP CLIENT REQUEST command.  

For example:
      WEB TCP CLIENT REQUEST "YXBpa2V5" + cr, buff%()
      LONGSTRING PRINT buff%()

Geoff
Geoff Graham - http://geoffg.net
 
PhilP

Newbie

Joined: 25/01/2017
Location: United Kingdom
Posts: 31
Posted: 10:44am 12 Apr 2023
Copy link to clipboard 
Print this post

@peter - Thanks for clarification. In answer to my question whether I can send emails to myself the answer seems to be possibly. I have now got the following responses from Send Grid
334 UGFzc3dvcmQ6
235 Authentication successful
250 Sender address accepted
250 Recipient address accepted
354 Continue
but I have not received any emails yet. I will keep trying.

@Dave - I may be wrong but I believe that 2G and 3G are inefficient in usage of bandwidth so they will be going. Everything will either need to go via a 4G or 5G bearer. A SimCom SIM7600E supports SMS over 4G but is expensive hence the interest in email with PicoWeb.

Phil
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 10:55am 12 Apr 2023
Copy link to clipboard 
Print this post

After
WEB TCP CLIENT REQUEST "DATA" + cr, buff%()
you should get back the message 354 Continue

After
WEB TCP CLIENT REQUEST body$, buff%()
you should get back a message starting with 250 Ok:

It sounds like you are not seeing 250 Ok: and that implies that you have not constructed the message in body correctly.  Use my example as a template, it must finish with cr + "." + cr

Geoff
Geoff Graham - http://geoffg.net
 
PhilP

Newbie

Joined: 25/01/2017
Location: United Kingdom
Posts: 31
Posted: 08:03am 13 Apr 2023
Copy link to clipboard 
Print this post

Thanks Geoff for the very helpful information. I am beginning to get my head around this stuff. It will be a great benefit when i get it working.
I worked out how to print out the buffer using LONGSTRING myself by reading the manual - a proud moment. I did use your code in my program changing the fields as necessary.

I did not print out the last buff% message in my last post but have now. It says "250 Ok: queued as qTd6C85PT_O_yyLAJKlQsA" which obviously means that it has reached Sendgrid and is in its system waiting for something before sending it back to me.
I checked on my Sendgrid account page and indeed it shows that it has received 8 emails from me but delivered none of them. I am still investigating the settings as to why but so far they seem OK but there are a lot of them.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 09:09am 13 Apr 2023
Copy link to clipboard 
Print this post

In my experience SendGrid immediately forwards the email after responding with 250 Ok:

Perhaps your emails are caught in your spam folder?

Geoff
Geoff Graham - http://geoffg.net
 
PhilP

Newbie

Joined: 25/01/2017
Location: United Kingdom
Posts: 31
Posted: 07:09pm 15 Apr 2023
Copy link to clipboard 
Print this post

Geoff - the issue is not technical but my SendGrid account is still not activated. SendGrid appears to primarily provide commercial services to companies who want to do large scale email shots. I have an 'open ticket' with SendGrid and they want to know how I will be using their services to market to my clients, how I choose the recipients, details of my company, etc. I have told that I am a private individual and will be doing 'transactional emailing' in their terminology. I am awaiting to see if my account gets activated. Perhaps I am not the sort of person they want?
Phil
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2870
Posted: 01:29am 16 Apr 2023
Copy link to clipboard 
Print this post

Hi Geoff, All,

I wont pretend I understand all of this. YET!,

But can the Pico RECEIVE an email?

This would be a good way of requesting a status update of something or issuing a command remotely.

Kind regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 06:59am 16 Apr 2023
Copy link to clipboard 
Print this post

  PhilP said  SendGrid appears to primarily provide commercial services to companies who want to do large scale email shots.

That is correct but they still have a free service which works well.  Perhaps you have convinced them that you want a paid account, I'm not sure how you can un-convince them!!!  Maybe try opening a new account.  BTW because they provide a commercial service they are reliable and robust - just what we need.

  bigmik said  But can the Pico RECEIVE an email?

No, that would require implementing the POP protocol, way beyond what the WebMite can do.

Geoff
Geoff Graham - http://geoffg.net
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3674
Posted: 01:18pm 16 Apr 2023
Copy link to clipboard 
Print this post

  bigmik said  But can the Pico RECEIVE an email?

Possibly. Via a server that holds it and the pico would download it.

I see Geoff said no but POP3 isn't too hard (maybe something - a support API or whatever - missing from what Pico can do?).

John
Edited 2023-04-16 23:20 by JohnS
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3022
Posted: 03:11pm 16 Apr 2023
Copy link to clipboard 
Print this post

  Geoffg said  
  bigmik said  But can the Pico RECEIVE an email?

No, that would require implementing the POP protocol, way beyond what the WebMite can do.

Geoff


Perhaps related: can the WebMite receive a UDP message?
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
PhilP

Newbie

Joined: 25/01/2017
Location: United Kingdom
Posts: 31
Posted: 09:22am 19 Apr 2023
Copy link to clipboard 
Print this post

Geoff - I have convinced SendGrid that I am a private individual and they are happy with that. They asked more questions about social media I use, which emails addresses I will use and example emails. I have finally satisfied them and I now have an active account. I can now send me emails from my PicoMiteWeb. As Shakespeare said All's Well That Ends Well.  
SendGrid are obviously very cautious about  who has access to their servers - quite rightly. So if other people try to open an account with them they might get similar queries and it is worth persevering.  Perhaps it depends on who reviews requests for accounts in SendGrid - some may be more diligent than others.
Anyway thanks Geoff for pointing out this facility which will be very useful for my applications.

Phil
 
Andrew_G
Guru

Joined: 18/10/2016
Location: Australia
Posts: 842
Posted: 01:37am 27 Aug 2023
Copy link to clipboard 
Print this post

G'day all,
I'm trying to set up Geoff's SC Watering System to send emails from the WebMite but I cant get past SendGrid's 20++ questions.
I'm only at the very beginning!

It seems to be insisting that I enter a Company Name and its website.
It didn't like " " - any suggestions?

Rather than annoying Geoff and Peter have any of you recorded how you progressed through the Q/A process?

Many thanks,

Andrew
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 01:49am 27 Aug 2023
Copy link to clipboard 
Print this post

I didn't have any trouble when I signed up but it seems that you and Phil has not been so lucky.  Perhaps they have since tightened up their criteria.

I will re look at it (might be a few days though).

Geoff
Geoff Graham - http://geoffg.net
 
     Page 1 of 3    
Print this page
© JAQ Software 2024