Thursday, January 31, 2013

DBA exam results (not yet)

I have been waiting and waiting for the result of the exam 'Introduction to Business Research 1' to appear - I should have received it six weeks after the exam and that date has already passed. Originally I had intended not to enroll in the second course, "Introduction to Business Research 2 - The literature review", until I had the result but decided otherwise a few days ago. I downloaded the course text today and will start reading in due course.

I wrote to the university asking what had happened and received the following reply (which I have edited slightly): I regret that as you took the exam in Israel, your exam paper has only just been returned to us from the British Council. As you sat in English, your paper will need to be marked by our marker who is not in the office at present (but will be in the office in due course). However, I will ensure that your results are released to you as soon as possible, but cannot guarantee this before the end of next week.

I find it extremely hard to understand why the local British council took so long to send the paper to Edinburgh. As far as I can see, they should have sent the paper within a few days.

Wednesday, January 30, 2013

Notes on 'The Fingertip effect" and ERP

D.N. Perkins introduced in his 1985 paper "The fingertip effect: how information processing technology shapes thinking" the concept of 'The fingertip effect' and defined it as follows.

Technology has a venerable history of putting things at our fingertips to be seized and used widely for their designed objectives as well as for other purposes. We can call this the first order fingertip effect, the difference an innovation straightforwardly makes. But a further question is often asked: 'What difference will it really make? The "really" refers not to the pragmatics of … computing but to deeper and more wide-ranging repercussions on society, personality and thought.

The papers which I have seen referring to the fingertip effect have concentrated on education; one paper (Barzilai and Zohar, 2006) investigates how computers have influenced academic researchers.

I propose to examine the fingertip effect with respect to how people use ERP programs, especially people without prior experience of such programs. In this respect, the first order fingertip effect is simply getting the work done in the way that the worker is used to doing it (or in the manner that the worker thinks appropriate).

If, for example, the worker is working in the Customer Relationship Management (CRM) field, then she will be tasked with recording data about customer contacts: names, telephone numbers, email addresses. She will need to record meetings with those contacts, maintain mailing lists containing those contacts and record attendance at company events. Should this worker be new to ERP, then she will maintain all of the above in an external program, such as a spreadsheet; the information may possibly originate in the ERP program but will be maintained in the external program.

The second order fingertip effect would be changing the worker's manner of thinking by realising that using a relational database (which in this respect is ERP) is better than using a flat database (such as a spreadsheet) and that ERP provides much more powerful tools for manipulating the data. ERP programs by definition are multi-user whereas spreadsheets tend to be single user (and are often stored in locations which prevent multi-user use).

The general second order fingertip effect is 'buying into' the ERP model, especially for the more complicated functions such as MRP, purchasing forecasts, etc. These functions allow one to 'get the work done' (the shop floor knows what it has to produce) as well as
•    allowing the purchasing department to order supplies in an effective and timely manner
•    allowing the distributors to know when orders will be ready to be supplied
•    allowing managers to measure manpower work loads and adjust appropriately
•    allowing maintenance to be scheduled

Perkins' paper is concerned with students (primarily in secondary education), with a specific series of goals and rewards, whereas I am concerned with adults working in a business environment. I doubt very much whether many employees consider themselves to be in a learning environment. As such, the questions which Perkins poses and answers ("Is the opportunity really there?", "Do learners recognise the opportunity?", "Are learners sufficiently motivated to take the opportunity?") are not really relevant.

In summary, it would seem that there are two types of workers – those that are interested in getting the job done (by any means, even if this means using tools which are not recommended) and those that are interested in getting the job done in the most efficient manner and in changing the nature of the job so that it will be both more powerful and more simple.

Monday, January 28, 2013

A productive Saturday

Despite having a CPAP machine, I haven't been sleeping very well lately, so it was a pleasure to have an excellent night's sleep on Friday night. I awoke on Saturday with an energy which has long been absent and shortly put that energy to good use.

The Occupational Therapist wanted me to add a field to a screen; when I enquired further, it transpired that what she needed was a new table and three screens to accompany it. No problem.

Then I cooked a delicious lunch of grilled St Peter's Fish fillets along with mashed potatoes and green beans.

In the afternoon, I recorded the vocals to the Van der Graaf Generator song, "Pilgrims", which I had sequenced several months ago but never completed. At first, I had found it too hard to sing, and afterwards, for a few months, I couldn't sing at all. I wouldn't say that it was easy to record the vocals to this song (it lasts for ten and a half minutes, and there is singing for almost ten minutes) but at least I was able to perform this time. I discovered that it was easier to sing the verses first and then the choruses; I would sing a part, check and correct it, and then move on to the next part.

In the evening, I was able for the first time to burn a complete cd of my VdGG covers; listening to it on the stereo in the living room was an interesting experience. I discovered that the bass was too loud on one track as were the drums in one section of 'Pilgrims', whereas my voice sounded slightly strange and needed to be louder here and there.

