Ethernet Email Attachment


hi,
    attempting send email containing attachment ethernet shield.
i can send email no problem, surfertim, having issue attachment.
i have tried several ways , read many posts.
however, cannot find complete solution.
i can blank file send , file canned text in send, not file on sd card.
any assistance appreciated.

the code use below. removed email addresses, passwords, etc.

code: [select]
/*
   email client sketch ide v1.0.5 , w5100/w5200
   posted 7 may 2015 surfertim
   encoding routines razorblade
   https://www.base64encode.org/ encoder site
   http://forum.arduino.cc/index.php?topic=296897.0 surfertim's addition thread
   http://forum.arduino.cc/index.php?topic=67701.30 encoding thread

*/
#include <sd.h>
#include <spi.h>
#include <ethernetv2_0.h> //library seeed ethernet sheild version 2.0

#define w5200_cs  10 //denotes pin 10 w5200 pin
#define sdcard_cs 4  //denotes pin 4 sd card pin

file sendfile;
 
static const char cb64[]="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";

//mac must unique
byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
//change network settings yours**************************
ipaddress ip( 0, 0, 0, 0 );   
ipaddress gateway( 0, 0, 0, 0 );
ipaddress subnet( 0, 0, 0, 0 );

char server[] = "mail.smtp2go.com";
int port = 80;
ethernetclient client;

void setup()
{
  serial.begin(115200);
  ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
//initialize sd card
  serial.print("initializing sd card...");
  pinmode(w5200_cs, output);
  digitalwrite(w5200_cs,high);
  pinmode(sdcard_cs,output);
  if (!sd.begin(sdcard_cs)) {
    serial.println("initialization failed!");
    return;
  }
  serial.println("initialization successful");
  serial.println(f("ready. press 'e' 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,port) == 1){
    serial.println(f("connected"));
  }
  else {
    serial.println(f("connection failed"));
    return 0;
  }
  if(!ercv()) return 0;

  serial.println(f("sending hello"));
//replace 1.2.3.4 arduino's ip**************************
  client.println("ehlo 1.2.3.4");
  if(!ercv()) return 0;

  serial.println(f("sending authorizes login"));
  client.println("auth login");
  if(!ercv()) return 0;

  serial.println(f("sending user"));
//change base64 encoded user**************************
  client.println("myusername");
  if(!ercv()) return 0;

  serial.println(f("sending password"));
//change base64 encoded password**************************
  client.println("mypassword");
  if(!ercv()) return 0;

//change email address (sender)**************************
  serial.println(f("sending from"));
  client.println("mail from: <sender@gmail.com>");
  if(!ercv()) return 0;

//change recipient address**************************
  serial.println(f("sending to"));
  client.println("rcpt to: <reciever@gmail.com>");
  if(!ercv()) return 0;
  serial.println(f("sending data"));
  client.println("data");
  if(!ercv()) return 0;
  serial.println(f("sending email"));

//change recipient address**************************
  client.println("to: <reciever@gmail.com>");

//change address**************************
  client.println("from: me <sender@gmail.com>");
  client.write("subject: arduino email test");



//****************************************************
//start of attach file
  client.write("mime-version: 1.0\r\n");
  client.write("content-type: multipart/mixed; boundary=frontier\r\n\r\n");
  client.write("--frontier\r\n");
  client.write("content-type: text/plain\r\n\r\n");
  client.write("this arduino!\r\n");
  client.write("--frontier\r\n");
  client.write("content-type: text/plain\r\n");
 
//this send file
  sendfile =sd.open("test.txt",file_read);
  client.print("content-type: text/plain; name=\"test.txt\"\r\n");
  client.print("content-transfer-encoding: base64\r\n");
  client.write("content-disposition: attachment; filename=\"test.txt\"\r\n\r\n");
  client.write("\r\n--frontier--\r\n");
  encode();
  sendfile.close();
//end of attach file
//****************************************************



  client.write(".\r\n");
  if(!ercv()) return 0;
  serial.println(f("sending quit"));
  client.println("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 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 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("isconnected"));
}

void encodeblock(unsigned char in[3],unsigned char out[4],int len) {
  out[0]=cb64[in[0]>>2]; out[1]=cb64[((in[0]&0x03)<<4)|((in[1]&0xf0)>>4)];
  out[2]=(unsigned char) (len>1 ? cb64[((in[1]&0x0f)<<2)|((in[2]&0xc0)>>6)] : '=');
  out[3]=(unsigned char) (len>2 ? cb64[in[2]&0x3f] : '=');
}

void encode() {
  unsigned char in[3],out[4];
  int i,len,blocksout=0;
  while (sendfile.available()!=0) {
    len=0;
    (i=0;i<3;i++){
      in[i]=(unsigned char) sendfile.read();
      if (sendfile.available()!=0) len++;
      else in[i]=0;
    }
    if (len){
      encodeblock(in,out,len);
      for(i=0;i<4;i++) client.write(out[i]);
        blocksout++;
    }
    if (blocksout>=19||sendfile.available()==0){
      if (blocksout) client.print("\r\n");  blocksout=0;
    }
  }
}


below in serial monitor

code: [select]
initializing sd card...initialization successful
ready. press 'e' send.
connected
220 mail.smtp2go.com esmtp exim 4.85 thu, 05 nov 2015 08:15:19 +0000
sending hello
250-mail.smtp2go.com hello 1.2.3.4 [1.2.3.4]
250-size 52428800
250-8bitmime
250-pipelining
250-auth cram-md5 plain login
250-starttls
250 help
sending authorizes login
334 vxnlcm5hbwu6
sending user
334 ugfzc3dvcmq6
sending password
235 authentication succeeded
sending from
250 ok
sending to
250 accepted <reciever@gmail.com>
sending data
354 enter message, ending "." on line itself
sending email
250 ok id=1zufhy-nrkl38-jx
sending quit
221 mail.smtp2go.com closing connection
disconnected
email sent



thanks

quote
but having issue attachment.
and issue is?

quote
i can blank file send , file canned text in send, not file on sd card.
the file have contains?
the data sent is?
the e-mail received contains?

inquiring minds want know.


Arduino Forum > Using Arduino > Programming Questions > Ethernet Email Attachment


arduino

Comments