[SOLVED] HTML and images
Moderators: Chris Corbyn, General Moderators
[SOLVED] HTML and images
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
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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
It's advanced, but create the HTML, manually setting the Content-ID from the images:
Store that in the database somewhere. Now you'll need a table which maps "cid" values to actual images on disk:
The when you send, set the HTML body:
Then attach all the images:
Voila 
EDIT | There are other ways, but effectively if you know the cid: (content-id) value then you can manually apply the image.
Code: Select all
$html = <<<HERE
<img src="cid:img1" alt="whatever" /> and <img src="cid:img2" alt="whatever again" />
HERE;Code: Select all
cid path
--------------------
img1 /some/image.jpg
img2 /another/image.jpgCode: Select all
$message->attach(new Swift_Message_Part($html_from_db, "text/html"));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);EDIT | There are other ways, but effectively if you know the cid: (content-id) value then you can manually apply the image.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
OK I have had a read of the file embedder and have some Qs.
do I assume this would work:
- I assume this tells swift what file extensions to accept?
so it should read
Code: Select all
$plugin->setTagDefinition("tagname", array("src", "href"), array("ext1", "ext2", "ext3"));Code: Select all
$plugin->setTagDefinition("tagname", array("src", "href","background"), array(".gif", ".html", ".jpg"));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');- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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');- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
OK tried to run it, here is the script:
Receive this error:
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();
}
}line 133 isFatal error: Call to a member function attachPlugin() on a non-object in /home/addictiv/public_html/maillist-admin.php on line 133
Code: Select all
$swift->attachPlugin($plugin, "file_embedder");- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I have added :
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?
Code: Select all
$message->setContentType("text/html");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 ...
is the array specific to the type of file?
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"));- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
OK tried that, no joy.
This is what comes out in the email:
not a
Any idea why it is ignoring the table background ?
Here is the bits of code :
This is what comes out in the email:
Code: Select all
<TABLE borderColor=#00709e
background=/home/addictiv/public_html/htmlpics/backuk.gif border=1>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";