embedding externally hosted images

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
kingsquare
Forum Newbie
Posts: 2
Joined: Mon Mar 26, 2007 11:49 am

embedding externally hosted images

Post by kingsquare »

All,

Whilst trying to user your classes i wanted to embed externally hosted images in my HTML mails...
Using PHP4 i came across the following (currently) working work-around:

classFile.php
Method:

Code: Select all

function setPath($path)
{
/*	if (!file_exists($path))
	{
		Swift_Errors::trigger(new Swift_FileException("No such file '" . $path ."'"));
		return;
	}
*/
	$this->handle = null;
	$this->path = $path;
	$this->name = null;
	$this->name = $this->getFileName();
}
my question: is that correct or will it break the code elsewhere? While trying it out with embedding one 1 local image and 1 external the above solution proved to work... but am i missing something? Another question related: why wasnt this feature incorporated into the class in the first place? Did i miss a comment here or there?

Thanks for the info!

[edit]
Just found out: it seems that when a 404 is encounterd: the mail just isn't sent (even though send() returns true)
is thát the reason for leaving out external images?
[/edit]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need allow_url_fopen in php.ini I think.

I'm not 100% if file_exists() will work on remote files either way though. I'll have to test that. Basically, you don't want to embed a file that doesn't exist or you'll just create an invalid email that will get stuck in spam filters. I could use a different check than file_exists() though. But why would you want to attach something that's giving a 404 page?
kingsquare
Forum Newbie
Posts: 2
Joined: Mon Mar 26, 2007 11:49 am

Post by kingsquare »

It's not that i want to embed 404-urls... i don't want my user to worry about images ;) (internally nor externally)

Using Swiftmailer to send user generated e-mails (from a WYSIWYG editor) it's possible to have anything (from links, to images, to whatever kinds of HTML elements) in that mail.. Right now it's not possible to see if the e-mail is sent or not because the send()-function always returns true even though it exited whilst loading in the 404 `image`.

(hope i'm clear enough, english is not my native tongue)

BTW in one of your many (cudo's) tutorials i came across the following example:

Code: Select all

// straight from the `plugin_decorator`-tutorial

require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
require_once "lib/Swift/Plugin/Decorator.php";
 
//Instantiate Swift as usual
$swift =& new Swift(new Swift_Connection_SMTP("your-server.tld"));
 
//Create the message, using some unique variables to search for
$message =& new Swift_Message("Hey {name}, what's up?", "It's a bit {weather} today wouldn't you say?");
 
//Specify the list of replacements as a 2-d array
$replacements = array(
    "joe@bloggs.com" => array("{name}" => "Joe", "{weather}" => "chilly"),
    "fred@perry.com" => array("{name}" => "Fred", "{weather}" => "muggy")
);
 
//Load the plugin with these replacements
$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
 
//Send messages
$swift->send($message, "fred@perry.com", "you@your-address.com");
$swift->send($message, "joe@bloggs.com", "you@your-address.com");
 
$swift->disconnect();
my question is: am i going at this correctly if i wanted to send it using a RecipientList(); instead of sending it to the subscribers using send($message, $email, $from) ? (i.e. when sending it to 10K recipients)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

send() wouldn't return false if the exception was there... the exception would cause a fatal error if uncaught :)

I could perhaps change the file_exists() to be a preliminary fopen() check, or even just wait until Swift tries to read the stream. But if the image doesn't exist remotely, Swift will still throw a FileException, so you'd want to do your own check before attempting to send the email.

Swift_Plugin_Decorator does a basic str_replace(). It will work with a RecipientList yes, but for 10K recipients I'd advise against using a recipientlist since you'll need to load all 10K recipients into memory at the same time (i.e. to populate the recipientlist object). If you just loop over a buffered result from MySQL you'll cut that memory usage down.
Post Reply