Email Send Error

I would set up my own email server if I were you. Easy enough with Linux. Set the firewall to allow only your stuff to connect.

Here is the bad news from gmail:

530 5.7.0 Must issue a STARTTLS command first. nt7sm7991698pbb.33

You cannot start TLS. Don't shoot me. I'm just the piano player!!

BTW: I loved Costa Rica. So did the entire crew (41' trimaran). I spent 3 months in Puntarenas in 1985 while on my way to Florida from Ventura, California. Panama was great also. The Panama Canal is one of the 7 wonders of the Modern World.

Maybe someday I'll stop in for a visit? :slight_smile:

One never shoots piano planers or sax players, maybe an occasional singer though (at least according to Simon).
Yup, maybe it's time set up another little server here at home. I did find that Chrome does a nice job of capturing the full "Post" text. I'll update on how that goes.

Stop by Ojochal sometime, dinner on us. It's between Dominical and Palmar on the Southern Pacific side. Panama has advantages too.

Pura Vida

Hi, I use this code, and I have a problem.

here is Arduino code :

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
  0x1C, 0xA8, 0x55, 0x34, 0x5E, 0x1D };  //this adress i manually set in router
IPAddress ip( 192, 168, 1, 20 );     //and this
IPAddress gateway( 192, 168, 1, 1 ); //adress to my router
IPAddress subnet( 255, 255, 255, 0 ); //my subnetmask

// gmail.com email server
IPAddress server( 173, 196, 69, 27 ); //this info 
int x=5000;
int y=20000;

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  delay(x);
  Serial.println("Ready. Press 'e' to send");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Email sent");
    else Serial.println("Email failed");
  }
}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;

  if (client.connect(server,25)) {
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

  // change this ip to your public ip
  client.write("helo  9*.***.**.**\r\n"); //here I change to my public IP

  if(!eRcv()) return 0;

  // change this
  client.write("MAIL From: <m****.s********@gmail.com>\r\n");

  if(!eRcv()) return 0;

  // change this  
  client.write("RCPT To: <milan.samardzic@gmail.com>\r\n");

  if(!eRcv()) return 0;

  client.write("DATA\r\n");

  if(!eRcv()) return 0;

  //change this
  client.write("To: You <m****.s********@gmail.com>\r\n");

  // change this
  client.write("From: Me <m****.s********@gmail.com>\r\n");

  client.write("Subject: Arduino email test\r\n");

  client.write("This is from my Arduino!\r\n");

  client.write(".\r\n");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("disconnected");
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(x);

  respCode = client.peek();

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}

void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println("disconnected");
}

Using nslookup I get this information about gmail.

" Non-authoritative answer: " is that problem?

> set query=any
> gmail.com
Server:  UnKnown
Address:  192.168.1.1

Non-authoritative answer:
gmail.com       text =

        "v=spf1 redirect=_spf.google.com"
gmail.com       MX preference = 5, mail exchanger = gmail-smtp-in.l.google.com
gmail.com       MX preference = 10, mail exchanger = alt1.gmail-smtp-in.l.google
.com
gmail.com       MX preference = 20, mail exchanger = alt2.gmail-smtp-in.l.google
.com
gmail.com       MX preference = 30, mail exchanger = alt3.gmail-smtp-in.l.google
.com
gmail.com       MX preference = 40, mail exchanger = alt4.gmail-smtp-in.l.google
.com
gmail.com       internet address = 173.194.39.181
gmail.com       internet address = 173.194.39.182
gmail.com       nameserver = ns3.google.com
gmail.com       nameserver = ns4.google.com
gmail.com       nameserver = ns1.google.com
gmail.com       nameserver = ns2.google.com

