Getting my program to email me when it encounters a new problem.

edited September 2015 in Library Questions

Hi! What I have below is a program I've been working on that draws from two massive text documents of questions and answers to give users access to my musings on things in an impersonal, database format. Everything works up until void sendMail, which is a bit of broken code from another user that I hope can be stitched back together to address my problem. What I want it to do is send me an email every time it encounters a question that I haven't answered yet, so I can add it and its answer to my text documents. What I don't know is how to set the message text to be the question that my user asked... Would I somehow convert the "cur" or "to_match" to a text string and then set that text string as the body of the automated email? It's quite over my head, and any advice would be much appreciated! The email-sending code is from http://shiffman.net/2007/11/13/e-mail-processing/

String[] to_match = {
  "HELLO?",
  "WHO ARE YOU?",
  "WHERE AM I?",
  "OK?"
};
String[] resps = {
  "Hello!", //"HELLO?",
  "My name is Jack Miller.", // "WHO ARE YOU?",
  "You are here.", //"WHERE AM I?",
  "It will all be ok." //"OK?"
};

String top = "Ask me anything.";
String cur = "";
final int QUERY_LIMIT = 35;

void setup() {
  size(1200, 500);
  textSize(20);
  fill(0);
  PFont jackFont;
  jackFont = loadFont("CourierNewPSMT-48.vlw");
  textFont(jackFont);
  to_match = loadStrings("to_match_file.txt");
  resps = loadStrings("resps_file.txt");
}

void draw() {
  background(255);
  fill(0);
  text(top, 20,20, 1200,500);
  fill(225, 0, 0);
  text("> "+ cur+((millis()%1000)<500?(" "):("_")), 20,440, 1200,400);
}

void keyPressed() {
  if (key>='a'&&key<='z') {
    if (cur.length()<=QUERY_LIMIT) cur =  cur + char(key+'A'-'a');
  }
  if (key>='A'&&key<='Z') {
    if (cur.length()<=QUERY_LIMIT) cur =  cur + char(key);
  }
  if (key==' '&&(cur.length()<=QUERY_LIMIT)) cur=cur+' ';
  if (key=='?'&&(cur.length()<=QUERY_LIMIT)) cur=cur+'?';
  if (key==DELETE||key==BACKSPACE) {
    if (cur.length()>0) cur=cur.substring(0, cur.length()-1);
  }
  if (key==ENTER||key==RETURN) {
    if(cur.charAt(cur.length()-1)!='?'){
      top = "Please ask a QUESTION.";
    } else {
      top = "I don't know. Ask something else.";
      sendMail();
    }
    for(int i=0; i < to_match.length; i++){
      if(cur.equals(to_match[i])){
        top=resps[i];
      }
    }
    cur="";
  }
}
void sendMail() {
  // Create a session
  String host="smtp.gmail.com";
  Properties props=new Properties();
  Properties props = System.getProperties();
  props.put("mail.pop3.host", "pop.gmail.com");

  // SMTP Session
  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "25");
  props.put("mail.smtp.auth", "true");
  // We need TTLS, which gmail requires
  props.put("mail.smtp.starttls.enable","true");

  // Create a session
  Session session = Session.getDefaultInstance(props, new Auth());

  try
  {
    // Make a new message
    MimeMessage message = new MimeMessage(session);

    // Who is this message from
    message.setFrom(new InternetAddress("jackstarcat@gmail.com", "Ask Me Anything"));

    // Who is this message to (we could do fancier things like make a list or add CC's)
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("jackstarcat@gmail.com", false));

    // Subject and body
    message.setSubject("Quick Question:");
    message.setText("????????????????????????????????????????????????????????????");

    // We can do more here, set the date, the headers, etc.
    Transport.send(message);
    println("Mail sent!");
  }
  catch(Exception e)
  {
    e.printStackTrace();
  }

}
Tagged:

Answers

  • Sigh. You really did need to learn some concepts. Sorry for giving you so much free code earlier - you've not learned enough about programming to be able to do this easily.

    Let's go back to basics.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
    }
    

    This is an empty sketch of blackness. It has two functions, setup() and draw(), which are special to Processing. When the sketch starts, the code in setup() is run once. Then what is drawn in the sketch window is constantly updated by running the draw() function over and over again, forever.

    In the above example, setup() calls another function, size(), and passes it two parameters. There are used as the width and height of the sketch.

    Also in the above example, draw() calls a function called background(), with one parameter. This is then used by background() as a grey-scale color value, resulting in a black sketch window.

  • Now let's change something.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(255);
    }
    

    Now I have changed the parameter passed to background(). Now the sketch is drawing a white background. The important concept here is that the value is passed to the function. The function needs this value to know what to do.

  • Now we're going to write our own function.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
      my_first_function();
    }
    
    void my_first_function(){
      text("I called a function!", 20, 20);
    }
    

    Here we have gone back to a black background, and we are printing some white text on it in another function, my_first_function(), that draw() is also calling now.

    Notice that this function doesn't need any additional information to do it's job, because it always does the same thing.

  • Now let's pass some information to the function.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
      my_first_function(44);
    }
    
    void my_first_function(int the_number){
      text("Here is your number: " + the_number, 20, 20);
    }
    

    As you can see, the function now takes a parameter, and uses that parameter to know which number it was told to display,

  • You can pass other kinds of information to functions too.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
      SAY_IT("Antici........pation.");
    }
    
    void SAY_IT(String a_string){
      text(a_string, 20, 20);
    }
    

    Here we see a new function that prints exactly the string it was told to print.

  • edited September 2015

    Of course, if the string to say is a global variable, there's no need to pass it in with a function call.

    String  global_string = "I do the thing.";
    
    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
      do_the_thing();
    }
    
    void do_the_thing(){
      text(global_string, 20, 20);
    }
    

    Now that you know all this, it should be OBVIOUS that to use the String cur as the text you want to send, simply pass the cur variable to the function that sets the text on line 95. There is no converting needed - your cur variable is ALREADY a String.

  • Also, as you are now loading the contents of your String arrays from files, there is no point in assigning them any values at the global level. Fix that too.

  • Fair enough. I wish I'd shown what code I already had before you helped me with my first few problems that required a bit of inventiveness: I'm not a TOTAL noob, I just have trouble with some cause-and-effect logic functions involving multiple variables... And this SMTP business which I've never had to deal with and which is what I really need help with. Thanks for helping me see the obvious in regards to using cur as the text for my outgoing emails, and for putting that refreshing basic stuff up concisely.

Sign In or Register to comment.