hello,
i'm new to the forum - and quite new to processing.
i'm working my way through tom igoe's 'making things talk' book.
right now i'm at the 'networked cat' project.
in the book, when the cat get's on the mat, processing starts a http GET request, and a php script sends out a mail using mail().
my provider requires to 'pre auth' before sending a mail, which isn't supported by mail(). i could install PEAR which would provide a function supporting 'pre auth' - but 1st, this looks complex as i'm new to php, too, and 2nd, i want to find a solution using processing.
i am able to send a message via terminal and telnet, using pre auth. this communication i want to replicate using processing.
tom provides a script for the http GET request ( makingthingstalk.com/chapter3/44/#more-44 ) which i modified for my script.
i use a 'for loop' for sending out the commands for the mail server. after every command i want to wait for the answer to send the next one.
but, it always send out only the first command 'EHLO server4'in the script below i replaced my personal data, such as username, and password, but you should be able to replicate the 'bug' by just copy and paste the script into processing.
Code:
/*
project: send mail preauth script
HTTP sender
language: processing
uses the processing net library to send a mail
*/
import processing.net.*; // gives you access to the net library
Client client; // a new net client
boolean requestInProgress; // whether a net requet is in progress
String responseString = ""; // string of test received by client
// boolean mailSent;
String hello = "EHLO server4";
String auth = "AUTH LOGIN";
String user = "username"; // username encrypted in base64
String pass = "password="; // password encrypted in base64
String from = "mail from: me@example.com"; // sender email address
String to = "rcpt to: someoneelse@example.com"; // recipient email address
String data = "data";
String subject = "mail subject";
String message= "mail message";
String content = "subject: " + subject + "\n" + message + "\r\n.\r\n";
String quit = "quit";
String conversation[] = {hello, auth, user, pass, from, to, data, content, quit};
// - - - - V O I D S E T U P - - - -
void setup() {
// check if array is assembled as wanted
for (int i = 0; i < 9; i++) {
println("test: " + conversation[i]);
}
// later the applet should stop after mail is sent
//mailSent = false;
//open a connection to the host:
client = new Client(this, "server4.domaincamp.de", 25);
requestInProgress = true;
}
// - - - - V O I D D R A W - - - -
void draw() {
// if (mailSent == false) {
waitForResponse1();
for (int i = 0; i < 9; i++) {
if (requestInProgress == false) {
println(conversation[i]);
client.write(conversation[i] + "\r\n");
// note that you've got a request in progress:
requestInProgress = true;
waitForResponse2();
}
}
}
// - - - - O T HE R V O I D S - - - -
void waitForResponse1 () {
// println(mailSent + " before");
//available() returns how many bytes have been received by the client:
while (client.available() > 0) {
// read a byte, convert it to a character and add it to the string:
responseString += char(client.read());
// add to a line of |'s on the screen (crude progress bar):
print("|");
// println(responseString);
}
// if there's no byte available, either the response hasn't started yet,
// or it's done:
if (client.available() == 0) {
// if responseString is longer thAn 0 bytes, the response has started:
if (responseString.length() > 0) {
// you've got some bytes, but now ther's no more to read. stop:
if (requestInProgress == true) {
// print the response:
println("\n"+ responseString);
// note that the request is over:
requestInProgress = false;
// reset the string for future requests:
responseString = "";
println("end of waitForResponse_1");
// println("mailSent_1 == " + mailSent);
println("requestInProgress_1 == " + requestInProgress);
}
}
}
}
void waitForResponse2 () {
// println(mailSent + " before");
//available() returns how many bytes have been received by the client:
while (client.available() > 0) {
// read a byte, convert it to a character and add it to the string:
responseString += char(client.read());
// add to a line of |'s on the screen (crude progress bar):
print("|");
// println(responseString);
}
// if there's no byte available, either the response hasn't started yet,
// or it's done:
if (client.available() == 0) {
// if responseString is longer thAn 0 bytes, the response has started:
if (responseString.length() > 0) {
// you've got some bytes, but now ther's no more to read. stop:
if (requestInProgress == true) {
// print the response:
println("\n"+ responseString);
// note that the request is over:
requestInProgress = false;
// reset the string for future requests:
responseString = "";
println("end of waitForResponse_2");
// println("mailSent_2 == " + mailSent);
println("requestInProgress_2 == " + requestInProgress);
}
}
}
}
i duplicated 'waitForResponse()' to see that only the first on, 'waitForResponse1(), is called.
i tried to change 'if()-else()' against 'while()-if()', but that doesn't make a difference.
i may should use 'return()' for the 'for loop' to wait for, but i don't know if this would be really a better approach.
any help?
many thanks,
kai