gmail.com       nameserver = ns3.google.com
gmail.com       nameserver = ns4.google.com
gmail.com       nameserver = ns2.google.com
gmail.com       nameserver = ns1.google.com
gmail-smtp-in.l.google.com      internet address = 173.194.69.26
gmail-smtp-in.l.google.com      AAAA IPv6 address = 2a00:1450:4008:c01::1a
alt1.gmail-smtp-in.l.google.com internet address = 74.125.25.27
alt1.gmail-smtp-in.l.google.com AAAA IPv6 address = 2a00:1450:4008:c01::1a
alt2.gmail-smtp-in.l.google.com internet address = 173.194.64.27
alt2.gmail-smtp-in.l.google.com AAAA IPv6 address = 2a00:1450:4008:c01::1b
alt3.gmail-smtp-in.l.google.com internet address = 74.125.193.26

And finally i get this on SerialPort

Ready. Press 'e' to send 

//after press e

connection failed
Email failed

Some solution?
Thanks !

I forget to say: ArduinoUno R3, Eternet ShieldW500

This is not the same...

gmail-smtp-in.l.google.com internet address = 173.194.69.26

...as this.

// gmail.com email server
IPAddress server( 173, 196, 69, 27 ); //this info

I try bouth adress, didn't work :-/

I used this ip, and my email was sent ok.

IPAddress server( 173, 194, 69, 26 );

Hi,

Sorry to open up an old topic.

I have to tried this, but, just by reading the code, only the first post have the part where we have to insert username and password. Were they have to be decoded?
Which code are the correct ones?
if we do not use username and password, how does it actually authenticate if I am the right sender?

Thanks.

There is no "right sender" when sending email like an email server sends to another email server. No username or password required. That is how my sketch code works. However, if your IP is not a commercial static IP, the receiving email server may not accept your email. My code will also tell you if that happens.

Email servers using a port other than port 25 use usernames and passwords to insure you are a customer of the ISP that hosts the server and not a spammer.

Thanks Surfer,

I had successfully send out email using my paid ISP email. but, failed to send via gmail.
Since I will be doing more coding and implementation, I decided to use web service to do the other bits as the Arduino will be too "small" to handle all the jobs.

Hello, I've been messing around with this code for a couple of days now and can't seem to make a connection with the server, I constantly get:

connection failed
Email failed

here's the code I've copied from your posts:
what do you think the connection problems could be? I do see the link light on when I plug the ethernet cable into the shield. I've pinged smtp.gmail.com for an ip and it still doesn't connect.

Thanks!

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x26, 0x38 };  
IPAddress ip( 192, 168, 1, 21 );    
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// gmail.com email server
IPAddress server( 173, 194, 69, 26 );//( 173, 194, 69, 26 )

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  delay(10000);
  Serial.println("Ready. Press 'e' to send");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println("Email sent");
      else Serial.println("Email failed");
  }
}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  
  if (client.connect(server,25)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

// change this ip to your public ip
  client.write("helo 67.231.68.89\r\n");

  if(!eRcv()) return 0;

// change this
  client.write("MAIL From: <mphgardner@gmail.com>\r\n");
  
  if(!eRcv()) return 0;

// change this  
  client.write("RCPT To: <mphgardner@gmail.com>\r\n");

  if(!eRcv()) return 0;

  client.write("DATA\r\n");

  if(!eRcv()) return 0;

//change this
  client.write("To: You <mphgardner@gmail.com>\r\n");

// change this
  client.write("From: Me <mphgardner@gmail.com>\r\n");
  
  client.write("Subject: Arduino email test\r\n");
  
  client.write("This is from my Arduino!\r\n");
  
  client.write(".\r\n");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("disconnected");
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  
  while(!client.available()) delay(1);

  respCode = client.peek();

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }
  
  return 1;
}

void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println("disconnected");
}

That server IP is no longer valid for Gmail.

// change this
IPAddress server( 173, 194, 69, 26 );//( 173, 194, 69, 26 )
// to this
IPAddress server( 173, 194, 64, 108 );

You really should use a domain name (DNS) to determine the IP address. Use smtp.gmail.com as the server.

hai i tried that code you given i am getting error reply like this..

