Page 1 of 1

[SOLVED] inline images

Posted: Wed Feb 28, 2007 8:51 am
by phobos
Hi, I've been using Swift for last 2 months to control e-mail traffic on my website and I'd like to do some e-mail info action. In order to do this I'd like to sent some messages with an image inline just the way spammers do. Here's the snippet of code I'm using (it's Swift version 2, I don't have time to migrate to version 3 now)

Code: Select all

$email = $this->emailContent; // get the template

// some manipulation with content - insert name, current date and then

$mailer = new Swift(new Swift_Connection_SMTP($address));

$cid = $mailer->addImage($this->config['image_file']);
$email = mb_eregi_replace('image_name', $cid, $email);

if ($mailer->authenticate($user, $pass) {
  $mailer->send($toAddress, $address, 'test', $email);
} 
else echo 'mailer failed to connect';
$mailer->close();
As you can guess, my email template contains part
<img src="image_name" style="some_style_over_here"/>
And the problem is that my image isn't even attached to e-mail at all! Should I do anything more? I thought that simple addImage method will be OK.

Thanks in advance
phobos

Posted: Wed Feb 28, 2007 8:55 am
by feyd
This post didn't seem applicable to the tutorial thread it was posted in, so it's been split.

Posted: Wed Feb 28, 2007 9:07 am
by phobos
Sorry for misplacing the post. I forgot to mention that var_dump($mailer) just before send(...) says that image is attached. And it still is not displayed in any client.

Posted: Wed Feb 28, 2007 9:42 am
by Chris Corbyn
Don't pass the message body to send() in v2. Use addPart() instead if you are adding attachments or images.

Code: Select all

$email = $this->emailContent; // get the template 

// some manipulation with content - insert name, current date and then 

$mailer = new Swift(new Swift_Connection_SMTP($address)); 

$cid = $mailer->addImage($this->config['image_file']); 
$email = mb_eregi_replace('image_name', $cid, $email); 
$mailer->addPart($email, "text/html");

if ($mailer->authenticate($user, $pass) { 
  $mailer->send($toAddress, $address, 'test'); 
} 
else echo 'mailer failed to connect'; 
$mailer->close();

Posted: Wed Feb 28, 2007 12:55 pm
by phobos
Thanks! That did the job. Another thing is - if I sometimes get to 'mailer failed to connect' part, what could it be? I send two mails from one account OK, but third one fails... or sometimes first one fails but the second one goes away.