Sending E-mail From Your MultiValue Programs - Part 1
E-mail is part of every day business life. We receive it and send it on a daily basis. Our vendors and customers send it on a daily basis. It is amazing that many companies still don’t have e-mail sending and receiving integrated directly into their business systems.
Many times, emails are sent using Windows GUI programs tied into the MultiValue systems, or through the terminal emulators. This works well when you are actively trying to send an e-mail from a process, but not when you are updating your business processes in the background or need to send status information to customers or employees when process are done without bothering the person entering the information.
The most powerful thing about the MultiValue business environments is the enhancements scripting and programs you have built into the database. Many other database systems out there require most of your business logic to reside on a separate server, or outside the database, which means when something in the database is updated, programs have to remember to run other processes as part of that update.
Since the business rules, logic, and other business processes run on our MultiValue database, we don’t have to remember to do things, nor do we have to worry about creating a Multi-Tier programming environment, or problems with running the same process from on different computers.
Integrating e-mail sending functions into your business application is actually really easy. The hard part is choosing which e-mail server you are going to use to send your e-mail.
In this article we will talk about how to send emails from different types of e-mail systems and the pro and cons of using those systems.
What is e-mail and how do we use it?
Ok, so this can sound like a dumb question. We all know what e-mail is and how to use it. E-mail allows us to communicate information between two or more parties. What many people forget is that the “parties” part of that statement isn’t necessarily a human person. It could be a business application, a blog, a newsgroup, or a status system.
The sender of the e-mail isn’t always human, either. But most e-mails need to be readable by humans. An example of this is bounced e-mail messages. We all have seen these. When we open them in our e-mail client, we see a message about why it bounced and maybe a part of the original e-mail. There is more to the e-mail than this though. There is computer parsable information that tells send servers, programs, or bots why this e-mail bounced in a status or error code, where it came from, which server it bounced from, in addition to a human readable description of the bounce reason.
I’m not going to go to in-depth in this article on bounce messages and no human readable e-mails. I just want to make you aware of it.
Before we get into how to actually send the e-mail, we have to understand how to create the actual e-mail. At a very high level, the format of e-mail is very simple. It can get very complex if you are trying to implement a full mail server with all the features for mail lists and spam filters, but for our purpose we will look only at the basics. It will likely be all you need, especially if you will only be sending e-mails.
An e-mail consists of two major sections: header and body. The body is the easy part. It is the information that you want the receiver to read. There is really only one requirement for the body section. A blank line MUST exists between the last line of the header section and the first line of the body section. Other than that, there is no real requirement except for how to format the information you want the user to read.
I know that it may not make complete sense, especially when you get into attachments. But let me get through how to create the header section first, and then things may make sense.
The header section of the e-mail provides the key information that needed in order for e-mail to be received successfully by its destination. There are only four required header fields, but others are recommended.
The required headers fields are “From”, “To”, “Date”, and “Subject.”
“From:” — This header is supposed to contain the e-mail address and name of the person or process sending the e-mail. The reality is that this can contain anything you want as long as it looks like an e-mail address.
“To:” — This header lists the e-mail addresses and names of the people or processes you want to receive this e-mail. Again, like the “From:” header, this really can contain anything you want as long as it looks like an e-mail address. Depending on the routines you use to actually send the e-mail, this header is used by the sending program or ignored. When I get into the actually sending part I’ll talk about that more.
“Date:” — This header contains a date and time of when the e-mail was created. This date and time needs to be formatted in a specific way. This format is called RFC 1123. It basically consists of the following: Day-Of-Week, Day MonthName Year 24Hour:Min:Sec Time-Zone
Ie: Wed, 9 Sep 2008 13:45:00 EST
“Subject:” — The subject of the e-mail. Pretty straight forward.
Each header field must have a “:” and a space or tab between the header field and the data, followed by a CRLF to terminate the line. For those that don’t know the values of CRLF, it is defined as a Carrage Return and Lind Feed, which is CRLF = CHAR(13) : CHAR(10) in MultiValue Basic.
There are many other headers, but I’m going to focus on a simple e-mail first before I get into the more complex headers.
In figure 1, I’ve included a sample of a simple e-mail with the headers and a text body.
To: firstname.lastname@example.org From: email@example.com Date: Weds, 9 Sep 2008 13:45:00 EST Subject: Simple Email hello, this is a simple email message.
Because of the origins of e-mail, e-mail clients will assume the body section of your e-mail is plain text. To tell a client what kind of information is in the body section, you need to include a header called Context-Type.
There are a lot of different context types out there. For most e-mails you will only care about four context types: text/plain, text/html, multipart/alternative, and multipart/mixed.
For plain text e-mails, you would want to add the “context-text: text/plain”. There are a few other headers that I recommend you include to help you get your e-mail safely past spam filters. These headers are MIME-Version, X-Mailer, and Content-Transfer-Encoding.
MIME-Version will always have a value of: “MIME-Version: 1.0”. X-Mailer header is a free form text description of where this e-mail comes from. Many times the e-mail sending routine you use will add this, but not always. There are many e-mail filters that specifically look to see if this header exists. If it doesn’t have a X-Mailer header, then it flags it as a higher possibility of spam.
Context-Transfer-Encoding is pretty straight forward as well. Unless you want to get into encoding the text of your e-mail, you should set it’s value as “8bit”. If you know you won’t have any characters above CHAR(128), then you can use “7bit”. Most text-only e-mails won’t have characters above CHAR(128) since that is in the unprintable range.
Take a look at figure 2 to see an e-mail with all the headers included.
To: firstname.lastname@example.org From: email@example.com Date: Weds, 9 Sep 2008 13:45:00 EST Subject: Simple Email Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: International Spectrum Email Sending 1.0 MIME-Version: 1.0 hello, this is a simple email message.
There is one last thing to keep in mind when creating a e-mail — most e-mail servers impose limits of 1,000 characters per line of e-mail, including the CRLF. For readability, it is suggested that you format your e-mails to a maximum of 80 characters per line.
Please keep in mind that I’ve only scratched the surface of creating e-mails. There is much more. In fact there are whole documents, web sites, and books that explain just how to create the e-mail headers and body. I will get into more of this later, but I wanted to give you something to start with. You can find out more about e-mail protocol and formats in the book Internet E-mail Protocols: a Developer’s Guide by Kevin Johnson, or you can just do a search on the Internet.
In the next article, I’ll cover how to use the information in this article to send an e-mail.