Yesterday I did some remixing although I have yet to burn another disc. I also discovered a new trick in the pitch correcting program that I use. Until now, I have only been able to correct pitch in the gross sense: for example, if I were singing a C instead of C#, I could move the note to its correct pitch. But there was another problem which I hadn't been able to solve: vibrato (or to give it a non-technical name, wobble). 

If the frequency of concert A is 440 Hz, then singing off-pitch means that one sings at 420 Hz. But vibrato means that within the course of the note, the frequency varies between 420 and 460 Hz; the wider the variation, the wider the vibrato. Choir singers aim for an absence of vibrato whereas solo singers (especially soul singers) aim for a certain amount of vibrato; too little sounds sterile whereas too much sounds like singing off key and uncontrolled.

Yesterday I discovered how I can reduce the amount of vibrato in my singing (or more correctly, in my recorded singing) in order to produce a more controlled and smooth sound, so I spent some time improving the more egregious parts of the 'Pilgrims' vocal, especially in the first part.

Apropos CPAP,  I should point out that I have an appointment with the 'sleep doctor' in a few weeks, after which I will probably have a new machine and certainly a new mask.

Wednesday, January 23, 2013

The simplest ideas are normally the best

The manager of one of the factories in my company asked me several months ago whether it was possible to print the production drawings of parts whilst printing work orders for those same parts. I replied that it would be no problem to do this in principle, but for in order for this to work, the drawings have to be connected somehow to the parts.

Unfortunately, many of the parts produced in this factory are ad hoc parts which are created automatically by a configurator program, which builds (defines) new parts on the basis of existing parts. According to our tests, the configurator does not copy to the new part documents which are linked to the existing part. 

I wrote a program which performs this copying (the documents themselves are not copied, only links to them) but hooking this program up to the output of the configurator (which is an extremely unfathomable black box external program to our ERP program) proved to be problematic. It was very difficult to test the program under controlled circumstances and seemed to work only occasionally.

We were discussing this program's short comings the other day when I suggested taking a different approach to the problem. Every part has what the ERP calls a 'base product': a part which is representative of a class of products. I suggested that the documents be linked to these base products and then at the end of every day, a program would run automatically which would copy the documents from the base product to the parts which had been created that day which belonged to the base product (that's complicated to write but the idea is quite simple).

The following day, following another discussion on the subject, it suddenly struck me that we were still taking a complicated path by focusing on copying the links to the documents. What if the program which prints the work orders printed the documents linked to the base products of the parts in the work orders? This way, there is no need to copy anything anywhere. In order to implement this, I needed to add one clause (about six words) to the SQL statement which controls which documents are to be printed.

As always, the fundamental idea in problem solving applies: the best solution will come once the core problem has been identified. I had been concentrating on copying the links when I should have been concentrating on printing the documents, which is a simpler solution by an order of magnitude. The simplest ideas are normally the best.

Monday, January 21, 2013

Another evening (song)

After the forced marches of programming, it's nice to get back to creating some music. Whilst thinking of songs to record, I remembered a song which I had written 39 years ago called 'Another evening' (AE). I had quite liked the song at the time, but it seems to have sunk without causing many ripples in my memory. This may well be because the lyrics weren't really about anything (except teenage angst, which was a strong theme in those days!) and the music was slightly strange.

As opposed to most of my songs, which have strange harmonic sequences but simple and standard structures, AE had a rather unusual structure, at least for me. It was a fairly short song consisting of one verse, one middle bit, a different middle bit and a coda based on the first part (let's call this A B C A'). Most of my songs have a harmonic rate of change of 1 chord per bar, but AE exhibits variable behaviour: there are chords which last for two bars, one bar and even half a bar. This was probably the reason why I haven't touched the song for such a long time.

I sequenced a very bare-bones version of the song as it was originally written and saw that it would last maybe a minute and a half. So I doubled the first part of the song, now making the structure A B A B C A', figuring that I could write new lyrics for the AB part. A few days later I changed the structure again: if A + B = verse, then the new structure becomes the very familiar VVCVCV', or 'two bridge' structure, where the verse between the bridges is instrumental. After adding a few four bar links between sections, the song now runs for about four and a half minutes.

Taking some time off from the music, I sat down yesterday evening to write some new lyrics. This was remarkably easy; I tossed off the lines in about ten minutes, probably because the new verse is structured in the same way as the original verse. It helps that the lyrics aren't really about anything; it's easier to find words that scan and seem to say something as opposed to finding words which scan and have to say something.

Although I had originally baulked at doing so, now I see no reason not to quote my additional words
Another evening casts its shadow on the western land
Another evening plays its shadows on the lonely man
And as the light dissolves into shade
My path is marked in vague tones of grey
No friends to call me, no dog for a walk
I’m hiding from a chance to talk
As the song is fairly quiet, I opted for a bossa nova rhythm and a basic instrumental pad to serve as the basis of the arrangement. From the second verse onwards, there is a vibraphone playing arpeggiated chords and there's a trumpet solo in the middle. I painstakingly recorded a solo on Saturday, but listening to it again last night, I realised that it was too 'busy' and so opted to simplify it greatly.

