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. 😀
[asp]
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
ELSE
.TextBody = pMes
END IF
.Send
END WITH
SET objMail = NOTHING
‘ SET objConfig = NOTHING
End Function
[/asp]
Whoa!! :dizzy:
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:
[asp]
strTo = “dude1@example.com”
strFrom = “dude2@example.com”
strSubject = “Hey Dude!!”
strMessage = “What’s up big bro?”
CALL mailCdo(strTo, strFrom, “”, “”, strSubject, strMessage, “text”)
[/asp]
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.
[code]
[/code]
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. 😀
Cool function, but it doesn’t run on WinXP Pro. I used it on Win2003 & it ran fine but gave out errors outside of my understanding when I ran the same code on WinXP.
Is there something you can fix?
Thanks for a cool function & a cool article. 😀
Yes, there’s some problem when running this function on WinXP. However, a fix has been mentioned for that. You just didn’t read my post well enough. 😀
To run this function on WinXP, you need to un-comment all the comments, which does’t have 2 slashes(//) in them, in the function. Also, you need to include 2 lines of code in the page on which you are calling this function. These 2 lines are mentioned in the post above, the last code block. These should be the first 2 lines of the page.
If you do all this, the function should work fine on WinXP too.
If you still get a problem like invalid or unauthorised or unknown port, then simply change the port from 25 to whatever your webhost is using for SMTP.