PHP Requires Chilkat v11.0.0+
PHP
Add an attachment and an inline image
See more Email Object Examples
Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.
Chilkat PHP Downloads
<?php
use Chilkat2\BinData;
use Chilkat2\Email;
use Chilkat2\StringBuilder;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$email = new Email();
try {
$email->LoadEml('text-and-html.eml');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$data = new BinData();
try {
$data->AppendString("Time,Activity\r\n09:00,Welcome\r\n10:00,Field session\r\n", 'utf-8');
} catch (\Chilkat2\Exception $e) {
echo "Failed: data.AppendString.\n";
exit;
}
try {
$email->AddAttachmentBd('agenda.csv', $data, 'text/csv');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// AddRelatedBd returns the identifier that must appear after cid: in the HTML.
$image = new BinData();
try {
$image->AppendEncoded('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==', 'base64');
} catch (\Chilkat2\Exception $e) {
echo "Failed: image.AppendEncoded.\n";
exit;
}
try {
$cid = $email->AddRelatedBd('confirmed.png', $image);
$html = new StringBuilder();
$html->Append('<p><img alt=\'Confirmed\' src="cid:');
$html->Append($cid);
$html->Append('"></p>');
$html->Append($email->GetHtmlBody());
$email->AddHtmlAlternativeBody($html->GetAsString());
$email->SaveEml('with-attachments.eml');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo "Saved with-attachments.eml.\n";