Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Swift 3,4,5... Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip

HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
PDF Signatures
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Swift 3,4,5...) SendGrid HTML Email with Embedded Images

See more SendGrid Examples

Demonstrates how to send an HTML email with embedded images using SendGrid.

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

func chilkatTest() {
    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code

    let req = CkoHttpRequest()!
    let http = CkoHttp()!

    var success: Bool

    // First.. load a JPG file and build an HTML img tag with the JPG data inline encoded.
    // Our HTML img tag will look like this:
    // <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA3ADcAA..." alt="" style="border-width:0px;" />
    let bdJpg = CkoBinData()!
    success = bdJpg.loadFile("qa_data/jpg/starfish.jpg")
    if success == false {
        print("Failed to load JPG file.")
        return
    }

    // Note: HTML containing embedded base64 image data may not be visible in all email clients.
    // For example, GMail might not display the image, but viewing in Outlook might be OK.
    let sbHtml = CkoStringBuilder()!
    sbHtml.append("<html><body><b>This is a test<b><br /><img src=\"data:image/jpeg;base64,")
    // Append the base64 image data to sbHtml.
    bdJpg.getEncodedSb("base64", sb: sbHtml)
    sbHtml.append("\" alt=\"\" style=\"border-width:0px;\" /></body></html>")

    let json = CkoJsonObject()!
    json.update("personalizations[0].to[0].email", value: "matt@chilkat.io")
    json.update("from.email", value: "admin@chilkatsoft.com")
    json.update("subject", value: "Test HTML email with images")
    json.update("content[0].type", value: "text/html")
    json.updateSb("content[0].value", sb: sbHtml)

    http.authToken = "SENDGRID_API_KEY"

    var resp: CkoHttpResponse?
    resp = http.postJson3("https://api.sendgrid.com/v3/mail/send", contentType: "application/json", json: json)
    if http.lastMethodSuccess != true {
        print("\(http.lastErrorText!)")
    }
    else {
        print("response status code: \(resp!.statusCode.intValue)")
        // Display the response.
        // If successful, the response code is 202 and the response body string is empty.
        // (The response body string may also be empty for error response codes.)
        print("\(resp!.bodyStr!)")
        resp = nil
    }


}

 

© 2000-2021 Chilkat Software, Inc. All Rights Reserved.