3

I want to make my Arduino connect via WiFi and then send an email. The idea is very simple, but the problem is that I've never worked with those protocols and other things before. I just wanted something that's already premade, so I would just change the information.

Is there a library or a function that sends the email?

If someone could help me to understand, so I can make it myself, that would be the best.

  • Email might be too much for something that runs on a 16 MHz. Timeout would probably prevent this from happening. What board are you using? – Anonymous Penguin Apr 7 '14 at 21:43
  • 1
    But this is fairly simple with an external service as temboo or similar services, and perfectly possible with an arduino. If the user posted some code using a library for one of such services, would it be an adequate question? So the problem is that it is not adequate to recommend SW solutions due to SO policies? – drodri Apr 7 '14 at 22:06
  • @drodri I don't quite understand your last few sentences... are you asking if it would be on topic if using a service like that? If so, then it could be considered to answer the question. I'm not aware of any SE policies that don't allow recommending software solutions... do you mean no shopping questions? If so, that's a completely different topic. – Anonymous Penguin Apr 9 '14 at 2:31
  • It is certainly possible and I use it with small text alerts. Look up the Ethernet library - the WiFi version should work in a similar way. Essentially you open a connection to port 25 (or applicable port at your mail server) and negotiate the HELO with println() and send the email. Lookup "telnet to send email" as it is essentially the syntax you will need. Note that most email providers will block such attempts to send email (to restrict spam-bots), so be sure it is permitted before trying it. – Ron J. Apr 9 '14 at 13:28
5

What you're looking for is a SMTP (Simple Mail Transport Protocol) library. A Google search finds 2 for the Arduino:

  • SMTPClient

    This seems to be a minimal-capability SMTP-only library that piggybacks on top of a connection library.

  • FullIP

    This seems to be a full IP package for the Arduino that works with select network devices which includes SMTP capability.

  • I am facing the same problem, but concerned about the security. If using e.g. my gmail account, I would have to provide my gmail passwd to the smtp connection, which is not very secure. I have tried external services, as temboo, which allows to store my gmail credentials, and I connect to the service (http) via service credentials, which are safe either, but at least seems an extre level of indirection. Which approach would you recommend? Does this concern deserve its own question? – drodri Apr 7 '14 at 23:05
  • GMail can use SSMTP, which is SMTP secured with SSL. I'm not sure if there are any SSMTP libraries for Arduino though, or even if it has enough power for it. You may need something like the Due or Yun (or something bigger, like a RPi or BB) which can provide a bit more oomph. – Ignacio Vazquez-Abrams Apr 7 '14 at 23:09
  • @drodri For the first library listed, it only uses an unauthenticated SMTP server... no passwords. There was some mention about implementing that later, but I don't know if it would use SSMTP or not. – Anonymous Penguin Apr 9 '14 at 2:27
3

You actually present 2 issues here, which can be handled separately:

  • You are concerned about having a WiFi, not an Ethernet shield. This doesnt affect at all for sending emails or any other network stuff. When you have internet connection, it is almost transparent from the programming side. So I recommend first to try your wifi shield connection and configuration as a separate problem, with a more simple task, as a simple HTTP_GET to a known web page. A function like this might help, assuming that you have an instance of WiFiClient (which is a Client), that has been connected (with connect) to the URL and port 80:

    void http_get(Client& www, char website[], char webpage[], int timeout, Stream& stream) {
        if (www.connected()) {
            int n = www.print("GET ");
            www.print(webpage);
            www.print(" HTTP/1.1\r\n");
            www.print("Host: ");
            www.print(website);
            www.print("\r\n");
            www.print("\r\n");
        } else {
            stream.println(F("Connection failed get"));
            return;
        }
    
        /* Read data until either the connection is closed, or the idle timeout is reached. */
        unsigned long lastRead = millis();
        while (www.connected() && (millis() - lastRead < timeout)) {
            while (www.available()) {
                char c = www.read();
                stream.print(c);
                lastRead = millis();
            }
        }
    }
    
  • The second problem is that you want to send an email. One you have a connection, it doesnt matter if it is wifi or wired. The above recommended libraries (SMTPClient, FullIP) will directly connect to your provider (e.g. gmail) and will request your gmail password, something that is not secure over non secure connections (and those libraries do not seem to provide secure layer), so personally I am not comfortable with that option. I have been using the temboo service, that allows to send emails, and I have found it to be quite convenient and simple. Furthermore, they also have many other services. The service stores your gmail or whatever provider credentials, and you access the service via a Secret Key and App Password they provide. While this keys might not be fully secure to be sent via non secure layers, at least it only compromises your app in the cloud application, which you might simply manage, and not your full email account. (Disclaimer: I have no relation with temboo, and there might be other similar services for this task, e.g. Carriots, I simply say that I have used them with success for this task and feel it is relatively simple). Your code using temboo, could be something like:

    TembooChoreo choreo(client); // your WiFiClient object
    
    // Set Temboo account credentials
    choreo.setAccountName(temboo_account);
    choreo.setAppKeyName(temboo_app);
    choreo.setAppKey(temboo_app_key);
    
    // credential is the name of your gmail profile in temboo
    choreo.setCredential(credential);
    choreo.addInput("MessageBody", body);
    choreo.addInput("Subject", subject);
    choreo.addInput("ToAddress", address);
    
    //Identify the Choreo to run
    choreo.setChoreo("/Library/Google/Gmail/SendEmail");
    
    choreo.run();
    choreo.close();
    
  • Cheers for that! You and @Ignacio Vazquez-Abrams Helped me ALOT! really! But, if i use SMTP/FUllIp instead of those more secures connections, would it be easier/simple to code? My project doesn't need security because it's just an automation for myself and i'll create a mailing email for that – user3504192 Apr 9 '14 at 1:41
  • If you don't need that security, and you are fine with just sending emails with a dedicated email account, the SMTPClient is indeed very simple to code, as you can see in the README of github.com/gregington/SMTPClient, I would say difficulty of code and setup is the same, but the SMTPClient will not require to configure your temboo service, which is also easy but will take you some time more. So you can stick with the SMTP, while considering external services as temboo for more complicated tasks, for example if your service (twiter, dropbox?) requires secure or more complex connection – drodri Apr 9 '14 at 14:02
1

There may be a problem with sending Email from Arduino, but not where you are expecting it. SMTP protocol is super simple, even Arduino can handle it. But you will need SMTP server somewhere that will talk to your Arduino. SSL connections (as on Gmail) are out of consideration (code to handle the encryption would be too much for Arduino) so you'd have to find mail provider that provides plain SMTP - and there aren't many. If you will set up one yourself, say on your home PC with static IP address, it would be an invitation for spammers and a guaranteed way to get your IP blacklisted by Spamhaus. My advice would be to have Python, PHP or Perl script hosted somewhere that will handle HTTP POST requests from your Arduino and do Email sending. It is safer and simpler.

  • Actually many ISPs (here in the US at least) provide non-SSL mail servers that only allow incoming connections from their customers' IP ranges; if your Arduino is going to be on a fixed connection (e.g. your home WiFi), you could use the ISP's SMTP server. – Doktor J Nov 27 '16 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.