PureBasic
PureBasic
Extract Linked Domains from an HTML Email
See more Email Object Examples
Demonstrates the Chilkat Email.LinkedDomains method, which extracts the domain names from the hyperlinks in an HTML email. Lowercase domains are appended to a StringTable without creating duplicates. This example sets an HTML body with several links and lists the distinct domains.
Background: The set of domains a message links to is a strong signal for spam and phishing analysis — legitimate mail usually points at a few expected domains, while abusive mail often hides links to many unrelated or look-alike hosts.
LinkedDomains gives you that de-duplicated list directly. Note it only parses the links; it does not fetch them, so no network request is made.Chilkat PureBasic Downloads
IncludeFile "CkEmail.pb"
IncludeFile "CkStringTable.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the LinkedDomains method, which extracts the domain names from the
; hyperlinks in an HTML email. Lowercase domains are appended to a StringTable without
; creating duplicates.
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email, "LinkedDomains example")
; An HTML body containing several hyperlinks.
CkEmail::ckSetHtmlBody(email,"<html><body>Visit <a href=" + Chr(34) + "https://www.example.com/page" + Chr(34) + ">Example</a>, <a href=" + Chr(34) + "http://sales.contoso.com" + Chr(34) + ">Contoso</a>, and <a href=" + Chr(34) + "https://www.example.com/other" + Chr(34) + ">Example again</a>.</body></html>")
; Extract the linked domains into a StringTable.
domains.i = CkStringTable::ckCreate()
If domains.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkEmail::ckLinkedDomains(email,domains)
If success = 0
Debug CkEmail::ckLastErrorText(email)
CkEmail::ckDispose(email)
CkStringTable::ckDispose(domains)
ProcedureReturn
EndIf
n.i = CkStringTable::ckCount(domains)
i.i
For i = 0 To n - 1
Debug CkStringTable::ckStringAt(domains,i)
Next
CkEmail::ckDispose(email)
CkStringTable::ckDispose(domains)
ProcedureReturn
EndProcedure