Below I have a PHP script that works perfectly. The problem starts when I set the variable $body_text to read the HTML code from the Db and the picture is no longer attached but set <img src exactly to the Swift function call. The script runs every five minutes to send emails where the HTML code is read from the Db. Any help.
<?php
require_once "../../include/lib/Swift.php";
require_once "../../include/lib/Swift/Connection/SMTP.php";
$smtp = new Swift_Connection_SMTP("mail.mymail.co.za", 25);
$swift = new Swift($smtp);
$message = new Swift_Message("My Swift Test");
$path = "C:/Data/pics/";
$body_text =
"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title>Test Swift</title>
</head>
<body>
<p>This is my test picture <img src='" . $message->attach(new Swift_Message_Image(new Swift_File($path
. "mypic.jpg"))) . "'> that does not show when the HTML code is read from the Db.</p>
</body>
</html>";
$sname = "My Company";
$webmaster = "webmaster@mymail.co.za");
$email = "me@mymail.co.za";
$message->attach(new Swift_Message_Part($body_text, "text/html"));
if($swift->send($message,new Swift_Address($email),new Swift_Address($webmaster, $sname)))
echo "email sent to: $email.";
else
echo "failed email sending to: $email.";
?>
Embedding the image
Moderators: Chris Corbyn, General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Embedding the image
I'm not clear what you're trying to do? Do you need to store the email template in a database? In that case you can't include embedded PHP code, unless you're happy to eval() the code, which I wouldn't advise.
Perhaps you could have some sort of a place holder in the text and add the image later?
Perhaps you could have some sort of a place holder in the text and add the image later?
Re: Embedding the image
Thanks for your help Chris. Yes we have 28 HTML templates where anyone of them can have up to ten images. When a user does something on our Web site, a relevant template is chosen and a confirmation email sent. We don’t send emails directly on the Web page/script but let another script handle the email sending.
Re: Embedding the image
Because the templates are different and the images are many (can also be dynamic), what is the best way of attaching/embedding these images using Swift? Tshwarelo.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Embedding the image
I'd suggest the FileEmbedder plugin but it has been known not to work for some people.
Perhaps you can write your markup something like:
Then when you get the HTML from the database, use a regular expression to find all the {{img .. }} parts of the code and replace them. Without writing it for you, something like:
Perhaps you can write your markup something like:
Code: Select all
<h2>Some thing</h2> Blargh, whatever {{img /path/to/some/image.jpg}} <strong>more blargh</strong>Code: Select all
class ImageAdder {
public $message;
public function addImageToMessage($matches) {
return '<img src="' . $this->message->attach(new Swift_Message_Image(new Swift_File($matches[1]))) . '" />';
}
}
$message = new Swift_Message($subject);
$adder = new ImageAdder();
$adder->message = $message;
$html = get_the_html_from_db();
$html_with_images = preg_replace_callback('~\{\{\ (.*?)\}\}~', array($adder, 'addImageToMessage'), $html);
$message->attach(new Swift_Message_Part($html_with_images, 'text/html'));
Re: Embedding the image
Hey Chris, you are a life saver! This technique worked perfectly. Thanks once again. 
Re: Embedding the image
curious technique
will be usefull !
will be usefull !