Sending email without all the hassle! Use the pickup directory.
Ok, I just learned something new the other day. I have been using and administering IIS for years with or without the SMTP service running, but I never knew that you could send emails simply by placing them in the pickup directory. Yes, just copy a text file into the c:\inetpub\mailroot\pickup folder and the SMTP service will try and send it.
Why is that a big deal you ask? Ask those developers who have for years used packages like CDONTS. In some languages like PHP, the built in mail features are great and they just work. In ASP, the add-on libraries that you have to use leave a bit to be desired. Quite frankly, I like the idea of writing the whole email to a file and having it just head off into the blue.
How does this work you ask? I generally build up the email string first (easiest way is to send it to myself using outlook, then paste it into my scripts!), then write the whole thing to the directory when it is ready usinge fopen/fwrite. Chances are you will never actually see the file appear in your explorer view if the SMTP service is running as it will see the file and copy it to the queue right away. If it fails to send the email, it will dump the email to the badmail directory along with an error message in a seperate file (remove the boxes and you will see the error). If it doesn’t end up in badmail, it should be delivered shortly.
One thing to note is that we are working in a windows environment here. It is VITAL that you use the proper line feed characters (\r\n). Failure to do so (\n alone won’t cut it) will result in your email being rejected even if everything else is perfect. I kept getting an error code (0xC00402CE) for exactly this reason.
Here is an example of a simple email that is properly formatted.
Date: 27 Nov 2005 0852 GMT
To: "Bob Smith" <bobs@smith.com>
From: "Franky Rumple" <frankyr@rumple.com>
Subject: Testing Email
MIME-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable
Hello, this is email!
That is a basic email. If you create a text file with that information (put in your email address on the “To” line) and copy it to the pickup folder, you should get it in your inbox shortly. If you don’t, one place to check is the IIS Manager under the SMTP Service area. You can define the domains that it will forward mail to. In my case, I just add *.com, *.org, etc… This tells the SMTP service it is ok to send emails to people at those domains. You do want to be careful here as you could turn yourself into a spam relay if your SMTP service is unprotected. Mine happens to be behind a firewall, so it can only be used by internal applications to send email.
Using this method, it isn’t to tough to add attachments and such. You will have to format the email to use the correct mime headers and possibly use Base64 encoding on your attached files. This isn’t too big a deal though. To get the correct format, I suggest using your regular email client (Outlook?) and sending yourself an email with the attachment. Once it arrives, view the message source. You can copy/paste this and use it as a template to generate your own emails. If your attachement will change, then cut out that part but leave all the MIME headers. Then, with your code, plug in the appropriate data when needed (use str_replace and put a place holder, or concatenate as you go, whatever is easier). Make sure your data encoding matches the MIME header “Transfer-Encoding”. If it is set to Base64, then your data needs to be encoded using Base64 encoding.
If you use scripts, make sure to use \r\n for line feeds instead of just \n. Also, notice that after the headers (MIME or main Email headers) that you need an extra \r\n to seperate the header from the data. This is just a clue that the header is done and the message or data is beginning.
Generating emails is much easier than processing them. You can spend a lot of time learning about all the different headers and such and how to create them by scratch. While learning is always great, if you just want to get an email out, create one using Outlook and send it to yourself and use that as a template. It can really save you some time.
Ray Pulsipher
Owner
Computer Magic And Software Design