Is this a swiftmail problem?

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
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Is this a swiftmail problem?

Post by Stryks »

I'm having some issues with swiftmail and I dont know if it has to do with how I am doing things, or with swiftmail itself. I'm using 3.2.6 with php5 if it makes any difference.

I'm sending a multi part email with embedded images for the html part.

Initially I was manually embedding and referencing the images, and I eventually got that to work, though it was pretty ugly. I have the parts stored in different files, a .txt and a .htm, which are just loaded into a string, so switching the image references to the embedded values caused alot of issues.

Anyhow, I eventually got that working, in outlook at least, but it was just showing a red broken image in thunderbird. I was actually starting to think that thunderbird didnt display inline images at all.

Late last night, I swapped over to using the FileEmbedder plugin to see what I could see and ... hey hey ... thunderbird now works like a charm, and it's still seems all good in outlook.

So I went to bed last night all happy and contented, only to come back today to find that outlook now puts my emails into the junk mail folder, and defaults back to text mode viewing. Also, in case it's related, there appear to be extra line breaks (appear as blocks if highlighted, so might be tabs) both above and below the text body, which dont appear in the source code.

By this stage, I've gone through so many permutations trying to test this thing, that I cant figure out if this is something that I has happened since using the FileEmbedder, or if I changed the email in some other way, or even if I changed something in outlook.

I'm going to run some tests, but I just wondered if anyone can pick something simple out of the air.

Cheers .... will post back anything else I find.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

I'm still a little lost on this issue, as to why it wasnt an issue at first but now it is. Maybe it's something about the way the embedder works, but for some odd reason I had height and widths set for the images and that was breaking the email.

Removed all the images and worked back, and once I got rid of those, it worked. Oddly enough, it still worked when doing it the previous way. Also, when viewing the source of the email, it still *appears* correct, ie, height and width tags are still present and in proper format.

Anyhow, it's working again for the time being.

Thanks for putting in the work on Swiftmailer by the way d11wtg if you're out there. It's such a huge relief to finally find a usable and fully functional solution to what might be one of the most often used but most difficult to get right areas of website creation / interaction.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

If you could post some example email source code and PHP source code for the broken emails it would be a help thanks :)

Glad it's working ok at the moment.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

I started to a few times, I really did.

But it's kinda complicated by the way I'm going about loading the emails. I'll have to post the loading method along with the usual gear.

I will post it when I get back in front of my machine though.

Cheers
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Ok ... firstly, here is the code I'm using to actually send the mail.

Code: Select all

require_once(PATH_SWIFT . "Swift.php");
require_once(PATH_SWIFT . "Swift/Connection/SMTP.php");
require_once(PATH_SWIFT . "Swift/Plugin/FileEmbedder.php");

$plugin =& new Swift_Plugin_FileEmbedder();
//	$plugin->setEmbedRemoteFiles(false);

$swift = new Swift(new Swift_Connection_SMTP("mail.xxxx.com"));
$swift->attachPlugin($plugin, "file_embedder");

$std_url = "http://" . $_SERVER['SERVER_NAME'];
$act_url = $std_url . "/activate.php?id=$new_user_id&code=$act_code";

require_once(PATH_CLASS . "class_eloader.php");
$eLoader = new eLoader();
$eLoader->transpose_data = array("{std_url}"=>$std_url, "{act_url}"=>$act_url);			// Set data to be transposed if found

$sender = new Swift_Address("sender@xxxx.com", "Sender");								// Create Sender
$recipient = new Swift_Address($clean->post["email_1"], $clean->post["givenname"]);		// Create Recipient
$message = new Swift_Message("Welcome");												// Create the message
$message->attach(new Swift_Message_Part($eLoader->load_text("signup")));				// Load text body
$message->attach(new Swift_Message_Part($eLoader->load_html("signup"), "text/html"));	// Load html body

// Send mail unless on testing server
if(!$SSL_DISABLE) $swift->send($message, $recipient, $sender);
$swift->disconnect();
So ... you can see that it's a single recipient email, into which I am inserting customised data manually. This is basically because I want to insert the HTML body into an HTML wrapper file so that when creating templates I only need to create the content, and the wrapper will place it in a consistent format for me. I could have used the ... ummm .. decorator plugin, but it just seemed easier to put into my own class for the purposes of a single email.

Anyhow, it relies on two other classes, one is my form validation class $clean, and the other is the more relevant $eLoader class. There are also a few constants in there but they should be pretty self explanatory.

Ok, so my eLoader class is basically just a roughed out system of loading email templates into swift. Nothing too complex.

class_eloader.php

Code: Select all

class eLoader {
	// Class Variables
	var $transpose_data = array();

	function load_text($name) {
		$content = $this->open_message_file($name . ".txt");
		$content = $this->transpose($content);
		return $content;
	}

	function load_html($name) {
		$content = $this->open_message_file($name . ".htm");
		$content = $this->transpose($content);
		
		$wrapper = $this->open_file("html_wrapper.html");
		$this->transpose_data = array("{mail_body}"=>$content, "{filepath}"=>MAIL_FOLDER);
		$content = $this->transpose($wrapper);
		
		return $content;
	}



	// *****************************************
	// *****************************************
	// Internal Functions	
	// *****************************************

	function open_message_file($filename) {
		return $this->open_file("messages/" . $filename);
	}

	function open_file($filename) {
		$filename = MAIL_FOLDER . $filename;
		$handle = fopen($filename, "rb");
		$contents = fread($handle, filesize($filename));
		fclose($handle);
		return $contents;
	}

	function transpose($raw_data) {
		foreach($this->transpose_data as $tag=>$value) $raw_data = str_replace($tag, $value, $raw_data);
		return $raw_data;
	}
}
So I just have the html_wrapper.html file with some formatting in it, with {body} where the body is to go, then two files (1 .txt, 1 .htm) with text and html versions respectively, which have the transpose_data inserted.

So where is this leading ... nowhere I guess. That's probably why I didn't immediately jump into listing pages and pages of code. The eLoader class complicates matters, but it really *shouldnt* impact on swift as far as I can tell, as it's work is done before swift sees it. I mean, the email was looking fine in thunderbird and just showing text version (though with the added spaces) in outlook junk mail. You could click "show in HTML" from that text version in junk mail and it would still show perfectly.

Anyhow, I've just posted this so that you can see how I'm using it.

The real purpose for this update however is just to let you know that I think it's content related after all.

Originally, the URL I passed used "?user= ....". At some point (maybe when I changed the image tags) I changed it to "?group=..." and we were back out of junk. A day or so ago I changed it back to "user" and back into junk mail it went. I changed it to "id" and I'm good again. I haven't put the sizes back into the images (I can if you want closure on it) but it would seem that it has flagged the word "user" as part of the URL.

So there we have it. Any thoughts (besides how pointless or poorly implemented the eLoader class is :lol:)?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Oh, just in passing, I was reading another thread about adding the plugin using:

Code: Select all

$plugin =& new Swift_Plugin_FileEmbedder();
// then make changes and attach
I read it and wanted to point out that it would be great to see an example of that $plugin being attached to the $swift instance on the documentation page. I muddled through it but it seems that I wasn't the only one confused by the example on that page.

I actually started to reply on that thread, but I dunno, I didn't want to come across poorly by dropping this comment into someone else's thread with nothing else helpful to say.

Anyhow ... Thanks again. 8)
Post Reply