As tomorrow is Election Day in Israel, we have the day off. I'll probably record some vocals then and complete the song.

[Edit:]
Here is a link to the completed song.

Saturday, January 19, 2013

Successfully sending an email in Hebrew with embedded picture and attachment

This is the final installment in an unplanned series about sending emails in Hebrew, with an embedded picture and an attachment.

For my final programming and debugging session, I thought it prudent to start with an email which I knew had been sent with UTF-8/Base64 encoding - I sent my home address an email from my Gmail address. Looking at this email as text, I could see that the HTML was far simpler than I had been using until now, so I replaced most of my complicated code with the simple code.

Then I considered the fact that the body of my text would have to be encoded as UTF-8. This actually was a very minor problem: instead of writing html.add ('this is a line'), I needed to write html.add (AnsiToUTF8 ('this is a line')). Of course, in real life, it was slightly more complicated as the UTF-8 encoded code wasn't shown properly in the preview form; the program creates encoded strings if sending an email or unencoded strings if previewing.

Even after encoding the text, the emails were arriving without Hebrew text. In a moment of inspiration, I realised that I had to tell the component holding the email that it too was encoded. In other words, instead of writing
 with TIdText.create (email.MessageParts, nil) do
  begin
   body.assign (html);
   contentType:= 'text/html';
  end;
I had to add a line...
 with TIdText.create (email.MessageParts, nil) do
  begin
   body.assign (html);
   contentType:= 'text/html';
   charset:= 'UTF-8';  // added line for encoding
   parentpart:= 1;       // added line for structure
  end;
Now my emails were arriving correctly both at home and at work.

Once I had got this fixed, I turned my attention to the subject of attachments. First, I read the tutorial which I mentioned previously very carefully; the email which I am trying to send has several parts (plain text, html, embedded picture, unconnected attachment) and there has to be an initial, empty, TIdText section to set things up correctly. Needless to say, my code was missing this section.

Once I added the initial TIdText section and added correctly numbered parentparts, my code worked ... most of the time: sometimes it would send attachments and sometimes not. It took a while for me to notice the pattern: text and jpg files were being sent, but zip, doc or pdf files seemed to hang the computer, requiring closing the program forcefully.

Reading about the subject didn't throw up any ideas why one attachment was being sent whereas another was not, but I did notice in someone's code that they were displaying the status of the email component. I thought that I would do this and saw (when I tried sending a pdf file) that the component was encoding the attachment (which took relatively long) and then successfully sending the file.

Although I had hoped that it would be possible to skip encoding the file (the file is already a sequence of bytes), it seems that there is no alternative. It would also seem that the time that the program took to send the email was not in fact connected to the type or size of the attachment, but rather connected to the handshaking which occurs between my client and Gmail. 

During this period, the program is unresponsive; it seems that the only way to make the program responsive whilst sending an email is to hand off the email code to a separate thread. I am somewhat wary of doing so as I have never done any real programming with threads. Also, there is no small amount of material which has to be passed from the program to the thread which complicates matters. It might actually be simpler for the program to spawn a new program whenever the main menu option is chosen; this spawned program can then display all the fields necessary and send the email in its own time without bothering the main program. I'll wait and see how sending emails 'in the field' goes before taking this step.

I have now spent two weeks working on (and documenting) this subject. I have covered in my research a great deal of esoteric material, most of which was unnecessary in retrospect. Sending email is akin to black magic; it's something that 'just happens' (not really dependent on my programming or problem solving skills) and requires in the final analysis boiler-plate code which may or may not be understood. The main thing is that it works and that I don't have to tinker with it again.

After two weeks of almost constant blogging, I now have no idea about what I am going to write about next....


Friday, January 18, 2013

Nic Potter RIP

A terse statement on Peter Hammill's website yesterday informed the world that Nic Potter, bassist on some of VdGG's early records, had died.

A longer statement, made by VdGG website manager Phil Smart: Nic had been ill for some time but was admitted to hospital about 10 days ago with pneumonia which was probably not related to his illness, although of course I am no doctor. I will put more on the website once I've gathered my thoughts... Many of you had noticed his gradual disappearance from the web and email but I was unable to say anything without the go-ahead of Nic's family. An announcement of some sort was being discussed recently but events have overtaken us.

The macabre amongst us will note that the first member of the VdGG 'family' to pass away was Keith Ellis, bassist in the first gigging incarnation on VdGG (1968/9); the second was the second bassist, Potter (1969/70, 1977/8).

The picture of Nic on the left comes from the sleeve of 'The least we can do is wave to each other'; he was about 18 years old at the time of the picture. As it happens, Nic played very sympathetic bass on the song 'The Birds', which I mentioned the other day.

