Sample code for 30+ languages & platforms
PureBasic

Embed Image in HTML Email

Demonstrates how to create and send an HTML email with an embedded image.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; The mailman object is used for sending and receiving email.
    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Set the SMTP server.
    CkMailMan::setCkSmtpHost(mailman, "smtp.comcast.net")

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

    ; Add an embedded image to the HTML email.
    fileOnDisk.s = "images/dude2.gif"
    filePathInHtml.s = "dudeAbc.gif"

    ; Embed the GIF image in the email.
    success = CkEmail::ckAddRelatedFile2(email,fileOnDisk,filePathInHtml)
    If success <> 1
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ; The src attribute for the image tag is set to the filePathInHtml:
    htmlBody.s = "<html><body>Embedded Image:<br><img src=" + Chr(34) + "dudeAbc.gif" + Chr(34) + "></body></html>"

    ; Set the basic email stuff: HTML body, subject, "from", "to"
    CkEmail::ckSetHtmlBody(email,htmlBody)
    CkEmail::setCkSubject(email, "PureBasic HTML email with an embedded image.")
    success = CkEmail::ckAddTo(email,"Admin","admin@chilkatsoft.com")
    CkEmail::setCkFrom(email, "Chilkat Support <support@chilkatsoft.com>")

    success = CkMailMan::ckSendEmail(mailman,email)
    If success <> 1
        Debug CkMailMan::ckLastErrorText(mailman)
    Else
        Debug "Mail Sent!"
    EndIf



    CkMailMan::ckDispose(mailman)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure