Perl Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript



Perl Examples

Quick Start
Perl Unicode
Perl Byte Array
Perl Certs
Perl Email
Perl Encryption
Perl FTP
HTML-to-XML
Perl HTTP
Perl IMAP
Perl MHT
Perl MIME
Perl RSA
Perl S/MIME
Perl Signatures
Perl Socket
Perl Spider
Perl Tar
Perl Upload
Perl XML
Perl XMP
Perl Zip

More Examples...
String
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

Dynamically Generate HTML Email

This Perl script builds an HTML email with embedded GIF images and CSS style sheet without the assistance of Chilkat MHT. It demonstrates how to use CID links to reference embedded content within the email.

Download Perl Programming Example Scripts

# file: BuildHtmlEmail.pl

# Perl example script to dynamically generate an HTML email 
# with embedded images and CSS style sheet and send.  	 

use chilkat;

$mailman = new chilkat::CkMailMan();
$mailman->UnlockComponent('anything for 30-day trial');

# Set the SMTP server hostname
$mailman->put_SmtpHost('smtp.comcast.net');

# If your SMTP server requires a login, set username/password
# $mailman->put_SmtpUsername('myUsername');
# $mailman->put_SmtpPassword('myPassword');

# Create a new email object.
$email = new chilkat::CkEmail();

# Images that are embedded within an HTML email are reference by "CID" links.
# For example, an HTML IMG tag has a CID URL for the SRC attribute:
# <img src="CID:abc123">
# When creating an HTML email with embedded images using Chilkat, you first add
# each image to the email.  Embedded images (and style sheets) are considered
# "related" items.  They are not attachments and are not listed as files attached
# to an email because they are considered to be part of the HTML body.

# This email will need 5 related items (4 images and 1 style sheet)
$cid1 = new chilkat::CkString();
$cid2 = new chilkat::CkString();
$cid3 = new chilkat::CkString();
$cid4 = new chilkat::CkString();
$cid5 = new chilkat::CkString();

$css = 'table#tscr {' .
'	border: 1px dashed;' .
'	margin: 8px;' .
'}' .
'table#tscr td {' .
'	font-family: Verdana;' .
'	font-size: 9pt;' .
'}';

# If the file on disk could not be found, or there were file permission issues,
# a false value is returned.  The CID is returned in the 2nd argument.
# This example will not check the return values...
$b1 = $email->AddRelatedFile('images/dudeJava.gif',$cid1);
$b2 = $email->AddRelatedFile('images/dudePython.gif',$cid2);
$b3 = $email->AddRelatedFile('images/dudePerl.gif',$cid3);
$b4 = $email->AddRelatedFile('images/dudeRuby.gif',$cid4);
		
# Our style sheet will be added from an in-memory string.
# The 1st argument is the "name" within the related part assigned to 
# the related part.  It should be any filename such that the file extension
# indicates the content-type (i.e. .gif, .jpg, .css, etc.)  It is not
# a file that exists on disk.  The charset indicates the charset to use
# for the text within the email.  
$email->AddRelatedString('sample.css',$css,'iso-8859-1',$cid5);

# Now that the CIDs are known, the HTML can be formed:
$html = '<html>' .
'<head>' .
'<link rel="stylesheet" type="text/css" href="CID:' . $cid5->getString() . '" />' .
'</head>' .
'<body>' .
'<table id="tscr" cellpadding="0" cellspacing="10">' .
'<tbody><tr> ' .
'<td colspan="4"><b>NEW:</b> Chilkat for Perl and other Scripting Languages</td>' .
'</tr><tr>' .
'<td><a href="http://www.chilkatsoft.com/java.asp">Java</a><br><a href="http://www.chilkatsoft.com/java.asp"><img src="CID:' . $cid1->getString() . '" border="0"></a></td>' .
'<td><a href="http://www.chilkatsoft.com/perl.asp">Perl</a><br><a href="http://www.chilkatsoft.com/perl.asp"><img src="CID:' . $cid3->getString() . '" border="0"></a></td>' .
'<td><a href="http://www.chilkatsoft.com/python.asp">Python</a><br><a href="http://www.chilkatsoft.com/python.asp"><img src="CID:' . $cid2->getString() . '" border="0"></a></td>' .
'<td><a href="http://www.chilkatsoft.com/ruby.asp">Ruby</a><br><a href="http://www.chilkatsoft.com/ruby.asp"><img src="CID:' . $cid4->getString() . '" border="0"></a></td>' .
'</tr>' .
'</tbody></table>' .
'</body>' .
'</html>';
	
# Add the HTML body.
$email->SetHtmlBody($html);
	    
$email->put_Subject('Dynamically generated HTML email');
$email->put_From('Chilkat Support <support@chilkatsoft.com>');

# Add a few recipients
$email->AddTo('Matt','matt@chilkatsoft.com');
$email->AddTo('TagTooga','admin@tagtooga.com');
   
# This line is not necessary.  It simply saves the email as a .eml 
# allowing you to examine the MIME source of the email in any text editor.
$email->SaveEml("test.eml");
    
# Send the HTML e-mail.
$success = $mailman->SendEmail($email);
if (! $success)
    {
	$mailman->SaveLastError('lastError.txt');	
    }






 

Need a specific example? Send a request to support@chilkatsoft.com

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