SMTP

20 Nov 2009

Hello,

I am wondering if antyone knows of a way to set up a simple smtp client on an mbed?

It could be used to shoot an email upon some kind of trigger- door open etc..

thanks!

21 Nov 2009 . Edited: 21 Nov 2009

It seems pretty easy to connect to a SMTP server with a socket connection.

Here is an example for the arduino, should be easy to port over to mbed: You do need to know your mail server address if you want this to work

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Mail server address  MODIFY THIS FOR THE TARGET DOMAIN's MAIL SERVER

Client client(server, 25);

void setup()
{
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
 if (client.connect()) {
   Serial.println("connected");
   client.println("EHLO MYSERVER");
   client.println("MAIL FROM:");
   client.println("RCPT TO:");
   client.println("DATA");
   client.println("SUBJECT: This is the subject");
   client.println();
   client.println("This is the body.);
   client.println("This is another line of the body.");
   client.println(".");
   client.println(".");
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }
}

Here's the commands you need to send to the server with the socket connection: http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol#SMTP_transport_example

But if you have a webserver you could make a PHP script which the mbed would call, and then it sends the email. <- That's what I'm doing

The PHP could be as simple as:

<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

21 Nov 2009 . Edited: 21 Nov 2009

Hi Matt,

matt gring wrote:
I am wondering if antyone knows of a way to set up a simple smtp client on an mbed? It could be used to shoot an email upon some kind of trigger- door open etc..

I'd second Vlad's approach of doing an http post to a server that could send the email on your behalf as a first test, but that it because I like http and it keeps things simple :)

The code for a server might look something as simple as (my php is a little rusty!):

<?php mail($_POST["to"], $_POST["subject"], $_POST["body"]); ?>

Then you could just do an HTTPClient post from mbed, and you'd know exactly when the biscuit cupboard door was opened.

Simon

21 Nov 2009 . Edited: 21 Nov 2009

Hey Simon,

I like Vlads approach more. If you unable to change the receiver address, therefore nobody can use the script to send spam.

 

An alternative would be to use an .htaccess file with basic auth and configure the mbed to login.

The client would look like:

 

HTTPClient client;

int main() {
    client.auth("user","password");
    client.post(url, "to=email@to.me&subject=My reminder&body=Door is open");
}


Cheers

 

Rolf

21 Nov 2009

Thank You all!!

09 Feb 2010 . Edited: 09 Feb 2010

Is there a way to use the http.post command and a php script to send something in the email? say a int? or a string?
Nevermind i just read above. i think i get it :)

Re-Edit!

Alright, having a little trouble with just the simple php. I can get a script running, much like the Christmas tree example, just using a simple http.get to call the php script. But when i try to use these variables (no authentication), i'm getting nothing

 

client.post(url, "to=email@to.me&subject=My reminder&body=Door is open");

<?php mail($_POST["to"], $_POST["subject"], $_POST["body"]); ?>


my code is as follows

#include "mbed.h"
#include "HTTPClient.h"

DigitalOut led (LED1);

HTTPClient http;


const char msg[] = "to=gxs.inc@gmail.com&subject=my test 2&body=dynamic test";
const char url2[]  = "myurl.com/test2.php";

int main() {

//second one, php script dynamic

printf("test 2 starting");

http.post(url2 , msg);

while(1){
        led = !led;
        wait(.2);
}

//printf("test 2 finished");

}
results are

 

test 2 starting IP: 192.168.1.66

and a flashing led, no emails