Sample code for 30+ languages & platforms
PureBasic

Remove the Plain-Text Body from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemovePlainTextAlternative method, which removes the plain-text body from the email if one exists. Other body representations, attachments, and related items remain unchanged. This example builds a message with both plain-text and HTML alternatives, then removes the plain text.

Background: This is the counterpart to RemoveHtmlAlternative. Removing the plain-text alternative leaves an HTML-only message. Note that dropping the plain-text fallback is generally discouraged for real mail — it hurts accessibility and can raise spam scores — but it is occasionally needed when a downstream system expects a single HTML representation.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    ;  Demonstrates the RemovePlainTextAlternative method, which removes the plain-text body
    ;  from the email (if one exists).  Other body representations, attachments, and related
    ;  items remain.

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

    CkEmail::setCkSubject(email, "Remove plain-text alternative")

    ;  Create an email with both plain-text and HTML alternatives.
    CkEmail::ckSetTextBody(email,"This is the plain-text version.","text/plain")
    CkEmail::ckAddHtmlAlternativeBody(email,"<html><body>This is the HTML version.</body></html>")
    Debug "NumAlternatives before = " + Str(CkEmail::ckNumAlternatives(email))
    Debug "HasPlainTextBody before: " + Str(CkEmail::ckHasPlainTextBody(email))

    ;  Remove the plain-text body, leaving the HTML alternative.
    CkEmail::ckRemovePlainTextAlternative(email)
    Debug "NumAlternatives after = " + Str(CkEmail::ckNumAlternatives(email))
    Debug "HasPlainTextBody after: " + Str(CkEmail::ckHasPlainTextBody(email))


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure