BBC BASIC for Windows
Programming >> Communication and Input/Output >> Formatting email
http://bb4w.conforums.com/index.cgi?board=communication&action=display&num=1396449053

Formatting email
Post by Edja on Apr 2nd, 2014, 2:30pm

I'm writing a program to facilitate the organization of a tennis tournament by the end of June. One of the functionalities I need to implement is the automatic sending of roughly 1000 e-mails
Thanks to Richard's code on the WIKI :
Code:
PROCsendmailattach(SMTP$,From$,To$,"","",Subject$,ReplyTo$,Message$,Attach$) 
I managed to make this work beautifully.

In the (trivial) FN herunder I compose the message (message$) to be sent. One of the elements of this string is another string Timeslot$. Timeslot$ will be something like "Saturday 28 June 4:00 pm to 5:30 pm"

My question: Is there a way to make Timeslot$ appear in bold in the e-mails perhaps by adding some control codes ? If yes, how ?
Code:
      DEF FN_Compose_Mail
      LOCAL message$
      message$="Beste "+Voornaam1_1$+","+LF$+CRLF$+"U hebt zich ingeschreven voor ons tornooi in de "\
      \+Reeksnaam$+" punten"+LF$+CRLF$\
      \+"De wedstrijd voor de eerste ronde wordt gespeeld op"+LF$+CRLF$+"           "+Timeslot$+LF$+CRLF$\
      \+"Uw partner: "+Voornaam1_2$+"  "+Naam1_2$+LF$+CRLF$+"Uw tegenstanders:  "\
      \+Naam2_1$+"  "+Voornaam2_1$+" - "+Naam2_2$+"  "+Voornaam2_2$+LF$+CRLF$+"Mvg,"+LF$+CRLF$\
      \+"De tornooileiding"+LF$+CRLF$+"Tel: GSM 0474.449.309"+CRLF$+"Mail: info@borsbeektennisclub.be"
      =message$
 


Eddy




Re: Formatting email
Post by rtr on Apr 2nd, 2014, 7:28pm

on Apr 2nd, 2014, 2:30pm, Edja wrote:
Is there a way to make Timeslot$ appear in bold in the e-mails

A 'plain text' email is just that, I'm afraid - you don't get any kind of control over text style. To do what you want would mean sending a MIME-encoded message containing both a plain-text part (Content-Type: text/plain) and an HTML part (Content-Type: text/html); then you can use HTML tags to control formatting etc:

http://en.wikipedia.org/wiki/MIME

The receiving email client will display either the plain text part or the HTML part, depending on its capabilities and user preferences (the great majority of modern email clients can render HTML).

So what you are asking for is entirely possible, but not something that I would call 'simple'!

Richard.
Re: Formatting email
Post by Edja on Apr 3rd, 2014, 08:55am

Quote:
what you are asking for is entirely possible, but not something that I would call 'simple'!
The result, as it is, already covers the functionality. It would have been a nice touch , though.
I may revisit your solution and check the Wikipedia article in detail. If 'within my talents', I'll work on it for the next edition of the tournament in 2015.
If you say it's not that 'simple', I feel forced to give priority to the other part of the program for now.

Thank you,Richard.

Eddy
Re: Formatting email
Post by rtr on Apr 3rd, 2014, 09:14am

on Apr 3rd, 2014, 08:55am, Edja wrote:
If you say it's not that 'simple', I feel forced to give priority to the other part of the program for now.

You could cheat! Use your regular email program (not webmail) to compose a message having the format you want. Send it to yourself, and when it arrives do a 'view source'. Then you can copy-and-paste the MIME-encoded data into your BB4W program. wink

Richard.
Re: Formatting email
Post by Edja on Apr 3rd, 2014, 7:04pm

Quote:
Use your regular email program (not webmail) to compose a message
This looks like a 'golden' cheat and I'd love to give it a try.
Unfortunately, I'm affraid I have no licence of Outlook lying around. I only use webmail. But I'll keep this in mind and see if I can try this on a friend's PC who has Outlook installed.
Perhaps next week.

Eddy
Re: Formatting email
Post by rtr on Apr 4th, 2014, 09:00am

on Apr 3rd, 2014, 7:04pm, Edja wrote:
This looks like a 'golden' cheat and I'd love to give it a try. Unfortunately, I'm affraid I have no licence of Outlook lying around.

You could always install Eudora or Thunderbird, which are free. Or, if you prefer, simply send a suitably-formatted email to me and I'll do the 'view source' and send the MIME-encoded data back to you in a ZIP.

Richard.

Re: Formatting email
Post by Edja on Apr 4th, 2014, 10:49am

Quote:
You could always install Eudora or Thunderbird, which are free
I have downloaded/installed Thunderbird and have sent an email with some text parts in bold to myself.
Then did a 'view source'. This displayed lots of data on the screen.
I 've copied/pasted this into my BB4W application and tried to apply some common sense in doing this. Several times. But,this was just trial and error and all attempts failed.
Quote:
... and send the MIME-encoded data back to you in a ZIP
I thank you for this but it seems I now got this far overhere. I'm left with 2 problems :
If it helps I can send you the MIME-encoded data that I've harvested in a separate mail ?

Eddy

Re: Formatting email
Post by rtr on Apr 4th, 2014, 1:41pm

on Apr 4th, 2014, 10:49am, Edja wrote:
which exactly is the part that I need to copy/paste into my string Message$="...?...."

You can't make it work that way because PROCsendmailattach() doesn't let you modify the headers. You will need to create a custom version of the procedure which, for example, has separate plain$ and html$ parameters rather than the existing body$ parameter.

The code of PROCsendmailattach() is not complicated, and as it already incorporates a MIME header to accommodate the attachment ('application/octet-stream' part) it should be straightforward to adapt it to add a new 'text/html' part.

Richard.

Re: Formatting email
Post by Edja on Apr 4th, 2014, 3:31pm

Quote:
it should be straightforward to adapt it to add a new 'text/html' part
As a famous Greek writer once said:'Beauty is in the eye of the beholder'. So is 'straightforwardness' cheesy
I'm not familiar with MIME , html and the likes and I'll have to dissect PROCsendmailattach. It will take me some time before I see clear in all this.
I have to meet the deadline for the tournament and will use PROCsendmailattach as is. The result is absolutely functional.
If some time time left I'll try to integrate this in the application otherwise after the tournament (end June)

Thank you for setting me on track.
Eddy
Re: Formatting email
Post by rtr on Apr 4th, 2014, 3:54pm

on Apr 4th, 2014, 3:31pm, Edja wrote:
As a famous Greek writer once said:'Beauty is in the eye of the beholder'. So is 'straightforwardness' cheesy

You yourself used the word 'trivial' without qualification! In fact, looking again at the procedures, whilst it's correct that you cannot do what you want using the unmodified PROCsendmail() you can using PROCsendmailattach() because it's already got the necessary MIME ingredients. So it's even easier than I thought!

Quote:
I have to meet the deadline for the tournament and will use PROCsendmailattach as is. The result is absolutely functional.

Seems a shame when what you would have liked to achieve could be done in minutes. In fact my suspicion is that the only reason what you tried before didn't work was that you didn't appreciate that you needed to change the 'boundary string' from whatever Thunderbird used to "--BB4Wsep" which is what PROCsendemailattach uses.

Richard.

Re: Formatting email
Post by rtr on Apr 4th, 2014, 4:28pm

on Apr 4th, 2014, 3:54pm, Richard Russell wrote:
Seems a shame when what you would have liked to achieve could be done in minutes.

I've sent you an email that is, I believe, the best that can be done using the unmodified PROCsendmailattach(). As you will see it's not perfect because there's a spurious 'attachment' containing a copy of the HTML, which you would probably prefer isn't there. To eliminate that would need a small change to the procedure.

Edit: Here's the modified procedure:

http://bb4w.wikispaces.com/Sending+an+HTML+email+with+optional+attachments

Richard.

Re: Formatting email
Post by Edja on Apr 4th, 2014, 7:04pm

It worked !
Thanks to Richards adapted PROCsendmailhtmlattach(smtp$,from$,rcpt$,cc$,bcc$,subject$,replyto$,plain$,html$,attach$)

I've sent the simplest possible email to myself with just one word in bold to avoid obscuring the issue. Once the email is received the result is like this :
Quote:
Beste,

dit is de tekst voor de test

Eddy

As an example for those (like me) who are not familiar with MIME, htlm,... it's maybe useful to show the 2 functions that create the plain text and the html text .
This 'cheat' will allow you to create some effects inside the emails. I only tested out bold text for now, but will play around with other format effects. Pls. read the previous posts from Richard in this thread where is explained how to get the relevant parts out of, in this case, Thunderbird
And don't forget to replace " with "" in the body text
Code:
     DEF FN_Compose_Plain
      Plain$="Beste,"+CRLF$+\
      \"dit is de*tekst *voor de test"+CRLF$+\
      \"Eddy"
      = Plain$
      REM----------------------------------------------------------
      DEF FN_Compose_Html
      Html$="<body text=""#000000"" bgcolor=""#FFFFFF"">"+CRLF$+\
      \"Beste,<br>"+CRLF$+\
      \"<br>"+CRLF$+\
      \"dit is de<b> tekst </b>voor de test<br>"+CRLF$+\
      \"<br>"+CRLF$+\
      \"Eddy<br>"+CRLF$+\
      \"</body>"
      =Html$ 

Eddy
Re: Formatting email
Post by rtr on Apr 4th, 2014, 8:03pm

on Apr 4th, 2014, 7:04pm, Edja wrote:
It worked !

I'm slightly surprised, since your HTML appears to have no <html></html> tags. Is there some specific reason why you didn't use my example code on the Wiki as a template?

Code:
      Plain$   = "This is a test of sending an HTML email."
      HTML$    = "<html><head></head><body>" + \
      \          "This is a test of sending an <b><i>HTML</i></b> email." + \
      \          "</body></html>" 

You may feel that I should have added those tags in PROCsendmailhtmlattach(), and that might have been sensible, but I didn't!

Richard.

Re: Formatting email
Post by Edja on Apr 4th, 2014, 10:00pm

Quote:
your HTML appears to have no <html></html> tags. Is there some specific reason why
No specific reason. It just seemed the 'natural' way to proceed. I assumed the tags would be provided by PROCsendmailhtmlattach().

I admit that I was so eager to try it out that I overlooked the part of the WIKI article with the example code. I just copied the updated procedure and then coded the two functions FN_Compose_Plain and FN_Compose_Html.
Once it worked and after reporting back here on the forum I read the article again and discovered that I deviated from your example code. In the mean time I already modified the two functions and added the <html></html> tags

If it wouldn't have worked I would have looked at the article sooner to find out what was wrong and I would have seen the missing tags. But since my code also works without the tags, I now wonder if they are really required

Eddy
Re: Formatting email
Post by rtr on Apr 4th, 2014, 10:37pm

on Apr 4th, 2014, 10:00pm, Edja wrote:
But since my code also works without the tags, I now wonder if they are really required

So you've tried it with every email client that your recipients might be using? You have been busy! wink

Please forgive the sarcasm, but I'm sure you appreciate that 'trial and error' coding is no more acceptable in HTML than it is in BBC BASIC (indeed probably less so, because there's a greater variety of email clients and HTML rendering engines than BBC BASIC interpreters!).

The only certain way of determining whether the tags are required is to refer to the relevant RFC(s), but I think one can assume that if Thunderbird (for example) includes them then they are at least desirable.

Richard.

Re: Formatting email
Post by Edja on Apr 5th, 2014, 08:33am

I just raised the question out of curiosity and certainly not to suggest that the tags were not needed.
I'm completely in line with your views. That's why I already added the tags in my FN's.
As for 'trial and error' : I should know better. I do know better. But sometimes ...
Then on the other hand : penicillin and also the light bulb were discovered that way. I'm maybe destined to discover great thingscheesy

Eddy