Page 1 of 1

Attachment not being sent

Posted: Thu Jul 26, 2007 5:17 pm
by neoaddict
PHP 4.3.11
Swift 3.2.6

Code: Select all

<?php

set_time_limit(0); 
error_reporting(E_ALL);
//Load in the files we'll need
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
//Start Swift
$smtp =& new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS);
$smtp->setUsername("test@gmail.com");
$smtp->setPassword("aaaaaaaaaaaaaaaaaaaaaaa");
 
$swift =& new Swift($smtp);;

$message =& new Swift_Message("My subject");
$message->attach(new Swift_Message_Part("I have attached a file to this message!"));
 
//Use the Swift_File class
$message->attach(new Swift_Message_Attachment("aaaaaaa"), "aaaaaa", "application/x-gzip");
 
//Now check if Swift actually sends it
if ($swift->send($message, "test@test.com", "test@gmail.com")) echo "Sent";
else echo "Failed";
?>
It keeps on sending me a file called "file.att.0" instead of the actual file. I tried putting in the file extension, didn't work either. The MIME type is application/x-gzip, I've checked in Nautilus. The file in question is ~7 MB.

I tried with a smaller text file, but that worked. :(

Thanks. :)

Posted: Fri Jul 27, 2007 2:13 am
by Chris Corbyn
You've got the parentheses in the wrong place. $message->attach() only takes one argument, the others belong in the constructor of the attachment:

Code: Select all

$message->attach(new Swift_Message_Attachment("aaaaaaa", "aaaaaa", "application/x-gzip"));

Posted: Fri Jul 27, 2007 1:39 pm
by neoaddict
The attachment has the right file name now, but the file size is 1 KB. 8O

Posted: Fri Jul 27, 2007 3:04 pm
by Chris Corbyn
neoaddict wrote:The attachment has the right file name now, but the file size is 1 KB. 8O
The file is a text file containing the text "aaaaaa" ;)

What exactly are you trying to do?

This maybe?

Code: Select all

$message->attach(new Swift_Message_Attachment(new Swift_File("aaaaaaa"), "aaaaaa", "application/x-gzip"));

Posted: Fri Jul 27, 2007 4:25 pm
by neoaddict
I'm trying to send a gzip archive to my e-mail address.

Tried that, gave me file.att.0, a 1 KB file again. Using new Swift_file keeps on making me download the PHP script.

Posted: Fri Jul 27, 2007 4:38 pm
by Chris Corbyn
The attachment class takes a string which is the file contents, otherwise it takes a stream as an instance of Swift_File. Your version was passing a string which will become the file contents, however your string is just "aaaaaa" which I guess is your filename.

This would also work:

Code: Select all

$message->attach(new Swift_Message_Attachment(file_get_contents("aaaaaaa"), "aaaaaa", "application/x-gzip"));