Page 1 of 2
[SOLVED] HTML and images
Posted: Tue Aug 07, 2007 3:53 am
by roscoe
I know I started this thread before, but I cannot find it so apologies.
I need to send HTML newsletters which have images in.
As I see it, I have 2 options:
an attachment (will it show the images? and come out as a HTML email?)
or save the HTML source in my mail out DB and somehow include the images.
My brain cannot seem to get around this one. I need to know
1) how to store the HTML email with images
2) how to get swiftmailer to send it out
Many Thanks
Posted: Tue Aug 07, 2007 11:49 am
by Chris Corbyn
It's advanced, but create the HTML, manually setting the Content-ID from the images:
Code: Select all
$html = <<<HERE
<img src="cid:img1" alt="whatever" /> and <img src="cid:img2" alt="whatever again" />
HERE;
Store that in the database somewhere. Now you'll need a table which maps "cid" values to actual images on disk:
Code: Select all
cid path
--------------------
img1 /some/image.jpg
img2 /another/image.jpg
The when you send, set the HTML body:
Code: Select all
$message->attach(new Swift_Message_Part($html_from_db, "text/html"));
Then attach all the images:
Code: Select all
$img1 = new Swift_Message_Image(new Swift_File($path1_from_db));
$img1->setContentId($cid1_from_db);
$message->attach($img1);
$img2 = new Swift_Message_Image(new Swift_File($path2_from_db));
$img2->setContentId($cid2_from_db);
$message->attach($img2);
Voila
EDIT | There are other ways, but effectively if you know the cid: (content-id) value then you can manually apply the image.
Posted: Tue Aug 07, 2007 12:13 pm
by roscoe
I was hoping for an unmanual way of doing this

Posted: Tue Aug 07, 2007 12:58 pm
by Chris Corbyn
Posted: Wed Aug 08, 2007 4:27 am
by roscoe
OK I have had a read of the file embedder and have some Qs.
Code: Select all
$plugin->setTagDefinition("tagname", array("src", "href"), array("ext1", "ext2", "ext3"));
do I assume this would work:
Code: Select all
$plugin->setTagDefinition("tagname", array("src", "href","background"), array(".gif", ".html", ".jpg"));
- I assume this tells swift what file extensions to accept?
so it should read
Code: Select all
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
require_once "lib/Swift/Plugin/FileEmbedder.php";
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
//Attach the plugin
$swift->attachPlugin(new Swift_Plugin_FileEmbedder(), "file_embedder");
$plugin->setTagDefinition("tagname", array("src", "href","background"), array(".gif", ".html", ".jpg"));
$message =& new Swift_Message($mysubject);
$message->attach(new Swift_Message_Part($myHTMLbody, 'text/html'));
$swift->send($message, 'someone@somewhere.com', 'sender@domain.tld');
Posted: Wed Aug 08, 2007 10:36 am
by Chris Corbyn
Almost:
Code: Select all
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
require_once "lib/Swift/Plugin/FileEmbedder.php";
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
$plugin =& new Swift_Plugin_FileEmbedder();
$plugin->setTagDefinition("tagname", array("src", "href","background"), array(".gif", ".html", ".jpg"));
//Attach the plugin
$swift->attachPlugin($plugin, "file_embedder");
$message =& new Swift_Message($mysubject);
$message->attach(new Swift_Message_Part($myHTMLbody, 'text/html'));
$swift->send($message, 'someone@somewhere.com', 'sender@domain.tld');
Posted: Wed Aug 15, 2007 10:18 am
by roscoe
just to complicate matters, can I run it withthe throttler?
Posted: Wed Aug 15, 2007 12:32 pm
by Chris Corbyn
roscoe wrote:just to complicate matters, can I run it withthe throttler?
Of course

