Problem with Swifmailer and FCKeditor

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
jefke007
Forum Newbie
Posts: 5
Joined: Fri Jan 11, 2008 5:48 am

Problem with Swifmailer and FCKeditor

Post by jefke007 »

Hello, i'm new with swiftmailer, i want to use fck editor to create a email.
Here is some code i got.
Every thing works fine, only the mail i get isn't good.
Can't see any images.

When i echo $content is get

Code: Select all

<p>Blabla bla</p>
<p><img width=\"500\" height=\"500\" src=\"" . $message->attach(new Swift_Message_Image(new Swift_File("../gfx/image/bureau.JPG" ))) . "\" /></p>
<p>bla bla</p>
<p><img width=\"500\" height=\"500\" src=\"" . $message->attach(new Swift_Message_Image(new Swift_File("../gfx/image/nachtkast.JPG" ))) . "\" /></p>
<p>bla bla bla </p>
If i copy and past the content of $content in the $message-> ... IT works.
I think there is something wrong with the line off
$message->attach(new Swift_Message_Part($content, 'text/html'));
I have tried "$content" '$content' $content

Code: Select all

<?php
	
 function str_replace_all($search,$replace,$subject) {
  while(strpos($subject,$search)!==false) {
    $subject = str_replace($search,$replace,$subject);
  }
  return $subject;
}	
$string = $_POST['nieuwsbrief'];
$string = addslashes($string);
$content = str_replace_all('src=\"/', 'src=\"" . $message->attach(new Swift_Message_Image(new Swift_File("', $string);
$content = str_replace_all('\" alt=\"\" />', '" ))) . "\" />',$content);		
$content = str_replace_all('WIBS', '..',$content);	
require_once "mail/Swift.php";
require_once "mail/Swift/Connection/SMTP.php";

$smtp =& new Swift_Connection_SMTP("uit.pandora.be", 25);
$swift =& new Swift($smtp);
$message =& new Swift_Message("Your subject");
echo $content ;
$message->attach(new Swift_Message_Part($content, 'text/html'));
if ($swift->send($message, new Swift_Address("jefke007@gmail.com", "Joe"), new Swift_Address("jefke007@gmail.com", "System")))
{
    echo "Message sent";
}
else
{
    echo "Sending failed";
}

//recommended to do this
$swift->disconnect();
?>
Can sombody help me with this? How to put a variable in to swift body ?
Thanks

Greatzz jefke007
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Problem with Swifmailer and FCKeditor

Post by Chris Corbyn »

Code: Select all

$content = str_replace_all('src=\"/', 'src=\"" . $message->attach(new Swift_Message_Image(new Swift_File("', $string); 
Check your use of backslashes, singe and double quotes here. I'd suggest using an editor which does syntax highlighting if not already since this would stick out like a sore thumb in that case ;)

EDIT | In fact, what you're trying to do isn't going to work at all. PHP isn't markup so you can't just replace HTML with PHP and email it. You need a preg_replace() to get this to work.

Code: Select all

function attachImage($matches) {
  global $message; //this isn't great but I don't know enough about your code
  return 'src="' . $message->attach(new Swift_Message_Image(new swift_File($matches[1]))) . '"';
}
$content = preg_replace_callback('~src="([^"]*)"~', 'attachImage', $string);  
jefke007
Forum Newbie
Posts: 5
Joined: Fri Jan 11, 2008 5:48 am

Re: Problem with Swifmailer and FCKeditor

Post by jefke007 »

Isn't working for me.
Alle the text in $content is fine, but the problem is that the content of $content isn't handled as HTML.

when i do
$content = "<p> testing 1234 </p>" ;
I get a good email, but when i use
$content ='<img width=500 height=500 src=\"" . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) ."\"/>';
I get a white image and attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) ."\"/>


I tested
$message->attach(new Swift_Message_Part($content, 'text/html'));
and
$message->attach(new Swift_Message_Part($content));

In al me emails i get the code op $content. So the content in $content isn't handled by $message...
here is the code. I changed is so you can test it.

Code: Select all

<?php
/*    
 function str_replace_all($search,$replace,$subject) {
  while(strpos($subject,$search)!==false) {
    $subject = str_replace($search,$replace,$subject);
  }
  return $subject;
}    
$string = $_POST['nieuwsbrief'];

function attachImage($matches) {
  global $message; //this isn't great but I don't know enough about your code
  return 'src=\"" . $message->attach(new Swift_Message_Image(new swift_File("'.$matches[1].'"))) ."\"';
}
$content = preg_replace_callback('~src="([^"]*)"~', 'attachImage', $string);


$content = str_replace_all('/WIBS', '..',$content); 
echo ($content);
*/
$content ='<img width=500 height=500 src=\"" . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) ."\"/>';
require_once "mail/Swift.php";
require_once "mail/Swift/Connection/SMTP.php";

