In a wake to continue my article Popkin Functions, here’s an extension to the mail() function. The mail() function in that article used CDONTS, the now deprecated eMail sending COM provided by Microsoft. I won’t go deep into it but Microsoft hinted its deprecation in Windows 2000 when it shipped another eMail sending COM named CDOSYS with it. CDONTS was included in it to have backward compatibility but in WindowsXP & Windows Server 2003, its been removed. So, those using CDONTS until now, if their host switches over to Windows Server 2003, might find them in a soup as their CDONTS scripts won’t work.
There’ve been many tutorials and articles on how to use & send eMails using CDOSYS & you can relax, I’m not going to start one. Its just that I’ve extended my mail() function, which uses CDONTS, to use CDOSYS. Actually, calling it an extension would be quite wrong, as its a brand new function with no reference or relation whatsoever with the mail() function, but anyways… 😀
So, not taking your much time, here’s the candy. 😀
- Private Function mailCdo(pTo, pFr, pCc, pBc, pSu, pMes, pBf)
- pTo = TRIM(LCASE(pTo))
- pFr = TRIM(pFr)
- pCc = TRIM(LCASE(pCc))
- pBc = TRIM(LCASE(pBc))
- pBf = TRIM(LCASE(pBf))
- SET objMail = Server.CreateObject("CDO.Message")
- ' SET objConfig = CreateObject("CDO.Configuration")
- ' '//Configuration:
- ' objConfig.Fields(cdoSendUsingMethod) = cdoSendUsingPort
- ' objConfig.Fields(cdoSMTPServer) = "localhost"
- ' objConfig.Fields(cdoSMTPServerPort) = 25
- ' objConfig.Fields(cdoSMTPAuthenticate) = cdoBasic
- ' '//Update configuration
- ' objConfig.Fields.Update
- ' SET objMail.Configuration = objConfig
- WITH objMail
- .From = pFr
- .To = pTo
- IF NOT(pCc="") THEN
- .CC = pCc
- END IF
- IF NOT(pBc="") THEN
- .BCC = pBc
- END IF
- .Subject = pSu
- IF pBf="html" THEN
- .HTMLBody = pMes
- .TextBody = pMes
- END IF
- END WITH
- SET objMail = NOTHING
- ' SET objConfig = NOTHING
- End Function
Well, I’ll also explain the usage, not to worry. 😀
Readers of Popkin Functions & users of my mail() function must’ve recognised some of the parameters, no? Anyways, here they are all explained.
pTo is for specifying the destination eMail address, pFr is for specifying the sender’s eMail address, pCc & pBc are for specifying Carbon Copy recievers, pSu is for setting the eMail’s Subject, pMes for the message or eMail body & finally pBf for specifying whether the eMail should be sent as plain text or html. The values you can give it are “html” or “text”, with quotes ofcourse. See, its not difficult at all. Now here’s an example:
- strTo = "firstname.lastname@example.org"
- strFrom = "email@example.com"
- strSubject = "Hey Dude!!"
- strMessage = "What's up big bro?"
- CALL mailCdo(strTo, strFrom, "", "", strSubject, strMessage, "text")
This will send our eMail in a text format as we have specified the BodyFormat as text in the function call. Notice the blank quotes in the function? They are in place where the pCc & pBc parameters are to be set. Since we don’t need them, I’ve set them as empty.
Now the important things to note are:
- All parameters except pCc, pBc & pBf, are required for sending an eMail.
- You can skip the pBf parameter if you want, as by default its set as TEXT, meaning that essentially you need to set it only when you want to send an HTML formatted eMail. But its a good practice that you set it as TEXT if you want to send a TEXT formatted eMail or as HTML if you want to send an HTML formatted eMail. And it should ofcourse be wrapped in quotes(“).
Now, some of you may find that this function gives out error in its current state, when called & every parameter is passed correctly. 😯 Well, the code isn’t faulty, so relax. 🙂 Its just that this situation will arise if you are running this code on WindowsXP. So for this function to work correctly, you need to uncomment all the lines in the function that doesn’t have double slashes(//) in them.
If your code still doesn’t work, providing that everything else is correct, you need to add following lines to the top of the page where you are calling this function.
- <!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows 2000 Library" -->
- <!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->
These lines should be the first two lines of your ASP page in which you are calling this function.
If you are running this code on Windows Server 2003, then you wouldn’t need the above 2 adjustments to the code. You can run the original code outright. 😀