Page 1 of 1

Swift 4.0 attachment path

Posted: Tue Mar 10, 2009 6:50 pm
by locus
In SwiftMail 3 to send attachment the following example works
if(!empty($_FILES["attachment"]["tmp_name"]))
{
if ($_FILES["attachment"]["error"])
{
//Redirect if the upload has failed
header("Location: ?p=emailFailure&error=upload_failed");
exit();
}
$file_path = $_FILES["attachment"]["tmp_name"];
$file_name = $_FILES["attachment"]["name"];
$file_type = $_FILES["attachment"]["type"];
}

//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}

//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
$message->attach( new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}

In SwiftMail 4.0 how is this accomplished?
When just using:
$message->attach(Swift_Attachment::fromPath($file_path.'/'.$file_name, $file_type));
it does not work

Re: Swift 4.0 attachment path

Posted: Tue Mar 10, 2009 7:16 pm
by Chris Corbyn
If in version 3 you had this, it's the Swift_File bit that specifies the path...

Code: Select all

$message->attach( new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
So your version 4 code should look like this:

Code: Select all

$message->attach(Swift_Attachment::fromPath($file_path, $file_type)->setFilename($file_name));

Re: Swift 4.0 attachment path

Posted: Tue Mar 10, 2009 11:17 pm
by locus
Thanks! - works great

Re: Swift 4.0 attachment path

Posted: Wed Mar 11, 2009 1:35 am
by Chris Corbyn
It's also good practice to move_uploaded_file() before attaching it, so your code would become something like:

Code: Select all

$new_path =  '/some/path/' . $file_name;
if (move_uploaded_file($file_path, $new_path)) {
  $message->attach(Swift_Attachment::fromPath($new_path));
}
NOTE: Most common MIME types are automatically detected in version 4.

Re: Swift 4.0 attachment path

Posted: Mon Mar 23, 2009 5:59 pm
by Terrantino
Chris Corbyn wrote:It's also good practice to move_uploaded_file() before attaching it, so your code would become something like:

Code: Select all

$new_path =  '/some/path/' . $file_name;
if (move_uploaded_file($file_path, $new_path)) {
  $message->attach(Swift_Attachment::fromPath($new_path));
}
NOTE: Most common MIME types are automatically detected in version 4.
Also, I just want to confirm if the 'fromPath()' method accept array values, and how would I best implement that?

Thanks.

Terrantino