PureBasic
PureBasic
Send HTML Email with External CSS as Related Item
See more SMTP Examples
Demonstrates how to compose an HTML email with an external CSS file included as a related item and referenced by CID (Content-ID).Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.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.
; 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
; Use your SMTP server hostname. This example uses office365, but it could be any SMTP server..
CkMailMan::setCkSmtpHost(mailman, "outlook.office365.com")
CkMailMan::setCkSmtpPort(mailman, 587)
CkMailMan::setCkStartTLS(mailman, 1)
; Set the SMTP login/password
CkMailMan::setCkSmtpUsername(mailman, "OFFICE365-SMTP-LOGIN")
CkMailMan::setCkSmtpPassword(mailman, "OFFICE365-SMTP-PASSWORD")
; Create a new email object
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email, "HTML Email with embedded CSS")
CkEmail::setCkFrom(email, "Chilkat Support <my-office365-user@mydomain.com>")
CkEmail::ckAddTo(email,"Chilkat Support","support@chilkatsoft.com")
sbCss.i = CkStringBuilder::ckCreate()
If sbCss.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bCrlf.i = 1
CkStringBuilder::ckAppendLine(sbCss,"body {",bCrlf)
CkStringBuilder::ckAppendLine(sbCss," background-color: powderblue;",bCrlf)
CkStringBuilder::ckAppendLine(sbCss,"}",bCrlf)
CkStringBuilder::ckAppendLine(sbCss,"h1 {",bCrlf)
CkStringBuilder::ckAppendLine(sbCss," color: blue;",bCrlf)
CkStringBuilder::ckAppendLine(sbCss,"}",bCrlf)
CkStringBuilder::ckAppendLine(sbCss,"p {",bCrlf)
CkStringBuilder::ckAppendLine(sbCss," color: red;",bCrlf)
CkStringBuilder::ckAppendLine(sbCss,"}",bCrlf)
; It's possible to add a CSS file directly by calling AddRelatedFile.
; This example will add the CSS from a string.
filenameInHtml.s = "styles.css"
contentIdCss.s = CkEmail::ckAddRelatedString(email,filenameInHtml,CkStringBuilder::ckGetAsString(sbCss),"utf-8")
If CkEmail::ckLastMethodSuccess(email) <> 1
Debug CkEmail::ckLastErrorText(email)
CkMailMan::ckDispose(mailman)
CkEmail::ckDispose(email)
CkStringBuilder::ckDispose(sbCss)
ProcedureReturn
EndIf
; The src attribute for the image tag is set to the contentIdCss:
sbHtml.i = CkStringBuilder::ckCreate()
If sbHtml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppendLine(sbHtml,"<!DOCTYPE html>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"<html>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"<head>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <link rel=" + Chr(34) + "stylesheet" + Chr(34) + " href=" + Chr(34) + "cid:CONTENT_ID_CSS" + Chr(34) + ">",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"</head>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"<body>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"<h1>This is a heading</h1>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"<p>This is a paragraph.</p>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"</body>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"</html>",bCrlf)
numReplacements.i = CkStringBuilder::ckReplace(sbHtml,"CONTENT_ID_CSS",contentIdCss)
CkEmail::ckSetHtmlBody(email,CkStringBuilder::ckGetAsString(sbHtml))
success = CkMailMan::ckSendEmail(mailman,email)
If success <> 1
Debug CkMailMan::ckLastErrorText(mailman)
Else
Debug "Mail Sent!"
EndIf
CkMailMan::ckDispose(mailman)
CkEmail::ckDispose(email)
CkStringBuilder::ckDispose(sbCss)
CkStringBuilder::ckDispose(sbHtml)
ProcedureReturn
EndProcedure