Simple Send Email Function for Classic ASP using CDO
Here is a simple function you can use to email via CDO from Classic ASP. Just create an include file with the following code and use a one line function call anywhere you want to send an email from.
Works with attachments, html or text.
Here is the include file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | smtp_mail_server="YOUR MAIL SERVER" function xsendmail (xto,xbcc,xfrom,xsubject,xhtml,xbody,xatt) set imsg = createobject("cdo.message") set iconf = createobject("cdo.configuration") Set Flds = iConf.Fields With Flds 'Set to 1 if you have SMTP on the webserver, else set to 2 to use another SMTP server .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'If you need SSL uncomment following line '.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = "true" 'SMTP Address set above .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtp_mail_server 'If your smtp server requires authentication, then set the following to 1, else comment out next 3 lines .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YOUR EMAIL USER NAME" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YOUR EMAIL PASSWORD" .Update End With With iMsg Set .Configuration = iConf .To = xto if xbcc <> "" then .BCC = xbcc end if .From = xfrom .Subject = xsubject if (xhtml) then .HTMLbody = xbody else .TEXTBody = xbody end if .fields.update .Send End With set iconf = nothing set imsg = nothing end function |
Now, from any page that you include the above file in, you can call the following code:
1 2 3 4 5 6 7 8 9 10 11 | to_name="Fred Flintstone" to_email="fred@bedrock.com" from_name="Barney Rubble" bcc="wilma@bedrock.com" 'optional from_email="barney@thequary.com" subject="Bowling" body="Hello, this is a test email." html=0 '0 for text or 1 for html att_name="" ' include file location on server if you want to add an attachment xsendmail to_name & "<" & to_email & ">",bcc,from_name & "<" & from_email & ">" ,subject,html,body,att_name |
Calling the UPS ratefinder API in Classic ASP
Last week I started this blog with some quick and dirty code for calling the rate finder API from the USPS. As with most API's, Classic ASP samples are hard to come by these days. So here is a sample of the UPS code. Remember, you have to have a web tools account with UPS which you can get here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | <% ship_to_zip=33323 weight=5 package_length=6 package_width=6 package_height=6 residential=true xml_request= "<?xml version='1.0'?>" xml_request = xml_request & "<accessRequest xml:lang='en-US'>" xml_request = xml_request & "<accessLicenseNumber>YOUR_ACCESS_LICENSE_NUMBER</accessLicenseNumber>" xml_request = xml_request & "<userId>YOUR_USER_NAME</userId>" xml_request = xml_request & "<password>YOUR_PASSWORD</password>" xml_request = xml_request & "</accessRequest>" xml_request = xml_request & "<?xml version='1.0'?>" xml_request = xml_request & "<ratingServiceSelectionRequest xml:lang='en-US'>" xml_request = xml_request & "<request>" xml_request = xml_request & "<transactionReference>" xml_request = xml_request & "<customerContext>Rating and Service</customerContext>" xml_request = xml_request & "<xpciVersion>1.0001</xpciVersion>" xml_request = xml_request & "</transactionReference>" xml_request = xml_request & "<requestAction>Rate</requestAction>" xml_request = xml_request & "<requestOption>Rate</requestOption>" xml_request = xml_request & "</request>" xml_request = xml_request & "<pickupType>" xml_request = xml_request & "<code>01</code>" xml_request = xml_request & "</pickupType>" xml_request = xml_request & "<shipment>" xml_request = xml_request & "<shipper>" xml_request = xml_request & "<shipperNumber>YOUR_SHIPPER_NUMBER</shipperNumber>" xml_request = xml_request & "<address>" xml_request = xml_request & "<postalCode>YOUR_POSTAL_CODE</postalCode>" xml_request = xml_request & "</address>" xml_request = xml_request & "</shipper>" xml_request = xml_request & "<shipTo>" xml_request = xml_request & "<address>" xml_request = xml_request & "<postalCode>" & ship_to_zip & "</postalCode>" if (residential) then xml_request = xml_request & "<residentialAddressIndicator/>" end if xml_request = xml_request & "</address>" xml_request = xml_request & "</shipTo>" xml_request = xml_request & "<service>" xml_request = xml_request & "<code>" & ups_code & "</code>" xml_request = xml_request & "</service>" xml_request = xml_request & "<package>" xml_request = xml_request & "<packagingType>" xml_request = xml_request & "<code>02</code>" xml_request = xml_request & "<description>Package</description>" xml_request = xml_request & "</packagingType>" xml_request = xml_request & "<description>Rate Shopping</description>" xml_request = xml_request & "<packageWeight>" xml_request = xml_request & "<weight>" & weight & "</weight>" xml_request = xml_request & "</packageWeight>" xml_request = xml_request & "<dimensions>" xml_request = xml_request & "<length>" & package_length & "</length>" xml_request = xml_request & "<width>" & package_width & "</width>" xml_request = xml_request & "<height>" & package_height & "</height>" xml_request = xml_request & "</dimensions>" xml_request = xml_request & "</package>" xml_request = xml_request & "<shipmentServiceOptions/>" xml_request = xml_request & "</shipment>" xml_request = xml_request & "</ratingServiceSelectionRequest>" 'uncomment to see response in case of error 'response.write xml_request URLToRSS = "https://wwwcie.ups.com/ups.app/xml/Rate" Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0") ' resolve, connect, send, receive - in milliseconds xmlHttp.setTimeouts 10000, 60000, 10000, 10000 xmlHttp.Open "Post", URLToRSS, false xmlHttp.Send(xml_request) set RSSXML = xmlHttp.ResponseXML Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument.3.0") xmlDOM.async = false xmlDOM.setProperty "SelectionLanguage", "XPath" xmlDOM.Load(RSSXML) if xmlDOM.parseError.errorCode <> 0 then response.write "DOM Not Loaded" end if 'check for error xmlQuery = "*/Error" set errorNode = xmlDOM.documentElement.selectNodes(xmlQuery) if not errorNode is nothing then errorLength = errorNode.length if errorLength > 0 then rate_error_severity= errorNode.item(0).selectSingleNode("ErrorSeverity").nodeTypedValue rate_error_code= errorNode.item(0).selectSingleNode("ErrorCode").nodeTypedValue rate_error_description= errorNode.item(0).selectSingleNode("ErrorDescription").nodeTypedValue end if end if if rate_error_severity="Hard" then total= 0 rate_error= rate_error_description 'response.end() else rate_error="" xmlQuery = "/RatingServiceSelectionResponse/RatedShipment" set docHeadlines = xmlDOM.documentElement.selectNodes(xmlQuery) total= docHeadlines.item(0).selectSingleNode("TotalCharges").selectSingleNode("MonetaryValue").nodeTypedValue GuaranteedDaysToDelivery= docHeadlines.item(0).selectSingleNode("GuaranteedDaysToDelivery").nodeTypedValue ScheduledDeliveryTime= docHeadlines.item(0).selectSingleNode("ScheduledDeliveryTime").nodeTypedValue end if %> |
Calling the USPS API via Classic ASP
For years I have been thinking about writing an asp interface for the UPS and USPS rate finder API. But since I had a com component installed on the one server I used it on, I always put it off and used the component. As luck would have it, one Sunday a few weeks ago (it's always a Sunday) the component decided to return errors from UPS. Of course, the component writer is long gone, so it was time to write the interface I had been contemplating for years. Now mind you, this was on a very busy ecommerce site, so I was in a rush.
Surprisingly, I had some asp code up and running quickly. Here is the simple code in ASP that calls the USPS API and reads the results. This can be plugged in easily into any code that needs the USPS rate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <% 'needs the following variables weight=1 ship_to_zip=33323 package_width=6 package_length=6 'package_height=6 if int(weight) <> weight then weight=int(weight) + 1 end if if package_width < 6 then package_width=6 if package_length < 6 then package_length=6 if package_height < 6 then package_height=6 xml_request= "<?xml version='1.0'?>" xml_request = xml_request & "<rateV3Request USERID='YOUR_USERID'>" if usps_service="" then usps_service="PRIORITY" end if xml_request = xml_request & "<package ID='1ST'>" xml_request = xml_request & "<service>" & usps_service & "</service>" xml_request = xml_request & "<zipOrigination>33323</zipOrigination>" xml_request = xml_request & "<zipDestination>" & ship_to_zip & "</zipDestination>" xml_request = xml_request & "<pounds>" & weight & "</pounds>" xml_request = xml_request & "<ounces>0</ounces>" xml_request = xml_request & "<container></container>" xml_request = xml_request & "<size>REGULAR</size>" xml_request = xml_request & "<width>" & package_width & "</width>" xml_request = xml_request & "<length>" & package_length & "</length>" xml_request = xml_request & "<height>" & package_height & "</height>" 'xml_request = xml_request & "<girth>55</girth>" if usps_service="PARCEL POST" then xml_request = xml_request & "<machinable>true</machinable>" end if xml_request = xml_request & "</package>" xml_request = xml_request & "</rateV3Request>" URLToRSS = "http://Production.ShippingAPIs.com/ShippingAPI.dll" Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0") xmlHttp.Open "Post", URLToRSS, false xmlHttp.Send("API=RateV3&XML=" & xml_request) 'uncomment below to see what USPS API is returning 'Response.write xmlHttp.ResponseTEXT Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument.3.0") xmlDOM.async = false xmlDOM.setProperty "SelectionLanguage", "XPath" xmlDOM.LoadXML xmlHttp.ResponseTEXT if xmlDOM.parseError.errorCode <> 0 then response.write "DOM Not Loaded" end if xmlQuery = "/RateV3Response/Package/Postage" set docHeadlines = xmlDOM.documentElement.selectNodes(xmlQuery) rate_error="" total=0 total= docHeadlines.item(0).selectSingleNode("Rate").nodeTypedValue 'returns total with rate %> |