Search code examples
arduinosmtpgmailemail-clientstarttls

I got an error 530 5.7.0 from gmail server while trying to send an email from Arduino and Ethernet shield


I got an error 530 5.7.0 from gmail server while trying to send an email from Arduino and Ethernet shield.

I want to send an e-mail from my arduino uno board. I bought an Ethernet shield for it. I found a code to send an e-mail using Ethernet shield below but I failed. Gmail server said "530 5.7.0" error code but I don't know what it means. What did I wrong?

/*
   Email client sketch for IDE v1.0.1 and w5100/w5200
   Posted December 2012 by SurferTim
*/

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

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
// change network settings to yours
IPAddress ip( 10, 6, 0, 248 );    
IPAddress gateway( 10, 6, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "smtp.gmail.com";

EthernetClient client;

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

void loop()
{
  byte inChar;

  inChar = Serial.read();

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

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

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

  if(!eRcv()) return 0;
  Serial.println(F("Sending helo"));

// change to your public ip
  client.println(F("helo 1.2.3.4"));

  if(!eRcv()) return 0;
  Serial.println(F("Sending From"));

// change to your email address (sender)
  client.println(F("MAIL From: <[email protected]>"));

  if(!eRcv()) return 0;

// change to recipient address
  Serial.println(F("Sending To"));
  client.println(F("RCPT To: <[email protected]>"));

  if(!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  client.println(F("DATA"));

  if(!eRcv()) return 0;

  Serial.println(F("Sending email"));

// change to recipient address
  client.println(F("To: You <[email protected]>"));

// change to your address
  client.println(F("From: Me <[email protected]>"));

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

  client.println(F("This is from my Arduino!"));

  client.println(F("."));

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  client.println(F("QUIT"));

  if(!eRcv()) return 0;

  client.stop();

  Serial.println(F("disconnected"));

  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while(!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }

  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;
  int loopCount = 0;

  client.println(F("QUIT"));

  while(!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return;
    }
  }

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

  client.stop();

  Serial.println(F("disconnected"));
}

Solution

  • There are some ways to send email with embedded system such as arduino. And your sketch shows how to send email directly to mail server which has an account for the destination address.

    As you already know that error code says "Must issue a STARTTLS command first". It means SSL connection is required. Of course, it's a one of restrictions from Google. Unfortunately, "Arduino Ethernet Shield" does NOT support SSL. So you may want to check another shield which supports SSL.

    "PHPoC Shield for Arduino" can be an alternative. To use this shield, "Arduino Library for PHPoC" is required. This library provides some examples. And "EmailClient" example can be a what are you looking for. enter image description here

    /* arduino email client - send email to server directly */
    
    #include "SPI.h"
    #include "Phpoc.h"
    
    PhpocEmail email;
    
    void setup() {
      Serial.begin(9600);
      while(!Serial)
        ;
    
    Phpoc.begin(PF_LOG_SPI | PF_LOG_NET | PF_LOG_APP);
    //Phpoc.begin();
    
    Serial.println("Sending email to server directly");
    
    // setup From/To/Subject
    email.setFrom("from_email_address", "from_user_name");
    email.setTo("to_email_address", "to_user_name");  
    email.setSubject("Mail from PHPoC Shield for Arduino");
    
    // write email message
    email.beginMessage();
    email.println("Hello, world!");
    email.println("I am PHPoC Shield for Arduino");
    email.println("Good bye");
    email.endMessage();
    
    // send email
    if(email.send() > 0)
      Serial.println("Email send ok");
    else
      Serial.println("Email send failed");
    }
    
    void loop() {
    }
    

    I run this sketch for your reference. Of course, I changed some parameters such as from_email_address, from_user_name, to_email_address and to_user_name. enter image description here

    But it's failed to send the email with 421-4.7.0 error code. enter image description here

    According to the error message, this email may be rejected because of the spam policy on google mail server. Anyway, your problem - got an error 530 5.7.0 from gmail server - can be resolved with this PHPoC Shield for Arduino.

    If you still want to send email to google, the below reference will be helpful to you.