[SOLVED] Dynamically adding embedded images
Posted: Mon Mar 05, 2007 6:22 am
Hi
I've been playing around with Swift over the last couple of days, and I have a question about Embedding Images.
Basically, the system I want to use Swift with will allow Admins to create newsletters online (using TinyMCE or similar) and send them to members. One of the specifications is that the images used in these mails are embeded.
At the moment, I am using regex to locate all the images and to slice up the html around them. The building the message part using all the array values. However I can't seem to get it working in a loop, so it's not very flexible at the moment.
This was just for testing, and I need to streamline it so that any number of images could be used
Is there an easier way to dynamically embed the images?
As you can see I am creating the html message part and its attachments almost statically from the array. I tried looping over the arrays and building it but this seems to just cause errors.
I looked on the Wiki, but the embedded images tutorial is in the ToDo list
I've been playing around with Swift over the last couple of days, and I have a question about Embedding Images.
Basically, the system I want to use Swift with will allow Admins to create newsletters online (using TinyMCE or similar) and send them to members. One of the specifications is that the images used in these mails are embeded.
At the moment, I am using regex to locate all the images and to slice up the html around them. The building the message part using all the array values. However I can't seem to get it working in a loop, so it's not very flexible at the moment.
This was just for testing, and I need to streamline it so that any number of images could be used
Code: Select all
<?php
$home_dir = dirname(__FILE__);
$plain_text = <<<PLAIN
You have recieved a rich media email. To view this on-line you need to go to the following link
{link}
Alternatively, you could change the settings on your mail client to display HTML.
PLAIN;
$email_template = <<<email
<html>
<head>
</head>
<body>
<table align="center" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="3"><img src="temp999/top.jpg" height="15" width="600" /></td></tr>
<tr>
<td><img src="temp999/left.jpg" height="160" width="342" /></td>
<td><img src="temp999/center.gif" height="160" width="240" /></td>
<td><img src="temp999/right.jpg" height="160" width="18" /></td>
</tr>
<tr><td colspan="3"><img src="temp999/bottom.jpg" height="225" width="600" /></td></tr>
</body>
</html>
email;
/**
* Matches all "<img src" in the HTML template
* @return array
* array[0] = Full <img /> tag
* array[1] = URL without file name inside src="" property
* array[2] = Filename from src="" proptery
*/
$image_pattern = '/<img.*?[\s]src=[\"\'](.*?)([^\/]*\.[a-zA-Z]*)[\"\'].*?>/';
preg_match_all($image_pattern, $email_template,$images);
require($home_dir . '/lib/Swift.php');
require($home_dir . '/lib/Swift/Connection/SMTP.php');
$swift =& new Swift(new Swift_Connection_SMTP('localhost'));
$email =& new Swift_Message("Test email from Swift");
$plain_part =& new Swift_Message_Part($plain_text);
/**
* Check if the collateral exists locally
* If not, then retireve from the app/db server
* If it exists then replace the src in the template
* with the embedded image/video (if the template is
* for an embeded / mixed client)
*
*/
foreach($collateral[2] as $file_index => $file_name){
if (!is_file($home_dir . '/collateral/' . $file_name)){
//get file
}
$full_src = $collateral[1][$file_index] . $file_name;
//replace image src with identifier for html slicing
$email_template = str_ireplace($full_src,'|~|',$email_template);
}
//slice html
$breakdown = explode('"|~|"',$email_template);
//this is the part i'm having problems with......
$email_part =& new Swift_Message_Part(
$breakdown[0] .
$email->attach(new Swift_Message_Image(new Swift_File($home_dir . '/collateral/' .$images[2][0]))) .
$breakdown[1] .
$email->attach(new Swift_Message_Image(new Swift_File($home_dir . '/collateral/' .$images[2][1]))) .
$breakdown[2] .
$email->attach(new Swift_Message_Image(new Swift_File($home_dir . '/collateral/' .$images[2][2]))) .
$breakdown[3] .
$breakdown[4] .
$email->attach(new Swift_Message_Image(new Swift_File($home_dir . '/collateral/' .$images[2][3]))) .
$breakdown[5] .
$email->attach(new Swift_Message_Image(new Swift_File($home_dir . '/collateral/' .$images[2][4]))) .
$breakdown[6]
,'text/html','quoted-printable');
//end
$email->attach($email_part);
if (!$swift->send($email,'{toaddress}','{fromaddress}')){
//handle send error
}
?>As you can see I am creating the html message part and its attachments almost statically from the array. I tried looping over the arrays and building it but this seems to just cause errors.
I looked on the Wiki, but the embedded images tutorial is in the ToDo list