$smtp =& new Swift_Connection_SMTP("uit.pandora.be", 25);
$swift =& new Swift($smtp);
$message =& new Swift_Message("Your subject");
$message->attach(new Swift_Message_Part($content,"text/html"));
//$message->attach(new Swift_Message_Part("<img width=500 height=500 src=\"" . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) ."\" />","text/html"));
/*

$message->attach(new Swift_Message_Part("<p>fhffhfhhf</p>
<p>sdsds</p>
<p><img width="500" height="500" src=\"" . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) ."\" alt="" /></p>, "text/html"));
*/
if ($swift->send($message, new Swift_Address("jefke007@gmail.com", "Joe"), new Swift_Address("jefke007@gmail.com", "System")))
{
    echo "Message sent";
}
else
{
    echo "Sending failed";
}

//recommended to do this
$swift->disconnect();

?>

Thanks

got any idea?

Thank u
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Problem with Swifmailer and FCKeditor

Post by Chris Corbyn »

Same problem. I've highlighted your code for you to have a closer look. Particularly this:

Code: Select all

$content ='<img width=500 height=500 src=\"" . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) ."\"/>';
require_once "mail/Swift.php";
require_once "mail/Swift/Connection/SMTP.php"; 
Red syntax indicates that it's a string. $message->attach(... yadda yadda is just a plain old string in your code and thus is not being interpreted as PHP. This is what I was trying to say the first time around.
jefke007
Forum Newbie
Posts: 5
Joined: Fri Jan 11, 2008 5:48 am

Re: Problem with Swifmailer and FCKeditor

Post by jefke007 »

is there any way to get is interpreted as PHP??
Maybe i can create a php file with this code and send the php file?

thank u
Last edited by jefke007 on Sun Jan 13, 2008 2:58 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Problem with Swifmailer and FCKeditor

Post by Weirdan »

jefke007 wrote:is there any way to get is interpreted as PHP??
thank u
get it out of the string, like this:

Code: Select all

 
$content ='<img width=500 height=500 src="' . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) .'"/>';
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Problem with Swifmailer and FCKeditor

Post by Chris Corbyn »

jefke007 wrote:is there any way to get is interpreted as PHP??
thank u
Yes, fix the wrong use of double quotes. You open your string with single quotes, then *try* to get out of the string by using souble quotes followed by . (dot). You need to get out of the string the same way you got into the string.

Code: Select all

$content ='<img width=500 height=500 src="' . $message->attach(new Swift_Message_Image(new swift_File("../gfx/image/bureau.JPG"))) .'"/>';
require_once "mail/Swift.php";
require_once "mail/Swift/Connection/SMTP.php";
jefke007
Forum Newbie
Posts: 5
Joined: Fri Jan 11, 2008 5:48 am

Re: Problem with Swifmailer and FCKeditor

Post by jefke007 »

isn't working for me, i get error :p
Fatal error: Call to a member function attach() on a non-object in C:\wamp\www\WIBS\admin\module_massmail_mail.php on line 17

i'm traing to change the " and the \ and so on but isn't working :(

Thanks for all the help in advance
jefke007
Forum Newbie
Posts: 5
Joined: Fri Jan 11, 2008 5:48 am

Re: Problem with Swifmailer and FCKeditor

Post by jefke007 »

I GOT IT WORKING :p:p:p:p:p:p:p

$string = str_replace_all("/WIBS", '..',$string);
function attachImage($matches) {
global $message; //this isn't great but I don't know enough about your code
return "src=\"". $message->attach(new Swift_Message_Image(new swift_File("$matches[1]")))."\" ";
}

require_once "mail/Swift.php";
require_once "mail/Swift/Connection/SMTP.php";

$smtp =& new Swift_Connection_SMTP("uit.pandora.be", 25);
$swift =& new Swift($smtp);
$message =& new Swift_Message("Your subject");
$content = preg_replace_callback('~src="([^"]*)"~', 'attachImage', $string);

THANKS, So if somebody wants this script :)
So now u can use fck editor to create a email with image and tekst.
Post Reply