[FIXED] attachment problems

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
profphp
Forum Newbie
Posts: 3
Joined: Tue Mar 06, 2007 10:04 pm

[FIXED] attachment problems

Post by profphp »

Code: Select all

require_once("lib/swift/Swift.php");
require_once("lib/swift/Swift/Connection/SMTP.php");
.....
$smtp = new Swift_Connection_SMTP('smtp.gmail.com',
                                  Swift_Connection_SMTP::PORT_SECURE,
                                  Swift_Connection_SMTP::ENC_TLS);
$smtp->setUsername($GMAIL_USERNAME);
$smtp->setPassword($GMAIL_PASSWORD);
$swift = new swift($smtp);
$message =& new Swift_Message("test1", "test2");
$message->setContentType("text/html");
$message->attach(new Swift_Message_Attachment(new Swift_File("one.out")));
$message->attach(new Swift_Message_Attachment(new Swift_File("two.out")));

$resultmail = $swift->send($message, $recipients, new Swift_Address($GMAIL_ADDRESS, "MONSTER"));
if (!$resultmail){
  echo "error: " .$resultmail;
 }
$swift->disconnect();
There are two problems in the e-mail that has been sent by the above code.
1-) Attachment files lose the first character of the filename...
So it is attached as "ne.out" and "wo.out"

2-) Body of the e-mail is lost. "test2" is nowhere to be found :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You have to attach a body part (Swift_Message_Part) if you're sending attachments because it's a multipart message. If you don't, your body becomes the text that would ordinarily be a mime warning saying "Upgrade you email client, it's archaeic". I designed it like that deliberately.

Technically, you cannot set the entire message to be text/html if it contains attachments because it has to be multipart/mixed so Swift would have adjusted that.

Code: Select all

$swift = new swift($smtp);
$message =& new Swift_Message("test1");
$message->attach(new Swift_Message_Attachment(new Swift_File("one.out")));
$message->attach(new Swift_Message_Attachment(new Swift_File("two.out")));
$message->attach(New Swift_Message_Part("test2", "text/html"));
Are you using PHP5? If not it's critical that you use the reference operator because Swift does a bit of Juggling with references internally. Also, which version of Swift is this? :)

If you're still getting truncated filenames can you post me what this gives you:

Code: Select all

$stream = $message->build();
echo "<pre>" . $stream->readFull() . "</pre>";
EDIT | I do apologise, it's clear you're using PHP5 or you would have gotten parse errors above, my bad. The note about the body still stands ;)

EDIT 2 | Got it, test extended to include what happens if you use a file from the current working directory:

Code: Select all

public function testFileNameIsReturned()
	{
		$file = new Swift_File("../files/manchester.jpeg");
		$this->assertEqual("manchester.jpeg", $file->getFileName());
		
		$file->setPath(__FILE__); //TestOfFile.php
		$this->assertEqual(basename(__FILE__), $file->getFileName());
		
		chdir("../files");
		$file = new Swift_File("manchester.jpeg");
		$this->assertEqual("manchester.jpeg", $file->getFileName());
	}
Fail: testFileNameIsReturned -> Equal expectation fails at character 0 with [manchester.jpeg] and [anchester.jpeg] at [/home/d11wtq/public_html/swiftmailer/trunk/php5/tests/units/testcases/TestOfFile.php line 54]
Change:

Code: Select all

===================================================================
--- trunk/php5/lib/Swift/File.php       (revision 271)
+++ trunk/php5/lib/Swift/File.php       (working copy)
@@ -88,7 +88,8 @@
                else
                {
                        $path = str_replace("\\", "/", $this->getPath()); //For windows paths, turn them into UNIX style paths
-                       return substr($path, strrpos($path, "/")+1);
+                       if (strpos($path, "/") === false) return $path;
+                       else return substr($path, strrpos($path, "/")+1);
                }
        }
        /**
I'm making a release this afternoon so you could either path that diff in, or wait for the new release :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Changes available in 3.0.6, just released.
Post Reply