Sample code for 30+ languages & platforms
PureBasic

Send HTML Email with Image Downloaded from URL

Demonstrates how to compose an HTML email with an embedded image where the image data is downloaded from a URL.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkHttp.pb"
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.

    ; First download the image we'll be adding to the HTML "img" tag.
    bdJpg.i = CkBinData::ckCreate()
    If bdJpg.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    success = CkHttp::ckQuickGetBd(http,"https://www.chilkatsoft.com/images/starfish.jpg",bdJpg)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkBinData::ckDispose(bdJpg)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

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

    ; Use your SMTP server..
    CkMailMan::setCkSmtpHost(mailman, "smtp.yourserver.com")
    CkMailMan::setCkSmtpPort(mailman, 587)
    CkMailMan::setCkStartTLS(mailman, 1)

    ; Set the SMTP login/password
    CkMailMan::setCkSmtpUsername(mailman, "my_login")
    CkMailMan::setCkSmtpPassword(mailman, "my_password")

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

    CkEmail::setCkSubject(email, "HTML Email with Image")
    CkEmail::setCkFrom(email, "Dave <somebody@mydomain.com>")
    CkEmail::ckAddTo(email,"Chilkat","info@chilkatsoft.com")

    html.s = "<html><body><p>This is an HTML email with an embedded image.</p><p><img src=" + Chr(34) + "starfish.jpg" + Chr(34) + " /></p></body></html>"
    CkEmail::ckSetHtmlBody(email,html)

    ; Note: The "starfish.jpg" here must match the name in the "img" tag's "src" attribute in the HTML above.
    success = CkEmail::ckAddRelatedBd2(email,bdJpg,"starfish.jpg")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkBinData::ckDispose(bdJpg)
        CkHttp::ckDispose(http)
        CkMailMan::ckDispose(mailman)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    CkEmail::ckSaveEml(email,"qa_output/out.eml")

    ; success = mailman.SendEmail(email);
    ; if (success == ckfalse) {
    ;     println mailman.LastErrorText;
    ;     return;
    ; }
    ; 
    ; ignore = mailman.CloseSmtpConnection();

    Debug "Success."


    CkBinData::ckDispose(bdJpg)
    CkHttp::ckDispose(http)
    CkMailMan::ckDispose(mailman)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure