Table of Contents
Sending an email automatically
by Richard Russell, October 2006
See also Sending an email with attachments and Sending an HTML email with optional attachments
There are two principal ways of automating the process of sending an email message: utililising the resources of the user's existing email client (e.g. Outlook or Eudora) or doing it entirely with BBC BASIC code.
Using an email client
The advantages of this method are that it's simple, requires no configuration and can work even with mail servers requiring authentication. The disadvantage is that the process is not fully automatic: the user still has to initiate transmission by clicking on send. It's therefore not suitable if you want to send an email covertly or without any user intervention.
To adopt this method use code similar to the following:
SYS "ShellExecute", 0, 0, "mailto:"+to$+ \ \ "?subject="+subject$+ \ \ "&body="+body$, 0, 0, 0
Here to$ is the email address of the main recipient, subject$ is the subject line and body$ is the body of the message. to$ can optionally consist of two or more addresses separated by commas.
If you want to copy the message to other recipients you can do that as follows:
SYS "ShellExecute", 0, 0, "mailto:"+to$+ \ \ "?cc="+cc$+ \ \ "&subject="+subject$+ \ \ "&body="+body$, 0, 0, 0
Again, cc$ can optionally consist of two or more addresses separated by commas.
If you prefer to blind copy the message (that is, the recipients don't know to whom other copies have been sent) replace cc with bcc:
SYS "ShellExecute", 0, 0, "mailto:"+to$+ \ \ "?bcc="+bcc$+ \ \ "&subject="+subject$+ \ \ "&body="+body$, 0, 0, 0
If the body of the message consists of more than a single line then you must represent the line terminators in a special way. Rather than the conventional CRLF (CHR$13+CHR$10) you must use the character sequence “”%0D%0A“”. Because % and & have special meanings, if they appear in the body of the message you must encode them as “”%25“” and “”%26“” respectively. If necessary you can do the encoding using this function:
DEF FNmailencode(A$) LOCAL B$, I% FOR I% = 1 TO LEN(A$) CASE MID$(A$,I%,1) OF WHEN CHR$13: B$ += "%0D" WHEN CHR$10: B$ += "%0A" WHEN "%": B$ += "%25" WHEN "&": B$ += "%26" OTHERWISE: B$ += MID$(A$,I%,1) ENDCASE NEXT I% =B$
Using BBC BASIC code
This approach gives you more control over the content of the email, and allows you to send it without any user interaction (assuming there is an internet connection). However there are a couple of disadvantages: you need to know the name (or IP address) of the SMTP server to be used, and it cannot work with a server which requires authentication. In practice this usually means you need to use the 'local' SMTP server provided by the ISP supplying the internet connection. There is, as far as I know, no way of discovering this automatically.
You can send an email using the procedure below. The to$, cc$, bcc$, subject$ and body$ parameters have the same meaning as in the previous example, except that there's no need to do any special encoding of the message body. The smtp$ parameter is the name of the SMTP server (as discussed above), from$ is the email address of the sender (which must be valid) and replyto$ is an optional address to which replies should be addressed.
The code uses the SOCKLIB library so there must be an “INSTALL @lib$+“SOCKLIB”“ statement previously in the program.
DEF PROCsendmail(smtp$,from$,to$,cc$,bcc$,subject$,replyto$,body$) LOCAL D%, S%, skt%, reply$ DIM D% LOCAL 31, S% LOCAL 15 DEBUG% += 0 SYS "GetLocalTime", S% SYS "GetDateFormat", 0, 0, S%, "ddd, dd MMM yyyy ", D%, 18 SYS "GetTimeFormat", 0, 0, S%, "HH:mm:ss +0000", D%+17, 15 D%?31 = 13 PROC_initsockets skt% = FN_tcpconnect(smtp$,"mail") IF skt% <= 0 skt% = FN_tcpconnect(smtp$,"25") IF skt% <= 0 ERROR 100, "Failed to connect to mail server" IF FN_readlinesocket(skt%, 1000, reply$) > 0 THEN IF DEBUG% PRINT "R: "+reply$ ENDIF WHILE FN_readlinesocket(skt%, 10, reply$) > 0 IF DEBUG% PRINT "R: "+reply$ ENDWHILE PROCsend(skt%,"HELO "+FN_gethostname) PROCmail(skt%,"MAIL FROM: ",from$) IF to$<>"" PROClist(skt%,to$) IF cc$<>"" PROClist(skt%,cc$) IF bcc$<>"" PROClist(skt%,bcc$) PROCsend(skt%, "DATA") IF DEBUG% PRINT "Sending header..." IF FN_writelinesocket(skt%, "Date: "+$D%) IF FN_writelinesocket(skt%, "From: "+from$) IF FN_writelinesocket(skt%, "To: "+to$) IF cc$<>"" IF FN_writelinesocket(skt%, "Cc: "+cc$) IF subject$<>"" IF FN_writelinesocket(skt%, "Subject: "+subject$) IF replyto$<>"" IF FN_writelinesocket(skt%, "Reply-To: "+replyto$) IF FN_writelinesocket(skt%, "MIME-Version: 1.0") IF FN_writelinesocket(skt%, "Content-type: text/plain; charset=US-ASCII") IF DEBUG% PRINT "Sending body..." IF FN_writelinesocket(skt%, "") IF FN_writelinesocket(skt%, body$) IF FN_writelinesocket(skt%, ".") PROCsend(skt%,"QUIT") PROC_exitsockets ENDPROC DEF PROClist(skt%,list$) LOCAL comma% REPEAT WHILE ASClist$=32 list$=MID$(list$,2):ENDWHILE comma% = INSTR(list$,",") IF comma% THEN PROCmail(skt%,"RCPT TO: ",LEFT$(list$,comma%-1)) list$ = MID$(list$,comma%+1) ELSE PROCmail(skt%,"RCPT TO: ",list$) ENDIF UNTIL comma% = 0 ENDPROC DEF PROCmail(skt%,cmd$,mail$) LOCAL I%,J% I% = INSTR(mail$,"<") J% = INSTR(mail$,">",I%) IF I% IF J% THEN PROCsend(skt%, cmd$+MID$(mail$,I%,J%-I%+1)) ELSE PROCsend(skt%, cmd$+"<"+mail$+">") ENDIF ENDPROC DEF PROCsend(skt%,cmd$) LOCAL reply$ IF DEBUG% PRINT "T: "+cmd$ IF FN_writelinesocket(skt%,cmd$) < 0 THEN ERROR 100, "Send failed" ENDIF IF FN_readlinesocket(skt%, 200, reply$) > 0 THEN IF DEBUG% PRINT "R: "+reply$ ENDIF WHILE FN_readlinesocket(skt%, 10, reply$) > 0 IF DEBUG% PRINT "R: "+reply$ ENDWHILE ENDPROC