Because the plugins are all effectively "observers" they don't conflict, they just run one-by-one.
Posted: Thu Aug 16, 2007 5:29 am
by roscoe
OK tried to run it, here is the script:
Code: Select all
require_once "control/mylib/Swift.php";
//change this to smtp
require_once "control/mylib/Swift/Connection/SMTP.php";
require_once "control/mylib/Swift/Plugin/FileEmbedder.php";
require_once "control/mylib/Swift/Plugin/Throttler.php";
$body=stripslashes($mailBody);
$plugin =& new Swift_Plugin_FileEmbedder();
$plugin->setTagDefinition("tagname", array("src", "href","background"), array(".gif", ".html", ".jpg"));
//Attach the plugin
$swift->attachPlugin($plugin, "file_embedder");
$message =& new Swift_Message($mailSubject);
$message->attach(new Swift_Message_Part($body));
//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
$throttler =& new Swift_Plugin_Throttler();
$throttler->setBytesPerMinute(20000000); //Roughly 20MB
$swift->attachPlugin($throttler, "throttler");
//or maybe you want to set the number of messages per minute?
$throttler->setEmailsPerMinute(3); //Max of 1 email every 2 seconds
$swift->attachPlugin($throttler, "throttler");
//Try sending the email
$sent = $swift->send($message, $sendout, $sender);
//Disconnect from SMTP, we're done
}
$swift->disconnect();
if ($sent)
{
print "Email sent to all subscribers";
print " $message\n";
exit();
}
else
{ echo"sending_failed";
//$swift->log->dump();
exit;
echo "failed to send";
exit();
}
}
Receive this error:
Fatal error: Call to a member function attachPlugin() on a non-object in /home/addictiv/public_html/maillist-admin.php on line 133
line 133 is
Code: Select all
$swift->attachPlugin($plugin, "file_embedder");
Posted: Thu Aug 16, 2007 7:22 am
by Chris Corbyn
$swift does not exist at the point you're trying to call a method on it

Posted: Thu Aug 16, 2007 7:35 am
by roscoe
d11wtq wrote:$swift does not exist at the point you're trying to call a method on it

Sorry I see it. The email does not come out as HTML though, it just shows the HTML code. What amd I missing here??
Posted: Thu Aug 16, 2007 8:09 am
by roscoe
I have added :
Code: Select all
$message->setContentType("text/html");
but I still get the script of the html rather than the HTML. It seems that the < or > are being translated into < or > would this be why?
Posted: Thu Aug 16, 2007 8:47 am
by roscoe
OK I have it in HTML, the only thing not showing is the background image which I checked IS in the correct place. Any ideas?
this is the HTML ...
Code: Select all
<TABLE borderColor='#00709e' background='/home/addictiv/public_html/htmlpics/backuk.gif' border='1'>
Code: Select all
plugin->setTagDefinition("tagname", array("src", "href","background"), array(".gif", ".html", ".jpg"));
is the array specific to the type of file?
Posted: Thu Aug 16, 2007 12:26 pm
by Chris Corbyn
Those extensions shouldn't have the "." in them. This could be the problem.
Posted: Fri Aug 17, 2007 3:50 am
by roscoe
OK tried that, no joy.
This is what comes out in the email:
Code: Select all
<TABLE borderColor=#00709e
background=/home/addictiv/public_html/htmlpics/backuk.gif border=1>
not a
Code: Select all
<IMG alt=""
src="cid:swift-118734027046c55fee70f4e.9" width=777>
Any idea why it is ignoring the table background ?
Here is the bits of code :
Code: Select all
require_once "control/mylib/Swift.php";
require_once "control/mylib/Swift/Connection/SMTP.php";
require_once "control/mylib/Swift/Plugin/FileEmbedder.php";
require_once "control/mylib/Swift/Plugin/Throttler.php";
$body=stripslashes($mailBody);
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
$plugin =& new Swift_Plugin_FileEmbedder();
$plugin->setTagDefinition("tagname", array("src", "href","background"), array("gif", "html", "gif"));
//Attach the plugin
$swift->attachPlugin($plugin, "file_embedder");
$message =& new Swift_Message($mailSubject);
$message->attach(new Swift_Message_Part($body,"text/html"));
//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}
$throttler =& new Swift_Plugin_Throttler();
$throttler->setBytesPerMinute(20000000); //Roughly 20MB
$swift->attachPlugin($throttler, "throttler");
//or maybe you want to set the number of messages per minute?
$throttler->setEmailsPerMinute(3); //Max of 3 email every 60 seconds
$swift->attachPlugin($throttler, "throttler");
//Try sending the email
$sent = $swift->send($message, $sendout, $sender);
//Disconnect from SMTP, we're done
}
$swift->disconnect();
if ($sent)
{
print "Email sent to all subscribers";
//print " $message\n";
exit();
}
else
{ echo"sending_failed";
//$swift->log->dump();
exit;
echo "failed to send";
exit();
}
//echo"line 188";
exit();
}
exit();
//echo"line 193";