"Ready. Press 'e' to send
start connecting
connected
220 mx.google.com ESMTP 9si17802234wjz.20 - gsmtp
250 mx.google.com at your service
250 2.1.0 OK 9si17802234wjz.20 - gsmtp
250 2.1.5 OK 9si17802234wjz.20 - gsmtp
354 Go ahead 9si17802234wjz.20 - gsmtp
550-5.7.1 [115.118.170.24] The IP you're using to send mail is not authorized to
550-5.7.1 send email directly to our servers. Please use the SMTP relay at your
550-5.7.1 service provider instead. Learn more at
550 5.7.1 'The IP you're using to send email is not authorized...' - Gmail Help 9si17802234wjz.20 - gsmtp"

i am try to sending mail from one gmail to another gmail account..

pls help me to solve this..

The IP you are using is not authorized to send email to a gmail server. That is normally the response you get if you do not have a commercial internet account and a static IP.

I'm fairly new to this however I managed to successfully implement your script successfully. However I had to make one small change. I had to change the "if(respCode >='4')" to ""if(respCode <'4')". My respCode is always = 255 and I couldn't connect without making the change. I'm using this script on a Mega 2560 with a Sparkfun CC3000 and an authenticated server. Can you explain to me what the respCode is for so I can correctly change my code. Many thanks.

I'd give up on SMTP. Some MX will let you through, most won't mainly because of the authentications, SSL and what not.

However, I can consistently send mail from arduino via php on my web server using the mail() function.

This should not work. respCode should be the first character of the server response. Any number returned that starts with 4 or larger is an error. Any response starting with 3 or smaller is ok.

I had to change the "if(respCode >='4')" to ""if(respCode <'4')".

Here are the server response codes. The respCode variable will contain the first character of the 3 digit response code.

However, I can consistently send mail from arduino via php on my web server using the mail() function.

Then you should install an email server on that computer also. Sendmail and Postfix are pretty easy to set up.

I'm recommending using sendmail. I have a web server with sendmail, no need to install. SMTP isn't going to work consistently on the arduino for everybody.

SurferTim:
This should not work. respCode should be the first character of the server response. Any number returned that starts with 4 or larger is an error. Any response starting with 3 or smaller is ok.
Here are the server response codes. The respCode variable will contain the first character of the 3 digit response code.
List of HTTP status codes - Wikipedia
Then you should install an email server on that computer also. Sendmail and Postfix are pretty easy to set up.

Thanks for the explanation. I've since found that the CC3000 currently does not have peek functionality. Until I find an alternative I just won't be able to check the status codes. I'll also check out mistergreens recommendation. Thanks for the help.

Tuliptromper:
Thanks for the explanation. I've since found that the CC3000 currently does not have peek functionality. Until I find an alternative I just won't be able to check the status codes. I'll also check out mistergreens recommendation. Thanks for the help.

Here's my code if you need it. I'm using POST, you can use GET with modification.

arduino_mail.php

<?php

$to = $_POST['to'];
$subject = $_POST['subject'];
$txt = $_POST['txt'];
$key = trim($_POST['key']);

$headers = "From: from@email.com". "\r\n";
//don't let mail function open for spam without key
if($key == "secretpasskey") {

	if(mail($to,$subject,$txt,$headers)) {
		echo "mail sent\n";
	} else {
		echo "can't send mail\n";
	}
	
} else {
	echo "wrong key: " . $key;	
}

?>

Arduino code

if (client.connect("www.server.com", 80)) {
        
        
        // Make a HTTP request:
        char emailString[200];
        char key[] = "secretpasskey";
        sprintf(emailString, "to=%s&subject=%s&txt=%s&key=%s&", toEmail, in_subject, in_message, key);
        
        client.print(F("POST /arduino_script/arduino_mail.php"));
      
        client.println(F(" HTTP/1.1"));
        client.println(F("Host: www.server.com"));
        client.println(F("Content-Type: application/x-www-form-urlencoded"));
        client.print(F("Content-Length: "));
        client.println(strlen(emailString));
        client.println(F("Connection: close"));
        client.println();
        client.println(emailString);
        client.stop();
        Serial.println("disconnect");
        
    }
    else {
        // didn't get a connection to the server:
        Serial.println("connection failed");
        client.stop();
    }