PureBasic
PureBasic
Add a Related Item from a BinData Object
See more Email Object Examples
Demonstrates the Chilkat Email.AddRelatedBd method, which adds a related item (such as an inline image) using the contents of a BinData object, and returns the generated Content-ID. This example loads an image into a BinData, adds it, captures the Content-ID, and references it from the HTML body.
Background: This is the binary,
Content-ID-based way to embed an inline resource — the right choice for images, whose bytes belong in a BinData rather than a string. Because Chilkat generates the Content-ID, the usual pattern is: add the item first, capture the returned ID, then build the matching <img src="cid:..."> reference from it (done here with a StringBuilder).Chilkat PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkEmail.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the AddRelatedBd method, which adds a related item (such as an inline image)
; using the contents of a BinData object, and returns the generated Content-ID.
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email, "Related image from BinData")
; Load the image data from a file into a BinData object.
bdImage.i = CkBinData::ckCreate()
If bdImage.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(bdImage,"qa_data/images/logo.png")
If success = 0
Debug CkBinData::ckLastErrorText(bdImage)
CkEmail::ckDispose(email)
CkBinData::ckDispose(bdImage)
ProcedureReturn
EndIf
; Add the image as a related item; capture its generated Content-ID.
cid.s = CkEmail::ckAddRelatedBd(email,"logo.png",bdImage)
If CkEmail::ckLastMethodSuccess(email) = 0
Debug CkEmail::ckLastErrorText(email)
CkEmail::ckDispose(email)
CkBinData::ckDispose(bdImage)
ProcedureReturn
EndIf
; Reference the related item in the HTML body by its Content-ID.
sbHtml.i = CkStringBuilder::ckCreate()
If sbHtml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbHtml,"<html><body><img src=" + Chr(34) + "cid:PLACEHOLDER_CID" + Chr(34) + "/></body></html>")
numReplaced.i = CkStringBuilder::ckReplace(sbHtml,"PLACEHOLDER_CID",cid)
CkEmail::ckSetHtmlBody(email,CkStringBuilder::ckGetAsString(sbHtml))
Debug "NumRelatedItems = " + Str(CkEmail::ckNumRelatedItems(email))
; Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
; relative to the current working directory of the running application.
CkEmail::ckDispose(email)
CkBinData::ckDispose(bdImage)
CkStringBuilder::ckDispose(sbHtml)
ProcedureReturn
EndProcedure