|
Advanced SMTP Mail
The most common use of the SMTPMail component is to send simple text email. However the SMTPMail component is very powerful and can be used to send many different kinds of mail, this article will show you how to send richer forms of mail including HTML, different character sets and embedded pictures. The most limiting feature is that often you don't know what client software will be used to read the message. You don't want to send some HTML message if the reader can't display it. Therefore it pays to be conservative, never send important pieces if you don't know whether the end recipient can read them. SMTP and MIMEJust about all email on the Internet is sent using SMTP. The SMTP protocol has been around since 1982 and is pretty much ubiquitous. The protocol specifies how different participants in the mail system talk to each other. While SMTP describes how transfer agents, mail servers and clients all send email to each other the real work of formatting the mail message is done by the MIME protocol. MIME is a simple standard that has grown to become a bit of a mess. The most important concept is that it wraps different chunks of data, it can name them and most importantly it can describe their content type. AttachmentsOne of the first extensions to email was to send attachments. Today most email clients can read attachments so you should not have trouble with them. Here is an example: set m = CreateObject("Toolsack.SMTPMail")
m.SetFrom "me@nospam"
m.SetSubject "This is a test to show attachments"
m.AddRecipient "yourself@somewhere.com"
m.AddBody "Here are some attachments."
m.AddAttachmentFile "C:\temp\report.pdf"
htmldoc = "<h1>heading</h1><p>This is an html doc</p>"
m.AddAttachmentText htmldoc, "blah", "text/html"
m.Send "smtpserver.name.com"
This mail will have 2 attachments. Firstly the pdf file, then an HTML document. Note that the HTML document is given a content type of text/html. If the name had an html extension, the component would have sorted out the content type automatically like it did for the pdf. Alternate BodiesFor normal text mail a client would call the AddBody method once. There is a mechanism for sending more than one alternative body. These bodies are sent together in order in the message and clients choose which body it will display. They choose the last body that it is capable of displaying. For example: set m = CreateObject("Toolsack.SMTPMail")
m.SetFrom "me@nospam"
m.SetSubject "This is a test to show multiple bodies"
m.AddRecipient "yourself@somewhere.com"
m.AddBody "This is a plain text body"
m.AddBody "<p>This is an <b>html</b> body</p>", "text/html"
m.AddBody "This is some weird body type", "custom/random"
m.Send "smtpserver.name.com"
A client that received this mail would look at the bodies and choose one. The last body type is custom/random which it wont understand. The next last body type is HTML. If the client can display HTML it will display that body. If not it will default to the first body. Try to send mail to yourself with different body types to see what gets displayed by your email client.
HTML Bodies and PicturesHTML bodies can make email look much more attractive. Pictures enhance the mail even further. When sending HTML email you can embed pictures into the email. For example: Set m = CreateObject("Toolsack.SMTPMail")
m.SetFrom "me@nospam"
m.SetSubject "This is a test to show pictures"
m.AddRecipient "yourself@somewhere.com"
htmlbody = "<img src='http://www.toolsack.com/logo.gif'>"
htmlbody = htmlbody + "<img src='logo2.gif'>"
htmlbody = htmlbody + "<br><p>This is an html body</>"
m.AddBody htmlbody, "text/html"
m.AddPictureFile "C:\temp\logo2.gif"
m.AddAttachmentFile "C:\temp\file.txt"
m.AddAttachmentText htmldoc, "blah.html", "text/html"
m.Send "smtpserver.name.com"
You can see that the body contains two pictures. The first picture is just a link to a picture on a web server. When the email client shows the page, it will download the picture. This will mean the original email is smaller, but the picture will take a time to show, or may not show at all if the email client is disconnected from the internet or can not reach the web server for some reason. The second picture is added to the actual mail. This would usually be preferred, but some email clients can not handle this so be careful. Be especially careful about sending both pictures for HTML and attachments. Character SetsIt is possible to send mail using characters other than plain ASCII. This is done by setting the charset parameter when adding bodies or the subject. It is possible to set the character set when adding headers and recipients, but this is not recommended as many email clients (including Microsoft Outlook) do not display them correctly. If the component detects European characters the character set will automatically be set to ISO-3991.
HeadersYou can add extra headers to describe the mail, or to give instructions to the receiving mail client. For example you can make the mail high priority by adding the headers: mailObj.AddHeader "X-Priority", "1 (Highest)"
Similarly, you can set the priority to "2 (High) ", "4 (Low) ", "5 (Lowest)". You can request a read receipt with the header Disposition-Notification-To. Use it like this (Make sure you surround the address with <>): mailObj.AddHeader "Disposition-Notification-To", "<you@x.com>"
Also you can set an address to reply to with the "Reply-To" header: mailObj.AddHeader "Reply-To", "<you@x.com>"
There are a number of other headers you can experiment with. Most email clients will show you or allow you to view headers. Try different options to see what headers are generated. ConclusionThe Toolsack SMTPMail component is very powerful and can create very rich email. However not all email clients have all the functionality to read the mail so be careful when you use it.
|