Thursday, January 17, 2013

The birds

Before I became embroiled with the quest of writing an HTML enabled Hebrew mail client, I had some spare time which I devoted to musical pursuits. Still involved in my series of Van der Graaf Generator/Peter Hammill covers, I decided to record a version of "The birds". This is a song which appears on Peter's first solo album, "Fool's mate", coming at the end of a wonderful sequence.

Listening to the album once again in order to refresh my memory, I was struck by how beautiful Hugh Banton's piano playing was on this song; Robert Fripp's guitar work also impressed me but less than it used to (and I've been listening to this record for 40 years). For the first time, I got the impression that Fripp might have added his contribution after the basic track had already been recorded.

I should also point out that whilst Banton's incredible piano work is all over the record (check out "Vision"), he very rarely played the piano in the context of VdGG, leaving that to Hammill. I suspect that his delicate and filigree playing would have been lost in the wider context of the group, which is a shame.

Anyway, "The birds". This song harmonically is very simple, having the same line F/C/Dm/C repeated three or four times during the verse. Banton's playing disguises this simplicity but I would have to suffer it. After working out a brief swung version which didn't get past the demo stage, I started work in earnest with a pad and bass; I found a lovely guitar patch which was playing harmonics and utilised this with one of Fripp's licks. I balanced these harmonics with vibes, which at one stage play a lick from the Doors' "Riders on the storm". A solo was played on a slightly distorted electric guitar; after a few days, I improved the solo and added an arppegiated organ to fill out the background.

Recording the vocal went very quickly, and in no time I had a completed track ready. It's  fairly good, although also fairly basic. It's certainly lighter than some of the other epic tracks which I have recorded, although I deliberately aimed for some light contrast.

Wednesday, January 16, 2013

Relaying the email

I finished my last blog on a cliff-hanger: after getting HTML text, attachments and UTF-8 subject encodings to work properly, I discovered that there was a major problem with my program. As long as mail was being sent to an address with the same Internet provider as the SMTP definition - and the OP and I share an Internet provider - everything was ok, but as soon as I tried to send email to an address outside of the provider's domain, I received an error message about relaying and the mail was not sent.

It transpires that a few years ago, Internet providers tightened up their email protocols to prevent spam mail. I wasn't aware of this and blissfully continued to use code which worked in the past but not now. My first action was to switch providers and send email via Gmail, but this too required some changes. After more feverish googling, I discovered that almost certainly I wouldn't be able to send email via Gmail with Indy 9 (which comes with Delphi 7), but that changing to Indy 10 would solve my problem.

Trying to find a site from which I could download of Indy 10 was amazingly difficult; the Indy project is very coy about the downloads and I had to negotiate several screens before I eventually found what I was looking for. Very strange: I would have thought that they would have placed a link to the latest version on their home page. After downloading the new version and updating Delphi so it would use this new version (a procedure which took about an hour to my further surprise), I eventually got an Indy 10/Gmail demo to work! Hurrah! One 'gotcha' was that two units have to be linked manually into the project and no one told me this explicitly.

But then I had to convert the subject line to UTF-8, the body to HTML and add the attachments. Finally I received an answer to my query about encoding the subject line: the TIDMessage component which exists in both Indy 9 and Indy 10 has an event called 'OnInitializeISO', which exposes two fields, VTransferHeader and VHeaderEncoding. By setting one to 'UTF-8' and the other to 'B', the subject line is automatically encoded as UTF-8/Base64. Unfortunately, there is no help regarding this field, so as Randy Newman sang correctly, "There ain't no book you can read". Fortunately, there was "someone to tell you", which is how I learnt about this. This little tip would have saved me at least three hours of screwing around had I known about it beforehand.

I still haven't got 'non-connected' attachments working correctly but I think that I know what to do. The only problem left - and it's no small problem - is that the email which I sent from home to work arrived correctly but with no Hebrew text contained within. It looks like I'm going to have to encode all of the internal text (what would be the body of the email) to UTF8/Base64 before storing this in the email. This isn't really a problem, more of an annoyance.

As I've been writing the majority of this email series a couple of days after the events described, I always knew when writing how I solved the problems. Now I don't know whether I will solve the two remaining problems easily, but I am optimistic. There is a tutorial but I think that it could be improved; a complete demo program would certainly help. Fingers crossed.

I've been very busy during the day for the past few days which means that I've had to work hard at night on these email problems. One night I dreamt about encoding subject headers and last night I dreamt about setting up web sites for a Chinese girl and having problems with the encoding. Hopefully I'll be able to dream about more regular subjects in the near future.

Tuesday, January 15, 2013

Creating an email with a subject line in Hebrew

There ain't no book you can read
There ain't nobody to tell you
But I don't think I'm getting
What everybody's getting
Maybe I'm doing it wrong
 (Randy Newman, "Maybe I'm doing it wrong")

After having successfully created emails which display embedded pictures, we then discovered that the subject line of those emails was not always displayed. The test emails which I sent to myself displayed correctly (naturally), but the subject was not readable (only question marks) when the OP accessed the mail on her iPhone.

After rooting around on the Internet, I discovered that the subject line can have its own encoding. Whatever the coding, the line cannot contain characters whose value is over 127. It turns out that most email subject lines in Israel are encoded with the Windows 1255 code page, which is not surprising as this the code page for Israel.

For every encoding (such as Windows-1255 or ISO-8859), there are two possibilities: the line can be 'Quotable' or it can be 'Base64' encoded (Base64 encoding is a system which turns translates byte streams (there may be values over 127) into readable characters (A-Z, a-z, 0-9, +, =).


The above picture show the headers in an Email which I was sent. The part which interests me at the moment is the second line, Subject: =?windows-1255?B?Uk.......?=. Ignoring the 'subject:' part, the line consists of three sections: a prefix (=?windows-1255?B?), a suffix (?=) and a payload, which is everything inbetween.

The prefix tells the email client which type of encoding was used; in this case, it is base64 encoding of Windows-1255. The email client of the person who sent me the email encoded the subject into Base64 ; my email client reads the prefix and then decodes the subject. It's not clear to me at the moment why the majority of emails use B encoding as opposed to Q encoding.

Having learnt this, I 'instructed' my email program to send letters encoded as Windows-1255-Q; this requires taking a Hebrew letter and displaying it as the character equivalent in hex (for example: the letter 'aleph' is represented as character 224 in the code page. 224 is E0 in hex, so the encoding would be E0=). The subjects of letters sent like this were correctly decoded by computers running Outlook, but the iPhone remained stubborn. Presumably the iPhone doesn't know how to decode emails sent in windows-1255 format; emails sent from the iPhone are encoded in UTF-8?B format.

In order to send subjects encoded in UTF8?B format, I have to convert the subject line into UTF-8 encoding and then encode it again to Base-64. After no small amount of fiddling about, I discovered that the Delphi system function AnsiToUTF8 performs the first step correctly (at one stage, I assumed that there was a mistake as the Unicode coding for the letter aleph was not the same as the UTF-8 coding; it transpires that by definition they are not the same), but the Base-64 encoding was producing incorrect results.


After pulling out what is left of my hair and ruining the keyboard with a multitude of google queries, I discovered that there was a bug in the Base-64 implementation that I was using, which came from this page (which is the same as this page). The 'codes64' string begins with the digits, then capital letters, then lower case letters and then punctuation. But this page shows that the capital letters should come first, followed by the lower case letters, followed by the digits, followed by the punctuation. Once I had made this correction, the strings began to be encoded 'correctly' - I could read them properly in my email client, which I am taking to be the definition of 'correct'.

Why the epigram from Randy Newman which opens this posting? Because it seems that the information which I needed does not exist in one place; I had to ferret it out piece by piece and deal with misinformation. At the beginning, almost certainly I was doing it wrong; there was no book to read and no one to tell me. Obviously I'm not getting what every other email client is getting, so clearly I'm doing it wrong.

So: we have slain yet another problem in the path to sending Hebrew emails to a mailing list. Unfortunately, there is still another problem waiting in the wings....

Monday, January 14, 2013

Election day duty driver

On my kibbutz, like many others, there exists the function called 'duty driver'. This means that every day during the week, a kibbutz member takes a large car and drives people from the kibbutz to the nearest town then picks people up returning by train or by bus from the town to the kibbutz. The hours are from 5-10pm, after work, and it's a voluntary - meaning unpaid - job. 

In the old days, "everybody used to take a turn at doing everything" - there would be a rota for working on Saturday, a rota for working in the dining room in the evenings, a rota for guard duty, a rota for duty driver, etc. Over the years, most of these rotas have disappeared and the only ones left are duty driver and guard duty. Once men used to do both, but now they do either one or the other. I opted for duty driver as it gives me some sense of social reward (the hours are better too).

In the past few years, more and more kibbutz members have acquired their own form of transport and so the number of people who take advantage of the duty driver has steadily decreased. In turn, the scope of the duty driver has been reduced, meaning that regular journeys are made between 5-7:30pm, after which the driver sits at home and waits for people to telephone.

The last time that a general election was held in Israel was four years ago, on a rainy Tuesday in January. People don't work on the day elections are held, ostensibly to allow them to travel to wherever they have to be in order to vote, but for most people it is just a day off (shops are closed but public transport is available). As it happened, I was duty driver that day: along with the holiday and the rain, I don't think I gave anybody a lift. I simply drove through the empty and wet streets of Bet Shemesh.

There will be a general election again next Tuesday, and although it doesn't look like rain (we had enough last week), once again I will be duty driver on election day. Coincidence or favouritism? What are the chances of being duty driver on consecutive election days?

At first, I thought that it was a strange coincidence (well, all coincidences are strange by definition), but then I realised that the odds of this happening are far from astronomical. As I do this duty once every two months, there are about 45 people on the rota and so the probability of being duty driver on any given day is thus 1 in 45. As there is no connection between the rota from four years ago and the current rota, the two probabilities should be multiplied together meaning that there is a 1 in 2025 chance of being duty driver for two consecutive election days. 

Better odds of being duty driver on election day than winning the national lottery.

Sunday, January 13, 2013

Embedding pictures in HTML emails

Following on from my  post from a few days ago about previewing HTML emails, it seems that I prematurely congratulated myself about including pictures in the email. Whilst I could see the pictures in the emails (not only in the preview), other recipients of the emails could not see the pictures.

This was because the pictures were referenced by the "img src" command which assumes that the picture exists on the computer which is reading the HTML code. This works when one is reading an Internet page with a browser, but doesn't work when reading emails - because the picture is not embedded in the email.

In order to find out what command was necessary to do this, I first sent myself an email from Outlook which contained an embedded picture; I soon discovered that the necessary command is still "img src", but instead of accessing/naming a file, it uses a "cid" (content id). But how does one set this value?

There is a very informative page here, but this mainly discusses Indy 10, and I am (of course) using Indy 9. After asking the question on Stack Overflow, along with a bit of to and fro, I saw what I needed to do.

An HTML letter is built of several sections; one of these sections deals with embedded pictures.
if laPicture.text <> '' then
 with TIdAttachment.Create (email.MessageParts, laPicture.text) do
  begin
   ContentDisposition:= 'inline';
   ContentType:= 'image/jpeg';
   DisplayName:= ExtractFileName (laPicture.text);
   filename:= ExtractFileName (laPicture.text);
   ExtraHeaders.Values['Content-ID'] := '<12345>';
 end;

The key line is the one which starts with 'ExtraHeaders' - this assigns a content-ID value to the picture. The text quoted is arbitrary - it could have been the file name or the digit 1; anything will do as long as it is enclosed in angle brackets.

The HTML code for accessing this then becomes
img src="cid:12345"
Several tests confirmed that this was the key to embedding pictures in HTML mails.

The next problem to be overcome is the fact that the subject line of the email sometimes comes out in gibberish. I will address this is a future blog entry.

Saturday, January 12, 2013

Play it again, Sam

I went to the theatre the other night. To some of my readers, this may seem an everyday, almost mundane, thing to do, but as far as I can recall, I don't think that I've been once to the theatre in the last 34 years, ever since I emigrated... probably something to do with the language (and why should I go to see Shakespeare translated into Hebrew?).

So what dragged me out on a cold and windy (but thankfully not rainy) night? My old friend, Woody Allen's first play, "Play it again, Sam". As I related several years ago, this is probably my favourite film and I traveled many miles in order to see it. What I didn't write in that blog entry was that I also saw the play from which the film was derived - in a theatre in Hampstead, early 1978. I think that it was a student company that put on the play so one did not expect much.

Anyway, the play has been translated into Hebrew and is now being presented on the stages of Israel, courtesy of the Beer Sheva theatre company. The character of Alan Felix (aka Woody Allen) is played by either Guri Alfi or Eidan Alterman, both top Israeli comedians. The show which I saw had Guri Alfi, who is definitely more visible these days than Alterman, appearing twice a week on television; he seems a very good match for Allen.


Everything was excellent: the play, of course (the translation was fine and covered maybe 90% of the film), the set, the acting and especially the direction. I had wondered how they would deal with Bogart and Nancy (the ex-wife) coming in and out of the play; there were three video screens placed stage left, stage right and stage rear upon which Bogart appeared and performed his dialogs with perfect timing. Nancy appeared sometimes on the screens and sometimes in the flesh.

The actress who played Nancy also played several of the dates, most noticeably Sharon. This scene was played out almost identically to the film (although the track medal which Woody Allen displayed ostentatiously was replaced by a similar medal for 10km speed walking), including throwing the record across the room. This scene had an addition to the dialogue: the record (not played) was by Thelonious Monk, and Alfi got many laughs by pronouncing this name as unclearly as possible (this might be one of his personal party pieces). We had the Jackson Pollock dialogue in the museum complete, although they missed the joke at the end by the girl walking off without looking at Alfi after he asks what she's doing on Friday night (on Saturday night, she's committing suicide).

On stage was a pianist who accompanied the acting, along with playing the odd song (especially, of course, "As time goes by"). There was a joke possibly taken from Randy Newman in his recording of the song 'Shame' in which he starts talking to the background singers (who sing 'Shame') - "could you please keep quiet". Alfi also talks at one point to the pianist, asking him to be more quiet.

What surprised me the most was how they managed to encompass the variety of styles presented in the film - especially the third act - on one stage in a continuous performance. Indeed, very little was missed out; the entire Julie sequence when Alan, Dick and Linda go to the beach house for the weekend was unsurprisingly cut, but made very little difference.

The above picture shows Guri Alfi as Alan Felix and Efrat Boimold as Linda Christie. I should point out that Boimold didn't appear at the show we attended; her part was played by Yael Grobglass, who is both taller and slimmer than Boimold (her picture appears below).

If you get the chance, go and see this show.

Tuesday, January 08, 2013

Previewing an HTML email

Following on from my previous post about creating emails and sending them to a mailing list, the Occupational Psychologist asked me whether it is possible to add pictures to the emails. Whilst this is not an area in which I have great expertise, I was able to answer in the affirmative because the original HTML code which I found included a picture.

But my problem then became - how does the program know where to insert the picture (top, middle, bottom)? I solved this problem - or at least, postponed it - by adding a radiogroup to the form which allows the user to choose either top and bottom, and then simply add the HTML code necessary in the correct place:
if rg.itemindex = 0 then add ('!img src="' + laPicture.Text + '"!br!');
for ml:= 1 to mem.lines.count do add (mem.lines[ml - 1] + '!br!');
if rg.itemindex = 1 then add ('!img src="' + laPicture.Text + '"'!br!');
The above snippet again has substituted an exclamation mark for an angle bracket as the editor here does not like HTML code, even if it's within a blockquote passage.

I sent myself an email and discovered that the picture's path had best not contain any Hebrew text; it's best to store such a picture in the root directory of C or c:\windows\temp.

I then tried to second guess the OP, wondering what she would ask for next. As the email is now no longer WYSIWYG because of the picture, she would probably want to see a preview. No problem (or so I naively thought)! All I need to do is save the HTML code in a file, fire up the default Internet browser and tell it to read the file which I have just created.

First problem: the HTML code was only created in response to clicking the 'email' button, which would actually send the email. Thus I need to create the HTML code once for preview, storing it in a file, and once for sending. This actually is not too bad, as the user could change the text of the email, requiring the HTML to be recreated. Once I got past this stage, I then discovered that my Hebrew text was appearing as garbage letters.

I started googling Unicode and similar matters, only to discover that if I did try creating a unicode file (not necessarily a simple matter in Delphi 7), the result would still not appear correctly. Then I remembered that browsers have an 'encoding' option; once I used the correct encoding on my displayed page, the correct text was displayed. How can I avoid the user having to choose the correct encoding every time? By putting the instruction in the HTML file, of course! This requires inserting some code directly after the !html! line in the header - !meta http-equiv="Content-Type" content="text/html; charset=windows-1255"!. Windows-1255 is the default page for Hebrew. Once this instruction was inserted, the Hebrew text appeared correctly.

Last flourish: instead of my programming calling the default browser to display the code, why not include a minimal browser in my own program? This would allow me to delete the temporary file which I had created every time the preview screen closes. As this is Delphi, it was no problem to find the TWebBrowser component and insert it into a form. The browser is told to display the file by calling its Navigate method. Simple as pie.

What's left? Well, it would be nice to display a picture in an arbitrary place in the letter. I could do this by replacing the radiogroup with an edit box, asking the user in which line to place the picture (0=top, 999=bottom, 5=after the fifth line, etc) but this may be too much. I'll find out on Friday.

Another possibility would be always adding a picture, such as the OP's logo, at the bottom of the letter. This would require storing the name of the picture in the database, but is simple to implement.

Monday, January 07, 2013

Recurring actors in long running TV shows

It sometimes happens in a long running tv show that an actor will appear over the years in a few supporting roles, each time as someone different. This happened a fair amount in the Star Trek franchise, although they had it easy as someone could appear as a human one week, as a type 1 alien another week and as a type 2 alien the following week.

There are two long running British series that I watch every week: Casualty (now in series 26, Israel running about three months behind Britain) and Silent Witness (now in series 12, running about four years behind Britain). There have apparently been several actors who have played multiple characters in Casualty although I haven't been aware of this. As we only see one episode a week, it's easy to forget the extras.

But Silent Witness is shown every night (Sun-Thu), so we get through the episodes at a cracking pace. I've noticed two supporting actors who have appeared twice: Ruth Gemmell played a detective constable in all six episodes of the first series, but was 'killed' at the end of that series. Despite this, she made a remarkable recovery and appeared as a Detective Inspector in series nine or ten (not listed on IMDb). I know that dying (especially early) is a good way to sell records but apparently it also helps one's police career.

A rather more egregious example is that of Ciaran McMenamin, who played the part of Prof Sam Ryan's hitherto unknown son in episodes 1 and 2 of series 8, the final episodes of Ryan (Amanda Burton). In these episodes, he played a surly and unpleasant Irishman. Lo and behold, in episodes 5 and 6 of series 12, he once again plays a fairly major part, also as a surly and unpleasant Irishman.

In the first few series of SW, each series featured an unvarying police team (hence the multiple appearances of Ruth Gemmell as a dc) but since then, there have always been new policemen every week ... except once, when Paul Panting played the part (alliteration running wild for a minute) of DCI Mumford in a few different episodes. How this policeman rose to the exalted rank of Detective Chief Inspector is beyond me as he doesn't seem to be a very good policeman. The same actor apparently played the part of 'Gordon' in one of the earliest episodes but didn't trouble my memory then.

Sunday, January 06, 2013

Sending mail from programs

I've been working in a desultory fashion for the past two weeks on adding mailing lists to the management program which I write and maintain for the Occupational Psychologist. Whilst the concept is obvious (define a list which holds the email addresses of people), the actual implementation is slightly more complicated, as there are at least two types of people with email addresses defined in the program - therapists and customer contacts - and their details are stored in different tables. But once this problem is out of the way, all that remains is to create a form which receives a mailing list, a subject, an optional file and text (one's ordinary email message) and then send the subject, file and text to the mailing list.

There are a few caveats which need to be considered before one starts:
  1. Not everyone in the mailing list may have an email address
  2. The people in the mailing list should appear in the Bcc field, so all the recipients are oblivious to who also appears in the mailing list
  3. The number of recipients who appear in a given mail should not exceed 25, in order not to arouse the mail server's spam filter
As there are other parts of the program which send mails via Outlook, I thought that I would start with the same general idea (open Outlook, connect to the Inbox, create a mail, insert the data). I noticed that in all the other cases, the program displays the email on the screen but does not send it, leaving this to Outlook. This is because these cases insert a recipient's address but do not include any text.

This meant that I had to add the final necessary stages - add text, send, close Outlook - to the mailing list code. I wrote general code to do this a few years ago so it wasn't a problem to add the code. Having written that, I should point out that this old code was written before Outlook adopted a security patch which prevents unauthorised mails to be sent. The way to overcome this security patch is to use the Redemption software package. 

Whilst using this package allows my code to create and send emails, it also causes my code to crash with an Access Violation message. I noticed that the address in the AV message to always be the same, which is a give away that there is probably a bug in the clean-up code of the library. After I googled the issue, I came across a mail which I myself had posted to Stack Overflow, presenting this exact same issue (with the same address in the AV message) and asking for a solution. Whilst several people had posted answers, none of them helped.

I then realised that I must have given up on writing Outlook code at that point and had moved to sending emails via an SMTP server (what might be termed, "the classical way") and the Indy components which come with Delphi. Once I had this problem solved, sending the emails - along with all the caveats listed above - became easy.

For icing on the cake, I also discovered how I could send the emails as right to left displaying HTML text as opposed to left to right plain text (problematic when the text is in Hebrew). I would like to be able to take the output of a rich text component and use this directly but apparently rich text and HTML are different and there is no simple way to convert.

There is one problem which comes with using SMTP code: the component sending the mail has to know the address of the SMTP server, the account name and password. This is not the sort of information which I would like to embed in a program as a hacker could get the program, decipher the information and then hack the email account. In the past, I have stored this information (encoded or otherwise) in the computer's registry, but here I preferred to store the information in the database itself. 

And after all that typing, here's the code to a simple SMTP program

procedure TForm1.EmailBtnClick(Sender: TObject);
var
 i: integer;
 html: TStrings;
 htmpart, txtpart: TIdText;

begin
 html:= TStringList.Create;
 with html do
  begin
  Add ('!html!');
  Add ('!head!');
  Add ('!/head!');
  add ('!body!!div align="right"!');
  for i:= 1 to mem.lines.count do
   add (mem.lines[i-1] + '!br!');
  Add ('!/div!!/body!!/html!');
  end;

 with IdSMTP1 do
  begin
   host:= .......;
   username:= .......;
   password:= .......;
  end;

 with email do
  begin
   From.Address:= 'somewhere@somecompany.com';
   bcclist.add.address:= 'user@user.com';
    subject:= edSubject.Text;
   ContentType := 'multipart/mixed';
   Body.Assign (html);
  end;

 txtpart:= TIdText.Create(email.MessageParts);
 txtpart.ContentType:= 'text/plain';
 txtpart.Body.Text:= '';
 htmpart:= TIdText.Create (email.MessageParts, html)
 htmpart.ContentType := 'text/html';

// TIdAttachment.Create (email.MessageParts, fn);

 try
  try
   IdSMTP1.Connect (1000);
   IdSMTP1.Send (email);
  except
  end;
 finally
  if IdSMTP1.Connected
   then IdSMTP1.Disconnect;
 end;
 html.free;
 close;
end;

This would be the code, except that there's a bit missing - possibly the most important part. The formatter here doesn't seem to allow me to enter html code as actual text, so I've had to alter the code slightly. In the lines which follow 'with html do...', the exclamation mark (!) should be replaced by an angle bracket.