Attachment not being sent

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
neoaddict
Forum Commoner
Posts: 44
Joined: Thu Jan 12, 2006 12:28 pm

Attachment not being sent

Post 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. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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"));
neoaddict
Forum Commoner
Posts: 44
Joined: Thu Jan 12, 2006 12:28 pm

Post by neoaddict »

The attachment has the right file name now, but the file size is 1 KB. 8O
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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"));
neoaddict
Forum Commoner
Posts: 44
Joined: Thu Jan 12, 2006 12:28 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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"));
Post Reply