Tutorial 2: Sending an Html Email with an Embedded Image

Dundas

Tutorial 2: Sending an Html Email with an Embedded Image

Overview | Properties | Methods


Files to be embedded are uploaded by the user via file input elements in a form (with an EncType of "multipart/form-data"). Then the Dundas Upload Control is used to save uploaded files to disk with its Save method (we could also save directly to memory as well). Once this is done we can then iterate through all uploaded files via the Upload control's Files collection and check to see if valid file types were uploaded for each file input element (lets assume that there are two file input boxes, one for an image file and the other for an audio file). If a valid file type was uploaded we add the file to the Mailer control's HtmlEmbeddedObjs collection, and then embed the object into the Mailer control's HtmlBody property. When we embed the object into the HtmlBody property we "wrap" the appropriate tags around the object to be embedded and identify it by using the contentID (cid). We then continue looping through the Files collection. Once the objects have been added to the HtmlEmbeddedObjs collection the email is sent via the SendMail method of the Mailer control.

Please note that you CAN NOT use ASP's Request object to retrieve form element values when the ENCTYPE of the form is "multipart/form-data". To retrieve form values you MUST call the Save method of the Upload control. Not only does this save the uploaded files to disk but it also populates the Upload controls collections, thereby letting you access form elements with the Upload control's Form collection (e.g. strToAddress = objUpload.Form("txtTO")).

Assumptions

  • A form with an ENCTYPE of "multipart/form-data" is POSTING data to this ASP page.

  • The form contains two (2) file input elements named "txtAudio" and "txtImage" (e.g. <input type="file" name="txtAudio">).

<% Dim objUpload 'stores an Upload control object
Dim
objEmail 'stores a Mailer control object
Dim
strPath 'stores path to a directory we create at same level as this ASP page

'The SendMail function will throw an exception if the operation is
' unsuccessful, so we enable inline error trapping

On Error Resume Next

Set objUpload = Server.CreateObject("Dundas.Upload") 'Upload object
Set
objEmail = Server.CreateObject("Dundas.Mailer") 'Mailer object

'create a directory at same level as this ASP page
' this directory is used to store the uploaded files (which are renamed with a GUID preceding

' the original filename)

'NOTE: to delete these files you would have to use the
ImpersonateUser method of
' the Upload control since the IUSR_ account SHOULD NOT have permission to delete files

strPath = Server.MapPath(".") & "\temp"
objUpload.DirectoryCreate strPath

'save the uploaded files. THIS POPULATES THE UPLOAD CONTROL'S COLLECTIONS!
objUpload.Save strPath

'specify the recipient of this message
objEmail.TOs.Add "[email protected]"

'specify the subject of the email
objEmail.Subject = "This is the subject."

'specify the sender of the message
objEmail.FromAddress = "[email protected]"

'specify an SMTP Relay server. This increases the speed and reliability of the operation
objEmail.SMTPRelayServers.Add "SomeSmtpServer.com"

'initialize the HtmlBody property, we'll throw a header into it
objEmail.HTMLBody = "<Html><Head></Head><Body><H2>This is the body.</H2><BR><BR>"

'now lets loop through the uploaded files to be embedded.
For Each
Item in objUpload.Files
'we will check to see which file input element is responsible for the uploaded file

If
(Item.TagName = "txtAudio") Then
'we now know the file came from the txtAudio box, but lets make sure that

' the user actually uploaded some sort of audio file by using InStr and the ContentType property

If
InStr(1,Item.ContentType,"audio") Then
'the file is actually an audio type, so add it to the HtmlEmbeddedObjs collection

' and embed it into the HtmlBody property. Note that we set the ContentName argument

' of the HtmlEmbeddedObj to the OriginalPath property of the uploaded file so that

' if the client email software displays the name of the file the filename will be user-friendly

'Also note that we MAKE SURE that the ID for the embedded object is unique!!

objEmail.HtmlEmbeddedObjs.Add Item.Path, 1, Item.OriginalPath
objEmail.HtmlBody = objEmail.HtmlBody & "<BGSound src=cid:1></BGSound>"
End If

End If

If
(Item.TagName = "txtImage") Then
'lets make sure user uploaded a valid image file

If
InStr(1,Item.ContentType,"image") Then
objEmail.HtmlEmbeddedObjs.Add Item.Path, 2, Item.OriginalPath
objEmail.HtmlBody = objEmail.HtmlBody & "<IMG SRC=cid:2>"
End If

End If

Next

'finish html body by adding closing html tags
objEmail.HTMLBody = objEmail.HTMLBody & "</body></html>"

'send the email
objEmail.SendMail

'test for success/failure
If
Err.Number <> 0 Then
'an error occurred so output error message

Response.Write "Sorry, the following error occurred: " & Err.Description
Else

'success!

Response.Write "The html email was successfully sent."
End If

'release resources
Set
objEmail = Nothing
Set
objUpload = Nothing
%>