Sample code for 30+ languages & platforms
PureBasic

Count the DKIM Signatures in a Message

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.NumDkimSigs method, which returns the number of DKIM-Signature header fields in a MIME message. The only argument is a BinData holding the complete MIME.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: A message can legitimately carry several DKIM signatures — the original sender's plus ones added by forwarders or mailing lists — so a verifier needs to know how many there are before checking each. This count is what supplies the valid index range for DkimVerify. Note that it counts header fields by name only, without validating them, so a malformed DKIM-Signature field is still included in the total.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkDkim.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Dkim.NumDkimSigs method, which returns the number of DKIM-Signature header
    ;  fields in a MIME message.  The only argument is a BinData holding the complete MIME.

    dkim.i = CkDkim::ckCreate()
    If dkim.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Load the complete MIME message.
    mimeData.i = CkBinData::ckCreate()
    If mimeData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(mimeData,"qa_data/received.eml")
    If success = 0
        Debug CkBinData::ckLastErrorText(mimeData)
        CkDkim::ckDispose(dkim)
        CkBinData::ckDispose(mimeData)
        ProcedureReturn
    EndIf

    ;  Count the DKIM-Signature header fields.  The count includes malformed signatures, since the
    ;  fields are counted without validating their syntax.
    numSigs.i = CkDkim::ckNumDkimSigs(dkim,mimeData)
    Debug "The message has " + Str(numSigs) + " DKIM-Signature header(s)."


    CkDkim::ckDispose(dkim)
    CkBinData::ckDispose(mimeData)


    ProcedureReturn
EndProcedure